Broadcast Receiver not receiving ACTION_BOOT_COMPL

2019-07-28 03:24发布

I have been trying to create an app service that does not have an Activity and i ran into some issues.

I want the service to run from boot, so naturally used a BroadcastReciever to catch ACTION_BOOT_COMPLETED this is no problem while testing. I used an Activity to start and stop the service to test it was working then rebooted to see if the boot receiver worked, it did, happy days.

Removed the test Activity and from the application and used the below manifest.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <!-- <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER" />  
        </intent-filter> 
    </activity>  -->
    <receiver android:name=".BootReceiver">
        <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <service android:name=".SWKeepAliveService" />
</application>
</manifest>

Uninstalled then reinstalled the application, reboot the device and nothing. Tested it a few time with adb shell am broadcast -a android.intent.action.BOOT_COMPLETED, still nothing.

After some trial and error i figured out the broadcast receiver never receives the ACTION_BOOT_COMPLETED if the application has never run. Of course without a launcher, it would never run.

Have i missed something? I haven't seen any mention of this in the documentation.

EDIT 1

Ran some tests with my AOSP builds, the above manifest is fine on Gingerbread, but NOT on Jelly Bean. Something must have changed, i can only assume it's for security reasons (understandable). Though I haven't seen any documentation to support the fact.

1条回答
狗以群分
2楼-- · 2019-07-28 04:14

Have i missed something?

Yes.

I haven't seen any mention of this in the documentation.

It was mentioned in the Android 3.1 release documentation, blog posts by balding guys, and a seemingly infinite number of answers here on StackOverflow.

查看更多
登录 后发表回答