I have a class that extends Application. In the class I make a call to AlarmManager and pass in an intent. As scheduled my EventReceiver class, that extends BroadcastReceiver, processes the call in the onReceive method. How would I call the intent again from the onReceive method to schedule another event?
A:
You can use setRepeating()
instead of set()
, to have it recur automatically. Or, just create another PendingIntent
on an equivalent Intent
(same action, same Uri
, same component, etc.). You do not need the original PendingIntent
object each time.
CommonsWare
2010-04-26 22:59:59
I don't believe I can use set repeating since the times will vary and I need to get the next in my queue.It would be nice if there was a way to use the same intent and schedule events to occur at varying times. For example if I have an array of times that I would like to pass in and not have to chain the events one after another.
2010-04-27 00:59:21
Then use several slightly different `Intents` and schedule them all in a block. By "slightly different", they have to differ by more than the extras, otherwise they will be equivalent from `PendingIntent`'s standpoint. If you are using an `Intent` that specifies the component (i.e., it takes a `Class` as the 2nd parameter to the constructor), then just tuck in a unique action string, and that will keep them distinct yet not interfere with your receipt.
CommonsWare
2010-04-27 01:37:08
A:
final Intent intent = new Intent(context, YourService.class);
final PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pending);
long interval = 30000;//milliseconds
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pending);
More complete sample can be found in Photostream sample application http://code.google.com/p/apps-for-android/.
Fedor
2010-04-27 00:17:54