更新时间:2021-07-04 10:25:43
在Android中使用自定义权限是一项相当高级的操作.基本配方是:
Using custom permissions is a fairly advanced thing to do in Android. The basic recipe is:
permission.SHARE_POST
不是一个好选择—添加与您的域名或您要用作应用程序 applicationId
值基础的其他任何内容绑定的前缀.< permission>
元素,并使用 android:name
属性保存步骤1中的权限名称.(可选)为其提供一个 android:protectionLevel
属性(例如, signature
,因此只有使用同一签名密钥签名的应用才能一起使用). android:permission
属性(例如,< activity>
),其值为您从步骤1开始的权限名称.< uses-permission>
属性,并保留一个 android:name
属性步骤1中的权限名称. minSdkVersion
设置为21,因为旧版本的自定义权限存在安全问题.permission.SHARE_POST
is not a good choice — add a prefix that is tied to your domain name or whatever else it is that you are using as the basis for your apps' applicationId
values.<permission>
element, with an android:name
attribute holding the permission name from step #1. Optionally, give it an android:protectionLevel
attribute (e.g., signature
, so only apps signed by the same signing key can work together).android:permission
attribute on the component (e.g., <activity>
), with a value of your permission name from step #1.<uses-permission>
attribute, with an android:name
attribute holding the permission name from step #1.minSdkVersion
to 21, as there are security problems with custom permissions on older versions.如果始终在客户端之前安装防御程序(步骤2和3)(步骤4),则此方法将起作用.如果您希望这些应用程序可以按任一顺序安装,则将上面的第2步替换为:
This will work, if the defender (step #2 and #3) will always be installed before the client (step #4). If you want the apps to be installable in either order, replace step #2 from above with:
< permission>
元素,并保留一个 android:name
属性步骤1中的权限名称.(可选)为其指定一个 android:protectionLevel
属性(例如, signature
,因此只有使用同一签名密钥签名的应用才能一起使用).另外,请确保两个应用程序始终使用相同的签名密钥进行签名,否则它们将无法定义相同的权限.<permission>
element, with an android:name
attribute holding the permission name from step #1. Optionally, give it an android:protectionLevel
attribute (e.g., signature
, so only apps signed by the same signing key can work together). Also, ensure that both apps are always signed by the same signing key, as otherwise they cannot both define the same permission.