BroadcastReceiver to handle BOOT COMPLETED events
suggest changeThe example below creates a BroadcastReceiver for BOOT_COMPLETED. Use it to start a service or an activity after boot.
You can also restore alarms from BOOT_COMPLETED, because alarms are cleared when the device powers off. From Android 7.0, this broadcast arrives after the user first unlocks the device, unless the app is direct-boot aware.
NOTE: The user needs to have started the application at least once before you can receive the BOOT_COMPLETED action.
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.example" >
...
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<application>
...
<receiver android:name="com.test.example.MyCustomBroadcastReceiver">
<intent-filter>
<!-- REGISTER TO RECEIVE BOOT_COMPLETED EVENTS -->
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
public class MyCustomBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action != null) {
if (action.equals(Intent.ACTION_BOOT_COMPLETED) ) {
// TODO: handle boot. Start a service, show a notification, or start an activity.
}
}
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents