Question

My MainActivity's OnCreate method invokes scheduleAlarm method which I implemented in another class called PollReceiver. It has below code in it:

    Intent iWeekly = new Intent(context, ScheduledWeeklyService.class);
    PendingIntent piWeekly = PendingIntent.getBroadcast(context, 0, iWeekly, 0);

    AlarmManager alarmMgrWeekly = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmMgrWeekly.setRepeating(AlarmManager.RTC_WAKEUP, scheduleTime, Constants.PERIOD_WEEKLY, piWeekly);

My app also has a togglebutton to enable or disable the alarms.

The togglebutton code is shown below:

    public void enableDisableScheduler(View v){
        if (btnEnableDisableScheduler.isChecked()) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("toggleButton", tb.isChecked()); 
            editor.commit();        

            // Enable all alarms
            PollReceiver.scheduleAlarms(this);

            Log.i(TAG, "alarm is turned on");
        } else {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("toggleButton", tb.isChecked()); 
            editor.commit();        

            // Cancel all alarms
            Intent iWeekly = new Intent(context, ScheduledWeeklyService.class);
            PendingIntent piWeekly = PendingIntent.getBroadcast(context, 0, iWeekly, 0);

            AlarmManager alarmMgrWeekly = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            try {
                alarmMgrWeekly.cancel(piWeekly);
            } catch (Exception e) {
                Log.e(TAG, "alarm is not cancelled");
            }

            Log.i(TAG, "alarm is turned off");
        }
    }   

Now, everything works fine. So, when I exit the app & re-open it again, the above code gets fired again & it re-schedules the alarms which works great as well.

I was trying to avoid this re-scheduling of alarms by putting below code but it does not work. Any idea or help is greatly appreciated. If I turn off the toggle button & turn it on the alarms are not getting scheduled since somehow it doesn't satisfy below condition & does not satisfy below if condition. Any clue or idea how to do this?

boolean weeklyAlarmUp = (PendingIntent.getBroadcast(context, 0, new Intent(context, ScheduledWeeklyService.class), PendingIntent.FLAG_NO_CREATE) != null);

if (!weeklyAlarmUp) {
    Intent iWeekly = new Intent(context, ScheduledWeeklyService.class);
    PendingIntent piWeekly = PendingIntent.getBroadcast(context, 0, iWeekly, 0);

    AlarmManager alarmMgrWeekly = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmMgrWeekly.setRepeating(AlarmManager.RTC_WAKEUP, scheduleTime, Constants.PERIOD_WEEKLY, piWeekly);
}
Was it helpful?

Solution

This code to check if the alarm has been scheduled:

boolean weeklyAlarmUp = (PendingIntent.getBroadcast(context, 0, new Intent(context,
           ScheduledWeeklyService.class), PendingIntent.FLAG_NO_CREATE) != null);

doesn't work because you are relying on the existing of the PendingIntent. When you cancel the alarm you aren't canceling the PendingIntent so it still exists, even though the alarm has been canceled.

To fix that, make sure you also cancel the PendingIntent when you cancel the alarm, like this:

// cancel alarm
alarmMgrWeekly.cancel(piWeekly);
// Cancel PendingIntent
piWeekly.cancel();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top