欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

android 定時器的實現(xiàn)

系統(tǒng) 2495 0

?


在Android上常用的定時器有兩種,一種是Java.util.Timer,一種就是系統(tǒng)的AlarmService了。?


實驗1:使用Java.util.Timer。?
在onStart()創(chuàng)創(chuàng)建Timer,每5秒更新一次計數(shù)器,并啟動。?


mTimer = new ? Timer();???????

mTimer.schedule( new ? TimerTask() {???????????

???????????? @Override

???????????? public ? void ? run() {

???????????????? ++mCount;

???????????????? mHandler.sendEmptyMessage( 0 );???????????????

???????????? }

???????? }, 5 * 1000 , 5 * 1000 );
??

當(dāng)連接USB線進(jìn)行調(diào)試時,會發(fā)現(xiàn)一切工作正常,每5秒更新一次界面,即使是按下電源鍵,仍然會5秒觸發(fā)一次。?
當(dāng)拔掉USB線,按下電源鍵關(guān)閉屏幕后,過一段時間再打開,發(fā)現(xiàn)定時器明顯沒有繼續(xù)計數(shù),停留在了關(guān)閉電源鍵時的數(shù)字。?

實驗2:使用AlarmService:?
2.1通過AlarmService每個5秒發(fā)送一個廣播,setRepeating時的類型為AlarmManager.ELAPSED_REALTIME。?


AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);??

am.setRepeating(AlarmManager.ELAPSED_REALTIME, firstTime, 5 * 1000 , sender);

拔掉USB線,按下電源鍵,過一段時間再次打開屏幕,發(fā)現(xiàn)定時器沒有繼續(xù)計數(shù)。?
2.2setRepeating是的類型設(shè)置為AlarmManager.ELAPSED_REALTIME_WAKEUP?


AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);???

am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 5 * 1000 , sender);

拔掉USB線,按下電源鍵,過一點時間再次打開屏幕,發(fā)現(xiàn)定時器一直在計數(shù)。?

如此看來,使用WAKEUP才能保證自己想要的定時器一直工作,但是肯定會引起耗電量的增加



AlarmManager的使用機(jī)制有的稱呼為全局定時器,有的稱呼為鬧鐘。通過對它的使用,個人覺得叫全局定時器比較合適,其實它的作用和Timer有點相似。都有兩種相似的用法:(1)在指定時長后執(zhí)行某項操作(2)周期性的執(zhí)行某項操作

AlarmManager對象配合Intent使用,可以定時的開啟一個Activity,發(fā)送一個BroadCast,或者開啟一個Service.

下面的代碼詳細(xì)的介紹了兩種定時方式的使用:

?(1)在指定時長后執(zhí)行某項操作

?

Java代碼?? 收藏代碼
  1. ????? //操作:發(fā)送一個廣播,廣播接收后Toast提示定時操作完成 ??
  2. <!--??
  3. ??
  4. Code?highlighting?produced?by?Actipro?CodeHighlighter?(freeware)??
  5. http: //www.CodeHighlighter.com/ ??
  6. ??
  7. -->?????Intent?intent?= new ?Intent(Main. this ,?alarmreceiver. class );??
  8. ????intent.setAction( "short" );??
  9. ????PendingIntent?sender=??
  10. ????????PendingIntent.getBroadcast(Main. this ,? 0 ,?intent,? 0 );??
  11. ??????
  12. ???? //設(shè)定一個五秒后的時間 ??
  13. ????Calendar?calendar=Calendar.getInstance();??
  14. ????calendar.setTimeInMillis(System.currentTimeMillis());??
  15. ????calendar.add(Calendar.SECOND,? 5 );??
  16. ??????
  17. ????AlarmManager?alarm=(AlarmManager)getSystemService(ALARM_SERVICE);??
  18. ????alarm.set(AlarmManager.RTC_WAKEUP,?calendar.getTimeInMillis(),?sender);??
  19. ???? //或者以下面方式簡化 ??
  20. ???? //alarm.set(AlarmManager.RTC_WAKEUP,?System.currentTimeMillis()+5*1000,?sender); ??
  21. ??????
  22. ????Toast.makeText(Main. this ,? "五秒后alarm開啟" ,?Toast.LENGTH_LONG).show();??
?

//注意:receiver記得在manifest.xml注冊


