监听时间变动事件Intent.ACTION_TIME_TICK
在众多的Intent的action动作中,Intent.ACTION_TIME_TICK是比较特殊的一个,根据SDK描述:
Broadcast Action: The current time has changed. Sent every minute. You can not receive this through components declared in manifests, only by exlicitly registering for it withContext.registerReceiver()
意思是说这个广播动作是以每分钟一次的形式发送。但你不能通过在manifest.xml里注册的方式接收到这个广播,只能在代码里通过registerReceiver()方法注册。
在androidmanifast.xml里加入
<receiverandroid:name="com.xxx.xxx.TimeChangeReceiver"> <intent-filterandroid:name="android.intent.action.ACTION_TIME_TICK"></intent-filter> </receiver>
是无效的。
想要一直监听时间变化,就只能写一个后台的service,然后给它注册一个监听了。
IntentFilter filter=new IntentFilter(); filter.addAction(Intent.ACTION_TIME_TICK); registerReceiver(receiver,filter); private final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_TIME_TICK)) { //do what you want to do ...13 } } };