BroadcastReceiver 需要 android.permission.RECEIVE_BOOT_COMPLETED

BroadcastReceiver requires android.permission.RECEIVE_BOOT_COMPLETED

我的 Android 应用程序需要收到有关 BOOT_COMPLETED 事件的通知。 AndroidManifest.xml 包含 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 并且在 <application> 标签内 我有以下接收器定义:

<receiver android:name=".OnBootReceiver" 
          android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

是否需要android:permission="android.permission.RECEIVE_BOOT_COMPLETED"?如果它没有到位会发生什么,是否存在任何应用程序能够模拟启动事件并调用我的应用程序的风险?

在某些示例中,the receiver contains the RECEIVE_BOOT_COMPLETED permission and some the receiver does not。是否存在 API 级别的特定差异?

Android 文档指出:如果您不请求此权限,您将不会在那时收到广播。

Link

将收件人标记为

<receiver android:name=".OnBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

确保还包含完整的启动权限。

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

Is the android:permission="android.permission.RECEIVE_BOOT_COMPLETED" required?

不,对于这种特殊情况,您不一定需要 <receiver> 声明中的 permission 属性。来自 docs:

android:permission

The name of a permission that broadcasters must have to send a message to the broadcast receiver. If this attribute is not set, the permission set by the <application> element's permission attribute applies to the broadcast receiver. If neither attribute is set, the receiver is not protected by a permission.

所以如果你想确保只有具有授权权限的广播者才能发送它,你才需要这个属性。但是,BOOT_COMPLETED是一个无论如何只能由系统发送的protected intent。把它放在那里不会有什么坏处,但也没有必要。

编辑:

可能permission 属性留在那里不会有什么坏处,但是有这么多 Android 版本和设备变化,我不会包括属性只是为了确定。我没有将它包含在我的应用程序中。