views:

208

answers:

2

I am following sample code for sending an update notification every 10'seconds. The code follows and it is in an "UpdateService" for an AppWidgetProvider. If I put a Thread.sleep(10*1000); I can see the expected behavior of my servicing loop. I obviously have something fundamentally wrong that is triggering immediately. It is supposed to be a PendingIntent of an alarm that will broadcast update to my listener.

long nextUpdate = 10*1000;
Log.d(TAG, "Requesting next update in " + nextUpdate + " msec." );

Intent updateIntent = new Intent(ACTION_UPDATE_ALL);
updateIntent.setClass(this, UpdateService.class);

PendingIntent pendingIntent = PendingIntent.getService(this, 0, updateIntent, 0);

// Schedule alarm, and force the device awake for this update
AlarmManager alarmManager = (AlarmManager)getBaseContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 
    nextUpdate, pendingIntent);
+2  A: 

AlarmManager.setRepeating is defined as public void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation) The 2nd argument is when it should be first called. You're telling it to start at SystemClock.elapsedRealtime(), which is now.

Falmarri
Thanks to both +1 from me for the quick and accurate answer. On to the next problem. Quite ironically, I tried to put a second comment too soon (<15 seconds), so I get a timer-pop notification from SO. :))
mobibob
+2  A: 

You are telling setRepeating() that you want the first alarm to go off immediately (SystemClock.elapsedRealtime()). If you want the first alarm to go off some other time, add an offset (SystemClock.elapsedRealtime()+nextUpdate).

CommonsWare
+1 for having the same answer as me =]
Falmarri
Thanks to both +1 from me for the quick and accurate answer. On to the next problem.
mobibob