首页 > 其他 > 详细

监听时间变动事件Intent.ACTION_TIME_TICK

时间:2015-11-20 12:11:59      阅读:433      评论:0      收藏:0      [点我收藏+]

在众多的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                     
            }
        }
};

 

监听时间变动事件Intent.ACTION_TIME_TICK

原文:http://www.cnblogs.com/chenlong-50954265/p/4980019.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!