Skip to content

Instantly share code, notes, and snippets.

@richardleggett
Last active June 26, 2021 13:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardleggett/6bb4cf5377944c649bd0 to your computer and use it in GitHub Desktop.
Save richardleggett/6bb4cf5377944c649bd0 to your computer and use it in GitHub Desktop.
BOOT_COMPLETED
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapphere">
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="9" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar"
android:name=".MyApp"
>
<activity
android:name=".activity.LoginActivity"
android:label="@string/app_name"
android:screenOrientation="sensorPortait"
android:windowSoftInputMode="stateAlwaysVisible|adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.MainActivity"
android:label="@string/app_name"
android:screenOrientation="sensorPortait"
android:windowSoftInputMode="adjustResize">
</activity>
<receiver
android:name=".receiver.BootCompletedReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
package com.myapphere;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootCompletedReceiver extends BroadcastReceiver {
private static final String TAG = BootCompletedReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive()");
Intent intent = new Intent(context, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
To launch an activity from a BroadcastReceiver you have to launch with NEW_TASK (exceptions apply). With that in mind you may want to look at the various launch mode options for the Activity in the AndroidManifest.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment