如何在Android中将时钟应用小部件与系统时钟完美同步? [英] How to perfectly sync clock app widget with system clock in Android?

查看:149
本文介绍了如何在Android中将时钟应用小部件与系统时钟完美同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个数字时钟小部件,该小部件使用 AlarmManager 每60秒更新一次时间.

I made a digital clock widget which is using AlarmManager to update the time every 60 seconds.

@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));
}

问题是,每60秒轮询一次系统时间不会使我的窗口小部件时间与系统时间同步.假设用户在6:00:20添加了窗口小部件,我的窗口小部件显示了6:00时间并休眠了60秒,因此,当系统时间变为6:01:00时,我的窗口小部件还需要20秒才能赶上.

The problem is, polling the system time every 60 seconds does not sync my widget time with system time. lets say user adds widget at 6:00:20, my widget shows the time 6:00 and sleeps for 60 seconds, so when system time becomes 6:01:00, my widget needs another 20 seconds to catch up.

是否可以执行以下操作?

Is there a way to do the following ?

  • 步骤1 :获取系统时间并显示在小部件上.

  • step 1: get system time and show on widget.

步骤2 :设置第一个更新间隔=(60-当前秒值).

step 2: set the first update interval = (60 - current Seconds value).

步骤3 :首次更新后,将后续更新间隔设置为60秒.

step 3: after first update, set the subsequent update intervals to 60 seconds.

推荐答案

拦截

Intercept the ACTION_TIME_TICK broadcast. This broadcast action is sent on every minute change of the local time zone clock.

private BroadcastReceiver receiver;

@Override
public void onEnabled(Context context)
{
    super.onEnabled();
    receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context ctx, Intent intent)
        {
            if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0) {
                // update widget time here using System.currentTimeMillis()
            }
        }
    };

    context.getApplicationContext().registerReceiver(receiver, new IntentFilter(Intent.ACTION_TIME_TICK));
}

@Override
public void onDisabled(Context context)
{
    super.onDisabled();
    if (receiver != null)
        context.getApplicationContext().unregisterReceiver(receiver);
}

其他注意事项:

  • 我们本来可以尝试在 AppWidgetProvider 类的 onReceive()方法中捕获 ACTION_TIME_TICK 广播(在将操作添加到意图过滤器"),但文档指出,此广播操作只能被以编程方式注册的 BroadcastReceiver 拦截.不过,这可能值得一试.
  • 使用 BroadcastReceiver 并不是比使用 AlarmManager 更好.在不同情况下需要使用不同的方法,并且由于 ACTION_TIME_TICK 广播是由系统发送的,为什么不利用它?
  • We could have tried catching the ACTION_TIME_TICK broadcast in the AppWidgetProvider class' onReceive() method (after adding the action to the intent-filter in the manifest) but the documentation states that this broadcast action can only be intercepted by a BroadcastReceiver registered programmatically. Still, this may be worth a try.
  • It is not that using a BroadcastReceiver is "better" than using the AlarmManager. Different approaches are required in different situations, and since the ACTION_TIME_TICK broadcast is sent by the system, why not exploit that ?

这篇关于如何在Android中将时钟应用小部件与系统时钟完美同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