1

After reading lots of sample code into this matter, I'm trying to figure out the simplest way to achieve the following:

I want to be able to schedule an Intent that calls back to my Alarm BroadcastReceiver, which in turn fires off my Service. However, I want to set up so that it calls said Intent twice a day and to only schedule the alarms if they haven't already been set (likewise for canceling the alarms).

However, I am unsure if the following code is the correct way to set and cancel alarms.

//Static function for setting the alarm
//My midday calendar object (cal1)

...

//My evening calendar object (cal2)
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE)
Intent myIntent = new Intent(context, MyAlarmReceiver.class);

    PendingIntent firstCallIntent = PendingIntent.getBroadcast(context, FIRST_CALL_ID, myIntent, PendingIntent.FLAG_NO_CREATE);
    PendingIntent secondCallIntent= PendingIntent.getBroadcast(context, SECOND_CALL_ID, myIntent, PendingIntent.FLAG_NO_CREATE);
    if(firstCallIntent == null){
        if(DEBUG){
            Log.d(TAG, "Setting Midday Alarm");
        }
        firstCallIntent = PendingIntent.getBroadcast(context, FIRST_CALL_ID, myIntent, 0);
        alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), AlarmManager.INTERVAL_DAY, firstCallIntent);
    }
    if(secondCallIntent == null){
        if(DEBUG){
            Log.d(TAG, "Setting Evening Alarm");
        }
        secondCallIntent = PendingIntent.getBroadcast(context, SECOND_CALL_ID, myIntent, 0);
        alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, secondCallIntent);
    }


//Static call to cancel the alarm.
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE)
Intent myIntent = new Intent(context, MyAlarmReceiver.class);
PendingIntent firstCallIntent = PendingIntent.getBroadcast(context, FIRST_CALL_ID, myIntent, 0);
alarms.cancel(firstCallIntent);
firstCallIntent.cancel();
PendingIntent secondCallIntent = PendingIntent.getBroadcast(context, SECOND_CALL_ID, myIntent, 0);
alarms.cancel(secondCallIntent);
secondCallIntent.cancel();
Diego Tori
  • 399
  • 5
  • 19

1 Answers1

2

That seems alright to me, however instead of creating two calendar objects you could just set your interval to

AlarmManager.INTERVAL_DAY/2

unless your intents are doing different things.

Also,

alarms.cancel(firstCallIntent);
alarms.cancel(secondCallIntent);

should be enough to cancel all alarms of those types, no need for:

firstCallIntent.cancel();

Edit: Setting 2 calendar objects

//midday
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.HOUR_OF_DAY, 12);
cal1.set(Calendar.MINUTE, 00);
cal1.set(Calendar.SECOND, 00);

//7pm
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.HOUR_OF_DAY, 19);
cal2.set(Calendar.MINUTE, 00);
cal2.set(Calendar.SECOND, 00);

Calendar.getInstance() will return a calendar object and set it to the current system time. Each .set method changes a certain variable of that calendar object. So currently if it was 8pm, it would set the alarm for 12 and 7 that day, which would be no use. So if you want to set it for the next day, you'll need to use cal1.add(Calendar.DAY_OF_MONTH, 01); to add an extra day, setting for that time the next day. Hope this helps.

Matt Harris
  • 3,496
  • 5
  • 32
  • 43
  • Well, according to this post (http://stackoverflow.com/questions/4963271/android-alarmmanager-problem-with-setting-resetting-an-alarm), there's an issue if you don't cancel the PI after canceling the alarm, seeing that the code isn't passing FLAG_CANCEL_CURRENT when getting the PI. In a nutshell, to prevent task killers from nuking my alarms, my app either sets(re-sets) or cancels the alarm when we start up the app. Ditto for boot since I already have a Boot BroadcastReceiver taking care of it as well. – Diego Tori Jan 06 '12 at 21:17
  • I'm about to test it in a little bit. Hopefully it should work. Also, if I set the interval to half a day, wouldn't that just trigger the alert every 12 hours? What I want is for it to trigger the first alarm a little after noon and the second alarm at little after 7PM. – Diego Tori Jan 06 '12 at 22:12
  • Ah, yes. I thought you wanted it at even intervals of half a day, sorry. Good luck! If you encounter problems, edit your post and comment here, its hard for me to say whether it will work or not just by looking at the code. – Matt Harris Jan 06 '12 at 22:23
  • After setting the two alarms, when i did "dumpsys alarm", for both of those set alarms, they were slated to go off at the same time, which doesn't sound right. Is there any chance you can give me an example of how to set two distinct Calendar times, from the times I specified? – Diego Tori Jan 06 '12 at 23:01
  • So from there, it's just a matter of calling getTimeInMillis for each calendar object when setting the alarm, right? – Diego Tori Jan 06 '12 at 23:24
  • After setting shorter intervals for testing, it did fire off my Alarm without a hitch. I am fine with setInexactRepeating's behavior where it would wait almost a whole interval (in my case, one whole day) after the specified time to fire off the first request. – Diego Tori Jan 07 '12 at 05:26
  • There's actually a constant called INTERVAL_HALF_DAY, no need to divide – Mauker Apr 01 '20 at 01:03