Android Set Multiple Alarms

Android Set Multiple Alarms

If you want to set multiple alarms (repeating or single), then you just need to create their PendingIntents with different requestCode. If requestCode is the same, then the new alarm will overwrite the old one.

Here is the code to create multiple single alarms and keep them in ArrayList. I keep PendingIntent's in the array because that's what you need to cancel your alarm.

// context variable contains your `Context`
AlarmManager mgrAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();

for(i = 0; i < 10; ++i)
{
Intent intent = new Intent(context, OnAlarmReceiver.class);
// Loop counter `i` is used as a `requestCode`
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, intent, 0);
// Single alarms in 1, 2, ..., 10 minutes (in `i` minutes)
mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 60000 * i,
pendingIntent);

intentArray.add(pendingIntent);
}

Also, see this question: How to set more than one alarms at a time in android?.

How to set multiple alarms using alarm manager in android

You need to use different Broadcast id's for the pending intents. Something like
this:

Intent intent = new Intent(load.this, AlarmReceiver.class);
final int id = (int) System.currentTimeMillis();
PendingIntent appIntent = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_ONE_SHOT);

Using the system time should be a unique identifier for every pending
intent you fire.

Android: How to create multiple alarms and cancel them after select

Too much of a code,your code seems to work fine

To cancel all alarams use must cancel them one by one and would need all request codes

private void cancelAlarm(int RSQ) {

textAlarmPrompt.setText(
"\n\n***\n"
+ "Alarm Cancelled! \n"
+ "***\n");

Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RSQ, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
}

And then use to cancel each one individually

cancelAlarm(1);
cancelAlarm(2);
cancelAlarm(3);
cancelAlarm(4);

To cancel all

for(int i=1;i<5;i++)
{
cancelAlarm(i);
}

Android Multiple Alarm With Different Notification

Use AlarmManager to send a broadcast using a pending intent with the information of the movie in the extras and then use a BroadcastReceiver to build and display the notification.

This looks like a good example:
https://gist.github.com/BrandonSmith/6679223

check:
https://developer.android.com/reference/android/app/AlarmManager.html

https://developer.android.com/guide/components/broadcasts



Related Topics



Leave a reply



Submit