Create a BroadcastReceiver variable in your activity
private BroadcastReceiver mBootCompletedReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) {
and onCreate or onResume Activity events, you must register for this BroadcastReceiver
super.registerReceiver(mBootCompletedReceiver, new IntentFilter("android.intent.action.BOOT_COMPLETED"));
and onDestory or onStop or onPause, regardless of whether you have to unregister this BroadcastReceiver in a situation so as not to receive these updates anymore.
super.unregisterReceiver(mBootCompletedReceiver);
source share