Android Set Multiple Alarms

I am trying to implement an Android application that needs to be notified (or warned) several times over time.

I already searched, but the closest I found a fixed number of installed alarms, and I think the example does not work.

What I want to know is there any approach to dynamically dialing multiple alarms, such as an array of alarms, and then starting these alarms at their specific timestamps.

+33
android android-alarms
Oct 08
source share
3 answers

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

Here is the code for creating several single alarms and saving them in an ArrayList . I store the PendingIntent in an array, because this is what you need to cancel the 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 multiple alerts at once in android? .

+80
09 Oct
source share

You can set the alarm to repeat:

in this case:

 public void AddAlarm(int requestCode,MutableDateTime dueDate,int repeat) { Intent intent = new Intent(context, AlarmReceiver.class); intent.putExtra(Constants.RECORD_ID, requestCode); intent.putExtra("REPEAT", repeat); PendingIntent operation = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_ONE_SHOT ); MutableDateTime due = dueDate.toMutableDateTime(); switch(repeat){ case NO_REPEAT: due.addMinutes(0); break; case DAILY: due.addDays(1); break; case WEEKLY: due.addWeeks(1); break; case MONTHLY: due.addMonths(1); break; case MONTHLY_2: due.addWeeks(5); break; case YEARLY: due.addYears(1); break; } due.add(-(dueDate.getMillis())); due.setSecondOfMinute(0); dueDate.setSecondOfMinute(0); alarm.cancel(operation); alarm.set(AlarmManager.RTC_WAKEUP, dueDate.getMillis(), operation); alarm.setRepeating(AlarmManager.RTC_WAKEUP, dueDate.getMillis(), due.getMillis(), operation); } 
+5
Oct 08
source share

To dynamically configure multiple alarms, the approach I used was that I created one alarm. Then, in my alarm setup class, the static integer (which will be used as the request code) is initialized, which will increment each time from my main action whenever I click the "add alarm" button in my main action. For example.

MainActivity.java

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void addAlarmClick(View v) { AlarmActivity.broadcastCode++; startActivity(new Intent(this, AlarmActivity.class)); } } 

AlarmActivity.java

 public class AlarmActivity extends AppCompatActivity {` public static int broadcastCode=0; /*some code here*/ Intent myIntent = new Intent(AlarmActivity.this, AlarmReceiver.class); pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, broadcastCode, myIntent, 0); 

Hope this helps.

0
Aug 04 '15 at 9:11
source share



All Articles