0

I want my app to start at 9am everyday. for that I start Alarm_1 using setRepeatingAlarm() that starts at 9am everyday.

manager.setRepeating(AlarmManager.RTC_WAKEUP,
    timeOn.getTimeInMillis(), 86400000,startingIntent);

From alarm_1, I start another alarm, MainAlarm, that repeatedly starts my service at the interval of 1min.

manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    SystemClock.elapsedRealtime(), duration, MainActivity.mainIntent);

I want my app to stop at 9pm everyday.

For that I start Alarm_2 using setRepeatingAlarm() that cancel() MainAlarm everyday at 9pm.

But this is not working as expected.

What should be done? Is there any problem because I am using 3 alarms?

The alarms do not work the next day.

     This is my cancel() code:
          public class AlarmReceiver2 extends BroadcastReceiver {
          @Override
          public void onReceive(Context context, Intent intent) {
         AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
         manager.cancel(MainActivity.mainIntent);
      MainActivity.mainIntent.cancel();
          }
        }   
user2310041
  • 101
  • 1
  • 2
  • 10

1 Answers1

0

Firstly, I don't think there is a problem with having 3 alarms.

Secondly, perhaps you can add the code that you use to try and cancel with. A trick here is that you need your PendingIntent to be identical to the one you used to start the alarm - that's why it will be useful to see the cancel code.


Have a look at my answer here on a related question:


The question is similar enough that I don't feel I should copy the whole answer across, but here is a summary which links to a @commonsware answer:


Below answer copied from How to cancel this repeating alarm?

Call cancel() on AlarmManager with an equivalent PendingIntent to the one you used with setRepeating():

Intent intent = new Intent(this, AlarmReceive.class);
PendingIntent sender = PendingIntent.getBroadcast(this,
               0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.cancel(sender);
Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255