为了执行你自己的权限,你必须首先在你的AndroidManifest.xml
中使用一个或多个<permission>
标签声明它们。
例如,一个应用程序想用控制谁能启动一个activities,它可以为声明一个做这个操作的许可,如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.me.app.myapp" > <permission android:name="com.me.app.myapp.permission.DEADLY_ACTIVITY" android:label="@string/permlab_deadlyActivity" android:description="@string/permdesc_deadlyActivity" android:permissionGroup="android.permission-group.COST_MONEY" android:protectionLevel="dangerous" /> </manifest>
<protectionLevel> 属性是必需的,告诉系统用户应如何处理应用程序接到请求此权限的通知,或者在这个文档中对这个权限的许可的描述。
<permissionGroup> 属性是可选的,仅仅用于帮助系统为用户显示权限。通常,你要设置这些,向一个标准的系统组(列在android.Manifest.permission_group中) ,或者在更多的情况下要自定义。它更偏向于使用一个已经存在的组,做为简化的权限用户界面显示给用户。
注意:应该为每个权限提供标签(label)
和描述(description)。当用户浏览权限列表时,它们可以为用户展示字符资源,如(android:label
)
或者一个许可的详细信息
( android:description
)
。标签(label)比较短,用几个词来描述该权限保护的关键功能。描述(description)应该是一组句子,用于描述获得
权限的用户可以做什么。我们写描述的习惯是两句话,第一句声明权限,第二句警告用户如果应用许可该权限
时,会发生什么不好的事情。
下面是一个CALL_PHONE权限的标签和描述的例子:
<string name="permlab_callPhone">directly call phone numbers</string> <string name="permdesc_callPhone">Allows the application to call phone numbers without your intervention. Malicious applications may cause unexpected calls on your phone bill. Note that this does not allow the application to call emergency numbers.</string>
你可以在系统中通过shell命令 adb shell pm list permissions
查看权限当前定义。
特别,'-s' 操作以简单粗略的方式为使用者显示权限:
$ adb shell pm list permissions -s All Permissions: Network communication: view Wi-Fi state, create Bluetooth connections, full Internet access, view network state Your location: access extra location provider commands, fine (GPS) location, mock location sources for testing, coarse (network-based) location Services that cost you money: send SMS messages, directly call phone numbers ...