Android自定义权限 - Marshmallow

bra*_*all 31 permissions android android-6.0-marshmallow

背景

从历史上看,Android自定义权限一直是一个混乱,并且依赖安装顺序,这已知会暴露漏洞.

在API 21之前,有一个令人不安的解决方法,即在您的Manifest中声明另一个应用程序的自定义权限,授予权限......但是,自API 21以来,只有一个应用程序可以声明自定义权限并安装另一个应用程序声明同样的许可,将被阻止.

替代方案是重新安装需要许可的应用程序,因此系统会检测到它们,但这不是一个好的用户体验.或者在运行时检查调用应用程序的权限,但这并非没有理论上的缺陷.

问题

从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>
Run Code Online (Sandbox Code Playgroud)

从第三方应用程序,声明它使用Manifest中的自定义权限(并通过对话框或设置接受它)并调用:

    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);
Run Code Online (Sandbox Code Playgroud)

结果将返回CANCELED并且日志将显示:

system_process W/BroadcastQueue:权限拒绝:接收Intent {act = com.example.app.REQUEST_RECEIVER flg = 0x10(有额外内容)}到com.example.app/.MyBroadcastReceiver需要com.example.app.permission.CONTROL_EXAMPLE_APP由于发件人com.example.thirdparty

如果我使用标准ActivityCompat.requestPermissions()对话框允许用户接受权限,则接收器正如您所期望的那样正常工作.

这是预期的行为吗?或者我有点忽略了什么?

提出一个对话说起来似乎很荒谬

应用程序示例App希望使用示例应用程序的权限

它确实可能涉及用户,为他们提供这样一个荒谬的请求.

我当然可以将权限描述和名称更改为他们可以接受的内容,例如" 与其他已安装的应用程序通信 ",但在我叹息并采取该方法之前,我想我会问这个问题.

注意

有序广播的例子是复制问题.我的应用程序确实使用了内容提供程序的其他实现和绑定服务.它不是我需要的替代实现,它是对问题的确认.

感谢您阅读此内容.

编辑:为了澄清,对于其他实现,例如声明服务上的权限(这将是最简单的复制),将自动授予声明的自定义权限.

Vij*_*jai -1

虽然用户看到在同一个应用程序中声明的应用程序的权限请求可能会含糊不清,但这就是自 Marshmallow 以来 Android 的设计运行方式。我认为,从 Android 的角度来看,这种行为是符合预期且正确的。