0

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

blackHawk
  • 6,047
  • 13
  • 57
  • 100

1 Answers1

0

Try add permission to your manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Fori
  • 475
  • 5
  • 15
  • So, do you have "boot completed" in your log? You can test receiver using shell: `adb shell am broadcast -a android.intent.action.BOOT_COMPLETED` – Fori Jul 20 '17 at 21:20
  • What do you mean im testing like this i restart phone and then wait for notification, i cant see log because if phone restart while attach to adb, it detaches phone and i cant no longer see logcat – blackHawk Jul 21 '17 at 03:38