監聽時間變動事件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                     
            }
        }
};

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM