I'm trying to add a BroadcastReceiver
to my program that will launch my MainActivity
when the BOOT_COMPLETED
action is detected.
I've tried everything here and here and it refuses to work. I am running Android 5.1.1, API 22.
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- Declaring broadcast receiver for BOOT_COMPLETED event. -->
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
In the <intent-filter>
, I've tried using both BOOT_COMPLETED
and QUICKBOOT_POWERON
independently as well as in conjunction (as pictured) with no change in result.
BootReceiver.java
public class BootReceiver extends BroadcastReceiver {
private final static String TAG = BootReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "BootReceiver has been called - attempting to launch MainActivity");
Intent startMainActivityIntent = new Intent(context, MainActivity.class);
startMainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startMainActivityIntent);
}
}
EDIT: I was removing the app from multi-tasking before restarting the phone. If I start the activity and then restart the phone while it's running, it seems to still be there upon rebooting. However, attempting to open it results in a crash.