我创建了一个小部件并使用AlarmManager进行更新。在我的测试中,更新时间是1秒。代码运行完美。如果将来的更改代码也运行,但将日期更改为过去,则更新将停止工作。 有什么解决方案吗? 感谢
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);
答案 0 :(得分:2)
我终于解决了这个问题。解决方案是创建一个BroadcastReceiver来监听时间或日期变化并生成新的警报。
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);
}
}
}
并在AndroidManifest.xml中添加以下行:
<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>