views:

112

answers:

2

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