Java代碼?? 收藏代碼
  1. <!--??
  2. ??
  3. Code?highlighting?produced?by?Actipro?CodeHighlighter?(freeware)??
  4. http: //www.CodeHighlighter.com/ ??
  5. ??
  6. -->???? public ? static ? class ?alarmreceiver? extends ?BroadcastReceiver{??
  7. ??
  8. ???????? @Override ??
  9. ???????? public ? void ?onReceive(Context?context,?Intent?intent)?{??
  10. ???????????? //?TODO?Auto-generated?method?stub ??
  11. ???????????? if (intent.getAction().equals( "short" )){??
  12. ????????????????Toast.makeText(context,? "short?alarm" ,?Toast.LENGTH_LONG).show();??
  13. ????????????} else {??
  14. ????????????????Toast.makeText(context,? "repeating?alarm" ,???
  15. ??????????????????????Toast.LENGTH_LONG).show();??
  16. ????????????}??
  17. ????????}??
  18. ????}??
?

(2)周期性的執(zhí)行某項操作

?

Java代碼?? 收藏代碼
  1. <!--??
  2. ??
  3. Code?highlighting?produced?by?Actipro?CodeHighlighter?(freeware)??
  4. http: //www.CodeHighlighter.com/ ??
  5. ??
  6. -->????Intent?intent?= new ?Intent(Main. this ,?alarmreceiver. class );??
  7. ????intent.setAction( "repeating" );??
  8. ????PendingIntent?sender=PendingIntent??
  9. ????????.getBroadcast(Main. this ,? 0 ,?intent,? 0 );??
  10. ??????
  11. ??
  12. ???? //開始時間 ??
  13. ???? long ?firstime=SystemClock.elapsedRealtime();??
  14. ??
  15. ????AlarmManager?am=(AlarmManager)getSystemService(ALARM_SERVICE);??
  16. ??
  17. //5秒一個周期,不停的發(fā)送廣播 ??
  18. ????am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP??
  19. ????????????,?firstime,? 5 * 1000 ,?sender);??
?

?AlarmManager的setRepeating()相當(dāng)于Timer的Schedule(task,delay,peroid);有點差異的地方時Timer這個方法是指定延遲多長時間

以后開始周期性的執(zhí)行task;

AlarmManager的取消:(其中需要注意的是取消的Intent必須與啟動Intent保持絕對一致才能支持取消AlarmManager)

?

?

Java代碼?? 收藏代碼
  1. <!--??
  2. ??
  3. Code?highlighting?produced?by?Actipro?CodeHighlighter?(freeware)??
  4. http: //www.CodeHighlighter.com/ ??
  5. ??
  6. -->??Intent?intent?= new ?Intent(Main. this ,?alarmreceiver. class );??
  7. ??intent.setAction( "repeating" );??
  8. ??PendingIntent?sender=PendingIntent??
  9. ?????????.getBroadcast(Main. this ,? 0 ,?intent,? 0 );??
  10. ??AlarmManager?alarm=(AlarmManager)getSystemService(ALARM_SERVICE); ?

android 定時器的實現(xiàn)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日本在线观看 | a视频在线| 午夜精品视频 | 色综合久久天天综合观看 | 成人激情视频网站 | www视频在线观看 | 先锋资源久久 | 亚洲亚洲人成综合网络 | 国产99久久精品 | 六月综合网| 色噜噜亚洲男人的天堂 | 久久成人18免费网站 | 国产激情一区二区三区 | 国产精品久久久久久久7电影 | 国产AV亚洲精品久久久久 | 精品欧美成人高清视频在线观看 | 亚洲av毛片成人精品 | 站长推荐国产午夜免费视频 | 国产亚洲精品国产 | 欧美成人精品二区三区99精品 | 欧美视频在线看 | 欧美综合中文字幕久久 | 天天久久狠狠色综合 | 久久精品免费人成人A片 | 91九色精品国产 | 久久久久免费 | 免费观看成人毛片A片2008 | av电影直播 | 我把寡妇日出水好爽视频 | 精品国产AV色一区二区深夜久久 | www97影院| 涩涩久久| 午夜特级毛片 | 久久日本精品99久久久久 | 国产成人精品一区二区三区视频 | 色狠狠狠色噜噜噜综合网 | 三级网站免费观看 | 91免费版在线看 | 国产精品爽爽va在线观看网站 | 99re视频| 亚洲免费视频网站 |