android - 具有隐式 Intent 的自定义权限

标签 android broadcastreceiver android-permissions android-security

鉴于 Android 中的安全模型,我正在尝试对广播接收器使用自定义权限。

我做了什么:

我已经为接收者声明了自定义权限,从而限制了它可以接收的广播。 list 中的一些代码:

<permission android:name="abc"/>

<receiver android:name=".UpdateUserReceiver"
        android:permission="abc"
        android:enabled="true"
        android:exported="false">

        <intent-filter>
            <action android:name="android.intent.action.ACTION_UPDATE_USERNAME"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
</receiver>

现在我希望接收器 UpdateUserReceiver 将只接收来自使用权限“abc”的组件的广播。

广播发送代码:

// Update username. Invoke broadcast.
Intent updateUserBroadcast = new Intent();
updateUserBroadcast.putExtra("username", userName);
updateUserBroadcast.setAction("android.intent.action.ACTION_UPDATE_USERNAME");
sendBroadcast(updateUserBroadcast);

发送广播的 Activity :

<activity android:name=".UpdateUserNameActivity">

        <intent-filter>
            <action android:name="com.intent.action.UPDATE_USERNAME"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
</activity>

问题 1: 正如所见, Activity 无处使用接收者声明的权限,以便它可以接收来自 Activity 的广播。但是仍然调用了接收器,我怀疑这是由于使用了隐式 Intent ,但我不确定。有什么想法吗?

问题 2: 在应用程序级别声明的权限标签与接收器内的 android:permission 标签有什么区别?我理解第二个的使用,它在任何人都可以期望接收者接收广播之前强制执行许可,但是为什么需要第一个。这种情况是否需要它,或者是否可以将其删除。无论哪种方式,我都检查过接收器是否收到了广播。

最佳答案

答案1:
<uses-permission><manifest> 中标记请求此应用程序中所有组件的权限,您不需要为单个 Activity 请求权限。并且应用程序声明自定义权限使用 <permission>将自动持有它,无需再次请求。
我猜你的 Activity 和接收者在同一个应用程序中。
“隐式 Intent ”不能打破“许可规则”。

答案2:
<permission><application>将设置适用于所有应用程序组件的权限。
在这里检查: http://developer.android.com/guide/topics/manifest/application-element.html#prmsn

关于android - 具有隐式 Intent 的自定义权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34085502/

相关文章:

安卓测试 : is it correct implement "for Loop" in tests with Espresso?

android - 使用没有具体 Activity.class 的广播接收器进行集成测试

Android 权限 GET_ACCOUNTS 和 USE_CREDENTIALS 自动显示

Android-如何在没有用户交互的情况下授予运行时权限?

android - 我的应用仅使用存储权限,是否需要隐私政策?

Galaxy s3 的 Android list

java - Activity 中的ImageView setBackround动态

java - Retrofit 连接失败返回 RetrofitError.response 为 null

android - 广播接收器未在 AndroidManifest.xml 中注册

android - 如果有一个 Intent 过滤器,为什么要检查 BroadcastReceiver 中的 Intent Action