查找刷新时间的博客,发现一般都是通过开线程写死循环,每分钟或者每秒钟给handler发消息来刷新时间的,这种可能导致泄漏,也不稳定,所以个人并不想采纳。我们可以直接利用系统的广播来实现功能。
系统每分钟都会发送广播
Intent.ACTION_TIME_TICK
动态注册广播
IntentFilter filter=new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
registerReceiver(timeReciver,filter);
接收广播
private final BroadcastReceiver timeReciver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_TIME_TICK)) {
TODO//刷新时间
}
}
};