I have two alarm one starting alarm that start a job scheduler that sends notification at regular interval as describe in scheduler and second alarm is for canceling job scheduler, I wanted to preserve state, like if I restart my phone it should not cancel scheduler and sends notification at regular interval as described in scheduler.
For this I registered a class with BOOT_COMPLETE, like this but after reboot I'm not getting notification, not sure if schedule is cancelled or Boot_Complete didn't work, but the end result is I'm not getting notification after reboot
<receiver android:name=".classes.ClsRestartAlarm"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and implemented ClsRestartAlarm like this
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Logging.logMessage("boot completed");
Intent i = new Intent(context, BootService.class);
context.startService(i);
}
}
and in service:
@Override
protected void onHandleIntent(Intent intent) {
AlarmManagerUtils.setStartAlarm();
AlarmManagerUtils.setEndAlarm();
}
I'm using same setStartAlarm and setEndAlarm when I'm first setting alarm before rebooting device the alarms getting fired times from shared preference so I guess there should not be loss of data, the start and end alarm
public static void setStartAlarm(){
AlarmManager alarmManager = (AlarmManager)
MyApplication.getContext().getSystemService(Context.ALARM_SERVICE);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.set(Calendar.HOUR_OF_DAY
,SharedPrefUtils.getStartHour(MyApplication.getContext()));
time.set(Calendar.MINUTE
,SharedPrefUtils.getStartMin(MyApplication.getContext()));
time.set(Calendar.SECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis()
,pendingIntent(MyApplication.getContext()));
}
private static PendingIntent pendingIntent(Context context){
Intent intent = new Intent(context, ClsBroadCastReciver.class);
PendingIntent pendingIntent = PendingIntent
.getBroadcast(context, 01, intent, PendingIntent.FLAG_ONE_SHOT);
return pendingIntent;
}
public static void setEndAlarm(){
AlarmManager alarmManager = (AlarmManager)
MyApplication.getContext().getSystemService(Context.ALARM_SERVICE);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.set(Calendar.HOUR_OF_DAY
,SharedPrefUtils.getEndHour(MyApplication.getContext()));
time.set(Calendar.MINUTE
,SharedPrefUtils.getEndMin(MyApplication.getContext()));
time.set(Calendar.SECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,
time.getTimeInMillis(), endPendingIntent(MyApplication.getContext()));
}
private static PendingIntent endPendingIntent(Context context){
Intent intent = new Intent(context, ClsEndBroadcastReciever.class);
PendingIntent pendingIntent = PendingIntent.
getBroadcast(context, 02, intent, PendingIntent.FLAG_ONE_SHOT);
return pendingIntent;
}
the broadcast in Pending Intent are for scheduling job and canceling schedule respectively, also I have boot permission in manifest
Thanks