zoukankan      html  css  js  c++  java
  • 监听时间变动事件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                     
                }
            }
    };
  • 相关阅读:
    centos7安装KVM
    keepalived高可用
    Jenkins-Pipeline 流水线发布部署项目
    centos7部署jenkins
    版本控制gitlab
    c语言寻找3000以内亲密数对
    c语言寻找1000以内的完全数
    c语言分解因式
    c语言判断给定日期是当年的第几天
    c语言计算程序运行时间
  • 原文地址:https://www.cnblogs.com/chenlong-50954265/p/4980019.html
Copyright © 2011-2022 走看看