I capture the BOOT_COMPLETED event after a device is re-booted. I have a BroadcastReceiver that sends an Extra "bootCompleted" message to a JobIntentService. I fire a Toast if the message is successfully received. The Toast does fire and then disappear from the UI but then after about 6 or 7 seconds, the Toast fires again. Why would the Toast fire again as it appears like the JobIntentService is receiving the "bootCompleted" TAG again from the BroadcastReceiver?
public class RebootReceiver extends BroadcastReceiver {
private static final String LOCKED_BOOT_COMPLETED = "android.intent.action.LOCKED_BOOT_COMPLETED";
private static final String ACTION_BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";
private static final String QUICKBOOT_POWERON = "android.intent.action.QUICKBOOT_POWERON";
private static final String HTC_QUICKBOOT = "com.htc.intent.action.QUICKBOOT_POWERON";
private static final String TIME_SET = "android.intent.action.TIME_SET";
private static final String TIMEZONE_CHANGED = "android.intent.action.TIMEZONE_CHANGED";
private static final String LOCALE_CHANGED = "android.intent.action.LOCALE_CHANGED";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == null) {
return;
}
else {
int SDK_INT = Build.VERSION.SDK_INT;
if (SDK_INT >= Build.VERSION_CODES.N && (action.equals(LOCKED_BOOT_COMPLETED)||
action.equals(TIME_SET)||action.equals(TIMEZONE_CHANGED)||action.equals(LOCALE_CHANGED))){
intent.putExtra("TAG","bootCompleted");
}
else if (SDK_INT < Build.VERSION_CODES.N && (action.equals(ACTION_BOOT_COMPLETED) ||
action.equals(QUICKBOOT_POWERON)||action.equals(HTC_QUICKBOOT)||
action.equals(TIME_SET)||action.equals(TIMEZONE_CHANGED)||action.equals(LOCALE_CHANGED))){
intent.putExtra("TAG","bootCompleted");
}
}
// enqueue the job to the JobIntentService class
RebootService.enqueueWork(context,intent);
}
}
public class RebootService extends JobIntentService {
static final int JOB_ID = 10000;
public static void enqueueWork(Context context,Intent work) {
enqueueWork(context,RebootService.class,JOB_ID,work);
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String classname = extras.getString("TAG");
if (classname != null && classname.equals("bootCompleted")) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(() -> Toast.makeText(RebootService.this, "RebootReceiver fired this message in RebootService", Toast.LENGTH_LONG).show());
...
Manifest
...
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK"
android:maxSdkVersion="25" />
<receiver
android:name=".RebootReceiver"
android:directBootAware="true" >
<intent-filter>
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.LOCALE_CHANGED" />
</intent-filter>