Android AlarmManager - Scheduling recurring intent to fire twice a day

After reading a lot of code examples in this question, I'm trying to figure out the simplest way to achieve the following:

I want to be able to schedule an intent that will ring back to my Alarm BroadcastReceiver, which in turn disables my service. However, I want to configure so that it will call the specified Intent twice a day and only schedule alarms if they have not already been set (similar for canceling alarms).

However, I'm not sure if the following code is the correct way to set and clear 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();

      

+1


source to share


1 answer


This seems fine to me, however, instead of creating two calendar objects, you can simply set your interval

AlarmManager.INTERVAL_DAY/2

      

unless your intentions do different things.

Besides,

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

      



should be sufficient to cancel all alarms of these types, it is not necessary:

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 modifies a specific variable of that calendar object. So nowadays, if it was 8pm, that day, he set the alarm at 12 and 7, which would be useless. So if you want to install it on the next day, you will need to use cal1.add (Calendar.DAY_OF_MONTH, 01); to add an extra day, set for that time the next day. Hope this helps.

+1


source







All Articles