Android Set Multiple Alarms
If you want to set multiple alarms (repeating or single), then you just need to create their PendingIntent
s 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.
How can I setup multiple alarms in Android?
Ok, when you set an PendingIntent, you're supposed to assign it a unique ID to it, incase you want to identify it later (for modifying/canceling it)
static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags)
//Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent).
static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags)
//Retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast().
The Request code is that ID.
In your code, you keep resetting the SAME PendingIntent, instead use a different RequestCode each time.
PendingIntent pIntent = PendingIntent.getActivity(context,uniqueRQCODE, newIntent, 0);
It has to be an integer, i suppose you have a primaryid (itemId) that can identify Alarm A from Alarm B.
Related Topics
Sethinttextcolor() in Edittext
Set Visibility of Progress Bar Gone on Completion of Image Loading Using Glide Library
How to Click a Button Programmatically for a Predefined Intent
How to Place App Icon on Launcher Home Screen
Android Material Design Inline Datepicker Issue
How to Set Status Bar Background as Gradient Color or a Drawable in Android
Failed to Find Build Tools Revision 21.1.1 - Sdk Up to Date
How to Get My Android Device Internal Download Folder Path
How to Kill All Running Applications in Android
How to Get a View from an Event Coordinates in Android
Add Viewpagerindicator to Android Studio
How to Restart Service After The App Is Killed from Recent Tasks
Successful Share Intent for Android
How to Set Android Tablayout in The Bottom of The Screen
Android: Converting Color Image to Grayscale
How to Pick Image for Crop from Camera or Gallery in Android 7.0