Android整點報時

應(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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容