?
實(shí)驗(yàn)1:使用Java.util.Timer。?
在onStart()創(chuàng)創(chuàng)建Timer,每5秒更新一次計(jì)數(shù)器,并啟動(dòng)。?
|
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)試時(shí),會(huì)發(fā)現(xiàn)一切工作正常,每5秒更新一次界面,即使是按下電源鍵,仍然會(huì)5秒觸發(fā)一次。?
當(dāng)拔掉USB線,按下電源鍵關(guān)閉屏幕后,過一段時(shí)間再打開,發(fā)現(xiàn)定時(shí)器明顯沒有繼續(xù)計(jì)數(shù),停留在了關(guān)閉電源鍵時(shí)的數(shù)字。?
實(shí)驗(yàn)2:使用AlarmService:?
2.1通過AlarmService每個(gè)5秒發(fā)送一個(gè)廣播,setRepeating時(shí)的類型為AlarmManager.ELAPSED_REALTIME。?
|
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);??
|
|
am.setRepeating(AlarmManager.ELAPSED_REALTIME, firstTime,
5
*
1000
, sender);
|
拔掉USB線,按下電源鍵,過一段時(shí)間再次打開屏幕,發(fā)現(xiàn)定時(shí)器沒有繼續(xù)計(jì)數(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線,按下電源鍵,過一點(diǎn)時(shí)間再次打開屏幕,發(fā)現(xiàn)定時(shí)器一直在計(jì)數(shù)。?
如此看來,使用WAKEUP才能保證自己想要的定時(shí)器一直工作,但是肯定會(huì)引起耗電量的增加
AlarmManager的使用機(jī)制有的稱呼為全局定時(shí)器,有的稱呼為鬧鐘。通過對(duì)它的使用,個(gè)人覺得叫全局定時(shí)器比較合適,其實(shí)它的作用和Timer有點(diǎn)相似。都有兩種相似的用法:(1)在指定時(shí)長(zhǎng)后執(zhí)行某項(xiàng)操作(2)周期性的執(zhí)行某項(xiàng)操作
AlarmManager對(duì)象配合Intent使用,可以定時(shí)的開啟一個(gè)Activity,發(fā)送一個(gè)BroadCast,或者開啟一個(gè)Service.
下面的代碼詳細(xì)的介紹了兩種定時(shí)方式的使用:
?(1)在指定時(shí)長(zhǎng)后執(zhí)行某項(xiàng)操作
?
- ????? //操作:發(fā)送一個(gè)廣播,廣播接收后Toast提示定時(shí)操作完成 ??
- <!--??
- ??
- Code?highlighting?produced?by?Actipro?CodeHighlighter?(freeware)??
- http: //www.CodeHighlighter.com/ ??
- ??
- -->?????Intent?intent?= new ?Intent(Main. this ,?alarmreceiver. class );??
- ????intent.setAction( "short" );??
- ????PendingIntent?sender=??
- ????????PendingIntent.getBroadcast(Main. this ,? 0 ,?intent,? 0 );??
- ??????
- ???? //設(shè)定一個(gè)五秒后的時(shí)間 ??
- ????Calendar?calendar=Calendar.getInstance();??
- ????calendar.setTimeInMillis(System.currentTimeMillis());??
- ????calendar.add(Calendar.SECOND,? 5 );??
- ??????
- ????AlarmManager?alarm=(AlarmManager)getSystemService(ALARM_SERVICE);??
- ????alarm.set(AlarmManager.RTC_WAKEUP,?calendar.getTimeInMillis(),?sender);??
- ???? //或者以下面方式簡(jiǎn)化 ??
- ???? //alarm.set(AlarmManager.RTC_WAKEUP,?System.currentTimeMillis()+5*1000,?sender); ??
- ??????
- ????Toast.makeText(Main. this ,? "五秒后alarm開啟" ,?Toast.LENGTH_LONG).show();??
//注意:receiver記得在manifest.xml注冊(cè)
- <!--??
- ??
- Code?highlighting?produced?by?Actipro?CodeHighlighter?(freeware)??
- http: //www.CodeHighlighter.com/ ??
- ??
- -->???? public ? static ? class ?alarmreceiver? extends ?BroadcastReceiver{??
- ??
- ???????? @Override ??
- ???????? public ? void ?onReceive(Context?context,?Intent?intent)?{??
- ???????????? //?TODO?Auto-generated?method?stub ??
- ???????????? if (intent.getAction().equals( "short" )){??
- ????????????????Toast.makeText(context,? "short?alarm" ,?Toast.LENGTH_LONG).show();??
- ????????????} else {??
- ????????????????Toast.makeText(context,? "repeating?alarm" ,???
- ??????????????????????Toast.LENGTH_LONG).show();??
- ????????????}??
- ????????}??
- ????}??
(2)周期性的執(zhí)行某項(xiàng)操作
?
- <!--??
- ??
- Code?highlighting?produced?by?Actipro?CodeHighlighter?(freeware)??
- http: //www.CodeHighlighter.com/ ??
- ??
- -->????Intent?intent?= new ?Intent(Main. this ,?alarmreceiver. class );??
- ????intent.setAction( "repeating" );??
- ????PendingIntent?sender=PendingIntent??
- ????????.getBroadcast(Main. this ,? 0 ,?intent,? 0 );??
- ??????
- ??
- ???? //開始時(shí)間 ??
- ???? long ?firstime=SystemClock.elapsedRealtime();??
- ??
- ????AlarmManager?am=(AlarmManager)getSystemService(ALARM_SERVICE);??
- ??
- //5秒一個(gè)周期,不停的發(fā)送廣播 ??
- ????am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP??
- ????????????,?firstime,? 5 * 1000 ,?sender);??
?AlarmManager的setRepeating()相當(dāng)于Timer的Schedule(task,delay,peroid);有點(diǎn)差異的地方時(shí)Timer這個(gè)方法是指定延遲多長(zhǎng)時(shí)間
以后開始周期性的執(zhí)行task;
AlarmManager的取消:(其中需要注意的是取消的Intent必須與啟動(dòng)Intent保持絕對(duì)一致才能支持取消AlarmManager)
?
?
- <!--??
- ??
- Code?highlighting?produced?by?Actipro?CodeHighlighter?(freeware)??
- http: //www.CodeHighlighter.com/ ??
- ??
- -->??Intent?intent?= new ?Intent(Main. this ,?alarmreceiver. class );??
- ??intent.setAction( "repeating" );??
- ??PendingIntent?sender=PendingIntent??
- ?????????.getBroadcast(Main. this ,? 0 ,?intent,? 0 );??
- ??AlarmManager?alarm=(AlarmManager)getSystemService(ALARM_SERVICE); ?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
