I was making a digital clock widget using alarmManager, which updates every 1 minute. Here is the code for creating alarmManager. (this was taken from This StackOverFlow post ).
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Log.d("onEnabled","Widget Provider enabled. Starting timer to update widget every minute");
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 60000, createClockTickIntent(context));
}
private PendingIntent createClockTickIntent(Context context) {
Intent intent = new Intent(CLOCK_WIDGET_UPDATE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
The code works absolutely fine and my widget is updating every minute as expected. Problem is, lets say user adds the widget at 3:17:40 seconds, my widget shows the time as 3:17. The Alarmmanager is told to update the widget 60 seconds later, i.e. at 3:18:40. so from 3:18:00 to 3:18:39, my widget shows 3:17, instead of 3:18
So there is always a offset lag by few seconds (the time when widget was added, minus, the time when actual minute changed in system clock). How can I remove this lag? it looks really bad when the time in notification bar changes but my widget does not.
Invoking alarmManager every second will solve this issue but it's really bad approach. Is there any way by which I can set the "SetRepeating" when system clock changes a minute?