【问题标题】:using AlarmManager causing appWidget clock offset delay使用 AlarmManager 导致 appWidget 时钟偏移延迟
【发布时间】: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


    【解决方案1】:

    如何消除这种滞后?

    在您的代码中,您告诉AlarmManager现在每 60 秒让您控制一次。如果你想让AlarmManager下一分钟开始每60秒让你控制一次,你需要使用System.currentTimeMillis()以外的东西,比如使用java.util.Calendar来确定什么时候开始下一分钟是。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-06
      • 1970-01-01
      • 2018-01-23
      • 2017-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多