我正在按照这个教程创建一个定制的Android手表表盘。我已经实现了广播接收器来检测时间变化,代码如下:
在我的Activity中,我有一个静态块来过滤以下意图:
在我的Activity中,我有一个静态块来过滤以下意图:
static {
intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
intentFilter.addAction(Intent.ACTION_TIME_TICK);
intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
}
我的接收器类:
public class MyReciever extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
c = Calendar.getInstance();
Log.d("myapp", "time changed");
hrs = c.get(Calendar.HOUR_OF_DAY);
min = c.get(Calendar.MINUTE);
sec = c.get(Calendar.SECOND);
txt_hrs.setText(String.valueOf(hrs));
txt_mins.setText(String.valueOf(min));
txt_sec.setText(String.valueOf(sec));
}
}
我在oncreate()中注册了接收器:
MyReciever myReciever = new MyReciever();
registerReceiver(myReciever,intentFilter);
以上代码适用于小时和分钟,但不适用于秒。
Intent.ACTION_TIME_TICK
的问题在于它每分钟广播一次,而不是每秒。
我需要每秒检测时间的变化以更新手表表盘上的时钟。有没有人有“每秒检测时间变化”的解决方案?