I'm created a widget and use AlarmManager to update. In my test, the time to update is 1 second. The code run perfectly. If change code to future also runs, but change date to past, the update stops working. Is there any solution? Thanks
PendingIntent anIntent=PendingIntent.getBroadcast(context, i,
new Intent(WidgetViewsFactory.REFRESH_ACTION),
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmMgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME,
System.currentTimeMillis(), 1000, anIntent);
I finally resolve the problem. The solution is create a BroadcastReceiver to listen time or date changes and generate a new alarm.
public class DateChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
for (int i = 0; i < appWidgetIds.length; i++) {
PendingIntent anIntent = PendingIntent.getBroadcast(context, i,
new Intent(WidgetViewsFactory.REFRESH_ACTION),
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000, anIntent);
}
}
}
and in AndroidManifest.xml add the following lines:
<receiver android:name=".DateChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.TIME_SET"/>
<action android:name="android.intent.action.DATE_SET"/>
</intent-filter>
</receiver>