Tar Tar - 5 months ago 15
Android Question

In order to catch BOOT_COMPLETED, is it enough just to register the BroadcastReceiver in the `AndroidManifest.xml`?

In order to catch

android.intent.action.BOOT_COMPLETED
events, is it enough just to register the
BroadcastReceiver
in the
AndroidManifest.xml
? like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myDomain.myApp"
android:installLocation="internalOnly">
<!-- ... stuff ... -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- ... stuff ... -->
<application ...>
<!-- ... stuff ... -->
<receiver android:name=".bgServices.MyBootBroadcastReceiver">
</receiver>
<!-- ... stuff ... -->
</application>
</manifest>


The
BroadcastReceiver
:

package com.myDomain.myApp.bgServices;

// imports ...

public class MyBootBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Log.d("my-log", "MyBootBroadcastReceiver.onReceive()");
Toast.makeText(context, "YEAY!", Toast.LENGTH_LONG).show();
}
}


I'm asking because I'm sending:

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -n com.myDomain.myApp/.bgServices.MyBootBroadcastReceiver


But nothing happens.

(and I run the app, not just pushing it to the device)

Answer

Add the following IntentFilters to your BroadcastReceiver:

<receiver android:name=".bgServices.MyBootBroadcastReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    </intent-filter>
</receiver>

Although android.intent.action.BOOT_COMPLETED should be enough, it seems some devices require android.intent.action.QUICKBOOT_POWERON also.

For more information, you should take a look at the developer's guide on Intents and IntentFilters.

Comments