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.
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
Android Saving Created Bitmap to Directory on Sd Card
How to Make an Application Ignore Screen Orientation Change
Timepicker in Preferencescreen
Google Cloud Endpoints and User's Authentication
Programmatically Update Widget from Activity/Service/Receiver
Android Dependency Has Different Version for the Compile and Runtime
Android Image View Pinch Zooming
Passing Data of a Non-Primitive Type Between Activities in Android
Full Webpage and Disabled Zoom Viewport Meta Tag for All Mobile Browsers
Implementation of Onscrolllistener to Detect the End of Scrolling in a Listview
How to Create a Simple Divider in the New Navigationview
How to Find Older Versions of Eclipse Adt
How to Display Custom View in Actionbar
"Unable to Locate Adb" Using Android Studio
How to Enable Logging for Apache Commons Httpclient on Android
Android Studio - Unable to Merge from Svn Branch
Version Conflict Updating to Play-Services 9.4.0 Android Studio 2.2