广播接收器的权限拒绝

Permission Denial for Broadcast Receiver

我正在尝试设置接收器以在 phone 重新启动后重新启动我的应用程序 alarms/notifications。

我遇到了权限拒绝错误:

W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.BOOT_COMPLETED flg=0x9000010 (has extras) } to com.closedbracket.trackit/.BootBroadcastReceiver requires android.permission.RECEIVE_BOOT_COMPLETED due to sender null (uid 1000)

我看了很多与此类似的 SO 问题,但还没有找到解决方案。

这是我的清单:

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    <uses-permission android:name="com.android.permission.RECEIVE_BOOT_COMPLETED" />
<application
....
<receiver
            android:name="com.closedbracket.trackit.BootBroadcastReceiver"
            android:enabled="true"
            android:exported="false"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>
</application>

这是我的 BootBroadcastReceiver:

public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Log.i("BootBroadcastReceiver", "Received");
}}

我正在使用 Android Stuido 模拟器通过重启功能对此进行测试。然后我检查日志并查看引用我的广播接收器的 Permission Denial 行,但没有看到我的 onReceive 方法日志。

几乎尽我所能,甚至更改清单的 android:enable/export 值,并在其中添加权限。没有区别。

如果有人有任何想法,请告诉我。谢谢。

您拥有 com.android.permission.RECEIVE_BOOT_COMPLETED 权限,但如错误消息所述,您应该拥有 android.permission.RECEIVE_BOOT_COMPLETED 而开头没有 com.

每个 Intent 过滤器只能有一个操作。那是你的问题。 由于某种原因,它会回到列表中的最后一个,在您的情况下 QUICKBOOT_POWERUN。 在广播接收器中添加 2 个 Intent 过滤器,每个过滤器只有 1 个动作,它将成功接收两个广播。