0

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)

Tar
  • 8,529
  • 9
  • 56
  • 127
  • @jankigadhiya, see the last line in my post: "(and I run the app, not just pushing it to the device)" – Tar Jun 17 '16 at 11:27

2 Answers2

1

No That is not. You did not specified the component which will receive the ACTION android.intent.action.BOOT_COMPLETED. So you need to add intent-filter to your receiver

You should do it like below.

<receiver android:name=".bgServices.MyBootBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
1

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.

earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63