Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Set Multiple Alarms

I'm trying to implement an Android app that needs to alarm (or to alert) multiple times along the time.

I've already searched, but the nearest I found was a fixed-number of alarms set, and I guess the example didn't work.

What I want to know if there is exists an approach to dynamically set multiple alarms, like an Array of alarms and then to trigger those alarms in their specific timestamps.

like image 470
Sammy Avatar asked Oct 08 '12 16:10

Sammy


People also ask

Can I set multiple alarms at once?

The impact of your morning alarms can be far-reaching, with the general consensus being it's not a good idea to set multiple alarms. How many alarms should you set? The answer is just one, because setting multiple alarms to wake up may actually be harmful to your health.

Is there an alarm clock that can set multiple alarms?

The Jall Wooden Digital Alarm Clock looks great and has everything you need in an alarm clock. It's simple to set, read and use, and can wake you dependably with multiple alarms.

Can I set two alarms on my phone?

For Android devices, the built-in Clock app can schedule one-time alarms and weekly repeating alarms. It's possible to create multiple alarms and turn them on or off individually.


1 Answers

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?.

like image 166
Nikolai Samteladze Avatar answered Sep 28 '22 01:09

Nikolai Samteladze