AlarmManager-
This class provides access to the system alarm services. These allow you
to schedule your application to be run at some point in the future. When
an alarm goes off, the
The Alarm Manager holds a CPU wake lock as long as the alarm receiver's
onReceive() method is executing. This guarantees that the phone will not sleep
until you have finished handling the broadcast. Once onReceive() returns, the
Alarm Manager releases this wake lock. This means that the phone will in some
cases sleep as soon as your onReceive() method completes. If your alarm receiver
called Intent that had been registered for it
is broadcast by the system, automatically starting the target application
if it is not already running. Registered alarms are retained while the
device is asleep (and can optionally wake the device up if they go off
during that time), but will be cleared if it is turned off and rebooted.Context.startService() , it
is possible that the phone will sleep before the requested service is launched.
To prevent this, your BroadcastReceiver and Service will need to implement a
separate wake lock policy to ensure that the phone continues running until the
service becomes available.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 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); } And to cancel alarms : alarmmanager.cancel(intentArray.get(i)); //where i is position of alarm you want to cancel. Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler. |
Monday, December 21, 2015
Setting multiple alarms in android (Alarm Manager)
Subscribe to:
Post Comments (Atom)
Don't lose faith when you see others receive answers to their prayers
An elephant and a dog became pregnant at same time. Three months down the line the dog gave birth to six puppies. Six months later the dog...
WakingNews Alarm Clock app
ReplyDeleteWakingNews Alarm Clock is yet another beta app release with some potential. It functions primarily as an alarm clock. You set an alarm as usual and it goes off on time as usual. However, this one reads you the news from a variety of sources when it goes off. So it functions a little bit like old school radio alarm clocks. It has several good news sources, including Yahoo Finances, Yahoo Sports, Engadget, etc. However, there are some lesser sources of words there as well. Thankfully, you can choose the sources that play when the alarm goes off. It's in beta so there are definitely bugs. It is also free and has potential.
WakingNews Alarm Clock app
ReplyDeleteWakingNews Alarm Clock is yet another beta app release with some potential. It functions primarily as an alarm clock. You set an alarm as usual and it goes off on time as usual. However, this one reads you the news from a variety of sources when it goes off. So it functions a little bit like old school radio alarm clocks. It has several good news sources, including Yahoo Finances, Yahoo Sports, Engadget, etc. However, there are some lesser sources of words there as well. Thankfully, you can choose the sources that play when the alarm goes off. It's in beta so there are definitely bugs. It is also free and has potential.
WakingNews Alarm Clock app
ReplyDeleteWakingNews Alarm Clock is yet another beta app release with some potential. It functions primarily as an alarm clock. You set an alarm as usual and it goes off on time as usual. However, this one reads you the news from a variety of sources when it goes off. So it functions a little bit like old school radio alarm clocks. It has several good news sources, including Yahoo Finances, Yahoo Sports, Engadget, etc. However, there are some lesser sources of words there as well. Thankfully, you can choose the sources that play when the alarm goes off. It's in beta so there are definitely bugs. It is also free and has potential.