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 alarmmanager multiple alarms, one overwrites the other?
PendingIntent pendingIntent1 = PendingIntent.getActivity(getApplicationContext(), 0, intent1, PendingIntent.FLAG_ONE_SHOT);
change the 0 attribute to an id for your alarm, for example you have three alarms,
repeat the above code with 0,1,2.
this way they won't override each other.
Multiple Alarms using BroadcastReceiver and AlarmManager
You can put an "extra" in the Intent
that identifies which alarm it is. For example:
_myIntent.putExtra("alarm", "A");
Do this before calling PendingIntent.getBroadcast()
for each different alarm.
Then, in your BroadcastReceiver.onReceive()
you can check the "extra" in the Intent
to determine which alarm got triggered.
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 in android,last alarm overwrites preveious alarms in this case
You need to use unique id´s for your PendingIntent
:
pint=PendingIntent.getBroadcast(this,id,alarmintent,0);
So be sure that the second paramter of PendingIntent
, the requestCode
, is used only once. Also, you should implement a logic, that you can cancel the alarm every time. For canceling, you have to use the same id.
Related Topics
How to Get Access Token After User Is Signed in from Gmail in Android
Android Java Lang Runtimeexception Fail to Connect to Camera Service
Current Month Facebook Friends Birthdays in Android
Android: What Is Android.R.Id.Content Used For
Sqlite Android Database Cursor Window Allocation of 2048 Kb Failed
Android: Scrollview Force to Bottom
Android: How to Get Value of an Attribute in Code
How to Restrict the Edittext to Accept Only Alphanumeric Characters
Pass Arraylist from One Activity to Other
Actionbarsherlock + Tabs + Multi Fragments
Androidstudio Cannot Find Layout
Best Practice for Defining Button Events in Android