Broadcast Receiver not receiving ACTION_BOOT_COMPL

2019-07-20 16:06发布

问题:

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

回答1:

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.



回答2:

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.



回答3:

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

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

receiver - BroadcastReciever