2

I need to set Alarm on 9.00 AM, 11 AM and 1PM in everyday.

Simple solution is to use three different pending Intent but Is it any other way to implement same with one pending Intent?

Thanks in Advance!

Mukesh
  • 413
  • 4
  • 19

2 Answers2

1

Thanks, Got solution

Only need to change request code in same Intent. It will not cancel previous alarm. Click Here for solution

Community
  • 1
  • 1
Mukesh
  • 413
  • 4
  • 19
0

If you have a limited number of alarms that at any time need to be scheduled, maybe use one PendingIntent for each alarm can be fine.

However if you have a potentially unlimited (or maybe just a lot) number of alarms to be scheduled, I think that a better approch can be to provide a scheduler that can act over the scheduling information. For example, if you need to create something like a calendar where you can set one or more Event for each day, you should consider this approch.

You can take a look at the Alarm section of tha Android Calendar project

As you can see there is a class named AlarmScheduler in which you have a method

void scheduleNextAlarm(Context context, AlarmManagerInterface alarmManager,
            int batchSize, long currentMillis)

That determines the next alarm that need to be scheduled.

In the specific implementation you can see that the method calls another method:

queryUpcomingEvents()

that operates over a content provider (Calendar provider) and gets the upcoming events.

Each time you trigger a new Alarm you can reschedule a new one, looking in the content provider (or wherever you store your scheduling information), or you can applay different scheduling policiy. For example if an user create a new remider for an Event on the calendar you need to reschedule the alarms (for example you could decide to schedule all the alarms for today, or just the next alarm).

In my applications I usually use the following code (inside the onReceive() of the BroadcastReceiver that is triggered when the alarm fires):

private void restartScheduler(Context context){
        Intent t = new Intent("it.gvillani.socialapp.alerts.SCHEDULE_REQUEST");
        context.sendBroadcast(t);
    }

And then of course I have a BroadcastReceiver that waits for that action and try to reschedule the alarms:

public class SchedulerReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context c, Intent intent) {
        if (intent.getAction().equals("it.gvillani.socialapp.alerts.SCHEDULE_REQUEST")) {
            startScheduler(c);
        }
    }

    private void startScheduler(Context c) {
        AlarmScheduler.clearAlarms(c);
        AlarmScheduler.scheduleNextAlarm(c);
    }
}
GVillani82
  • 16,419
  • 26
  • 96
  • 162