【发布时间】:2015-03-27 10:48:03
【问题描述】:
我正在使用alarmManager 制作一个数字时钟小部件,它每1 分钟更新一次。这是创建alarmManager的代码。 (取自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;
}
代码运行良好,我的小部件按预期每分钟更新一次。问题是,假设用户在 3:17:40 秒添加小部件,我的小部件将时间显示为 3:17。警报管理器被告知在 60 秒后更新小部件,即在 3:18:40。所以从 3:18:00 到 3:18:39,我的小部件显示 3:17,而不是 3:18
所以总是会有几秒的偏移延迟(添加小部件的时间,减去系统时钟中实际分钟更改的时间)。我怎样才能消除这种滞后?当通知栏中的时间发生变化但我的小部件没有变化时,它看起来真的很糟糕。
每秒调用一次 alarmManager 可以解决这个问题,但这是一种非常糟糕的方法。当系统时钟改变一分钟时,有什么方法可以设置“SetRepeating”?
【问题讨论】:
标签: android