應(yīng)用地址:https://blog.csdn.net/duan140524/article/details/52289834
Android整點報時
原創(chuàng) walkinthecold 最后發(fā)布于2016-08-23 12:28:41 閱讀數(shù) 5412 收藏
展開
剛開始接到這個需求的時候,首先就想到了開個線程,不停的去掃當前的時間,但是一想這個做法很耗性能,肯定有更好的辦法。
果然,在網(wǎng)上百度了個demo,一看別人的代碼,原來Android系統(tǒng)里面有個 Intent.ACTION_TIME_TICK ,這個Action的意思就是
tick會以分鐘為單位,每分鐘發(fā)一次,那么我們只需要寫個廣播,接收一下就行了。注意這個廣播最好動態(tài)注冊,不然有可能接收不到。
這個問題解決了其他就很簡單了。完整代碼如下:
? ? ? /**
? ? ? ? * 整點報時
? ? ? ? */
? ? ? ? private void initTimePrompt() {
? ? ? ? ? ? IntentFilter timeFilter = new IntentFilter();
? ? ? ? ? ? timeFilter.addAction(Intent.ACTION_TIME_TICK);
? ? ? ? ? ? registerReceiver(mTimeReceiver, timeFilter);
? ? ? ? }
? ? ? ? private BroadcastReceiver mTimeReceiver = new BroadcastReceiver() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onReceive(Context context, Intent intent) {
? ? ? ? ? ? ? ? Calendar cal = Calendar.getInstance();
? ? ? ? ? ? ? ? int hour = cal.get(Calendar.HOUR_OF_DAY);
? ? ? ? ? ? ? ? int min = cal.get(Calendar.MINUTE);
? ? ? ? ? ? ? ? if (min == 0) {
? ? ? ? ? ? ? ? ? ? TXZTtsManager.getInstance().speakText("現(xiàn)在是北京時間" + hour + "點整");
? ? ? ? ? ? ? ? } else if (min == 30) {
? ? ? ? ? ? ? ? ? ? TXZTtsManager.getInstance().speakText("現(xiàn)在是北京時間" + hour + "點三十分");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? };
————————————————
版權(quán)聲明:本文為CSDN博主「walkinthecold」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/duan140524/article/details/52289834