2

I'm trying to do a basic BroadcastReceiver that can receive the Action_BOOT_COMPLETED. Whenever I run this in the emulator, it doesn't seem like the code from the BroadcastReceiver code.

Manifest as follows:

<uses-sdk android:minSdkVersion="8" />

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

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <activity
        android:name=".BootAtStartupActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
</application>

</manifest>

MainActivity:

package com.mfcoding.android.bootatstartup;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class BootAtStartupActivity extends Activity {
static final String TAG = "BootAtStartupActivity";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.d(TAG, "onCreate");
}
}

BroadcastReceiver:

package com.mfcoding.android.bootatstartup;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BootAtStartupReceiver extends BroadcastReceiver {
static final String TAG = "BootAtStartupReceiver";

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        Log.d(TAG, "*** onReceive ACTION_BOOT_COMPLETED");
    }

    Log.d(TAG, "*** onReceive");
}

}

In Logcat, I never see the Log printout for the BroadcastReceiver file. All i see in Logcat is the Activity log printout. Any ideas? I'd like to see in Logcat the Log print statements of the Broadcast Receiver class.

Link to project https://github.com/fangstar/BootAtStartup

Kara
  • 6,115
  • 16
  • 50
  • 57
fangstar
  • 190
  • 2
  • 10
  • 2
    "Whenever I run this in the emulator, it doesn't seem like the code from the BroadcastReceiver code" ...Just making sure that you are aware this Intent is only fired by the system when the DEVICE boots for the first time, not your application. You will need to "reboot" the emulator device after installing to fire `ACTION_BOOT_COMPLETED`. – devunwired Jul 12 '12 at 21:39
  • Aye. Yeah I misunderstood when ACTION_BOOT_COMPLETED actually gets fired. When I fire the ACTION_BOOT_COMPLETED through command line (http://stackoverflow.com/questions/5051687/broadcastreceiver-not-receiving-boot-completed?rq=1) I see that my code is working. thanks for the confirmation! – fangstar Jul 12 '12 at 21:59

3 Answers3

4

An app can only receive this Broadcast Intent after the first device reboot occurring after the app has been installed and executed at least once. Also note that if the app is installed on external storage it may never receive this broadcast because the external storage gets mounted after the Broadcast has been sent.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51
  • The "and executed at least once" is important. It means that you have to create an Activity even if you only want a background service. – Hervé Renault Dec 09 '15 at 08:51
1

try removing this line:

<category android:name="android.intent.category.DEFAULT" />

from your intent filter in the manifest. I've successfully implemented boot listener without this line so I know that it is not needed, however I don't know for sure if it would cause it to not work. Either way it is worth a shot though.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
0

You want to register your broadcast reciever in oncreate() method of your activity.

registerReceiver(receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

receiver - BroadcastReciever

Anandu
  • 145
  • 1
  • 4