背景
从历史上看,Android 自定义权限已经一团糟 https://code.google.com/p/android/issues/detail?id=65864 and 取决于安装顺序 https://code.google.com/p/android/issues/detail?id=25906,众所周知暴露漏洞 https://commonsware.com/blog/2014/02/12/vulnerabilities-custom-permissions.html.
在 API 21 之前,有一个令人不安的解决方法,即在清单中声明另一个应用程序的自定义权限,并授予该权限...但是,自 API 21 以来,只有一个应用程序可以声明自定义权限,并安装另一个应用程序声明同样的许可将被阻止。
替代方法是重新安装需要权限的应用程序,以便系统检测到它们,但是不是一个好的用户体验 https://stackoverflow.com/questions/10620464/reinstall-application-apk-programmatically-without-downloading。或者在运行时检查调用应用程序的权限,但是这并非没有理论缺陷 https://stackoverflow.com/questions/18573139/programmatically-alter-manifest-android-custom-permissions.
Problem
从 Android Marshmallow (6.0 - API 23) 开始,应用程序需要请求用户的许可,使用自己的自定义权限。声明的自定义权限不会自动授予。
这看起来很奇怪,因为现在只有一个应用程序可以声明它。
复制
在 Manifest 中声明自定义权限和 BroadcastReceiver。
<permission
android:name="com.example.app.permission.CONTROL_EXAMPLE_APP"
android:description="@string/control_description"
android:icon="@mipmap/ic_launcher"
android:label="@string/control_label"
android:protectionLevel="normal or dangerous"/>
<uses-permission
android:name="com.example.app.permission.CONTROL_EXAMPLE_APP"/>
// etc
<receiver
android:name="com.example.app.MyBroadcastReceiver"
android:permission="com.example.app.permission.CONTROL_EXAMPLE_APP">
<intent-filter android:priority="999">
<action android:name="com.example.app.REQUEST_RECEIVER"/>
</intent-filter>
</receiver>
从第三方应用程序中,声明它使用清单中的自定义权限(并通过对话框或设置接受它)并调用:
final Intent intent = new Intent("com.example.app.REQUEST_RECEIVER");
context.sendOrderedBroadcast(intent, "com.example.app.permission.CONTROL_EXAMPLE_APP", new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
// getResultCode();
}
}, null, Activity.RESULT_CANCELED, null, null);
结果将返回 CANCELED 并且日志将显示:
system_process W/BroadcastQueue:权限拒绝:接收意图{
act=com.example.app.REQUEST_RECEIVER flg=0x10 (有额外内容) } 到
com.example.app/.MyBroadcastReceiver 需要
com.example.app.permission.CONTROL_EXAMPLE_APP 由于发件人
com.example.thirdparty
如果我使用标准ActivityCompat.requestPermissions()
对话框允许用户接受权限,接收器如您所期望的那样正常工作。
Question
这是预期的行为吗?还是我忽略了一些事情?
提出一个对话说似乎很荒谬
应用程序示例应用程序想要使用示例应用程序的权限
它可能确实让用户担心,向他们提供这样一个无意义的请求。
我当然可以将权限描述和名称更改为他们会接受的内容,例如'与其他已安装的应用程序通信’,但在我叹息并采取这种方法之前,我想我应该问这个问题。
Note
有序广播的例子就是复现问题。我的应用程序确实使用内容提供程序和绑定服务的其他实现。这不是我需要的替代实现,而是对问题的确认。
感谢您阅读本文。
Edit:澄清一下,对于其他实现,例如声明服务的权限(这将是最容易复制的),声明的自定义权限会自动授予。