Intent.ACTION_TIME_TICK 广播需要动态注册,不能在清单文件配置.
- TimeReceiver mBroadcastReceiver = new TimeReceiver();
- IntentFilter intentFilter = new IntentFilter();
- intentFilter.addAction(Intent.ACTION_TIME_TICK);
- registerReceiver(mBroadcastReceiver, intentFilter);
在 TimeReceiver 监听广播接收。
- public class TimeReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- if(Intent.ACTION_TIME_TICK.equals(intent.getAction())) {
- //todo...
- }
- }
- }
在众多的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()方法注册。