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                     
                }
            }
    };
  • 相关阅读:
    Java EE企业应用发展
    黄金点游戏
    C++ Word Count 发布程序
    C++原创应用类库和工具类库
    软件之魂
    latex表格multirow的使用
    web service和ejb的区别
    RPC
    hashcode()和equals()方法
    JSON
  • 原文地址:https://www.cnblogs.com/chenlong-50954265/p/4980019.html
Copyright © 2011-2022 走看看