<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastofboot">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".BootCompleteReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
然而,開機並不能接受到廣播。
后來在網上查看問題所在,找到答案。
如果是三方應用沒有系統權限的話,無解。
原因如下:
谷歌為了安全考慮(避免流氓軟件、病毒啊干壞事,還能提高效率),4.0以后加了2個Flag:FLAG_INCLUDE_STOPPED_PACKAGES和FLAG_EXCLUDE_STOPPED_PACKAGES。系統發出的廣播帶有FLAG_EXCLUDE_STOPPED_PACKAGES這個flag,在應用進程沒有啟動的情況下是不能接收到的。
當然如果是用戶自定義的廣播可以帶有FLAG_INCLUDE_STOPPED_PACKAGES這個flag,那么即使應用沒啟動也可以收到廣播(很遺憾啊!只能是三方自定義的廣播)。
鏈接:http://bbs.csdn.net/topics/390630274/
