android闹钟实现原理
鬧鐘的原理可用下面我自己畫的一幅圖來概括:(不對(duì)的地方,盡管吐槽)
?
? 我們來看看新建鬧鐘到鬧鐘響鈴的步驟:
?
?1、新建一個(gè)鬧鐘:
?????
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // 獲得AlarmManager實(shí)例 final AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); // 實(shí)例化Intent Intent intent = new Intent(); // 設(shè)置Intent action屬性 intent.setAction("com.test.BC_ACTION"); intent.putExtra("msg", "該去開會(huì)啦!"); // 實(shí)例化PendingIntent final PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); // 獲得系統(tǒng)時(shí)間 final long time = System.currentTimeMillis(); am.set(AlarmManager.RTC_WAKEUP, time+5000, sender);//5秒后鬧鈴 // 設(shè)置按鈕單擊事件 setBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 重復(fù)提示,從當(dāng)前時(shí)間開始,間隔5秒 am.setRepeating(AlarmManager.RTC_WAKEUP, time, 5 * 1000, pi); } }); |
???
??在AndroidMainfest.xml里注冊(cè)廣播接收器?
?
| 1 2 3 4 5 | <receiverandroid:name="MyReceiver"> <intent-filter> <actionandroid:name="com.test.BC_ACTION"/> </intent-filter> </receiver> |
???
???
?2、定義一個(gè)AlarmReceiver extends BroadcastReceiver接收廣播,并彈出鬧鐘提醒視圖。?
??
?上面用到一個(gè)AlarmManage,我們分別來看看它的處理鬧鐘流程和作用及例子。?
?處理鬧鐘流程:對(duì)應(yīng)AlarmManage有一個(gè)AlarmManagerServie服務(wù)程序,該服務(wù)程序才是正真提供鬧鈴服務(wù)的,它主要遍歷鬧鈴列表并設(shè)置即將觸發(fā)的鬧鈴給鬧鈴設(shè)備,并且一直監(jiān)聽鬧鈴設(shè)備,一旦有鬧鈴觸發(fā)或者是鬧鈴事件發(fā)生,AlarmManagerServie服務(wù)程序就會(huì)遍歷鬧鈴列表找到相應(yīng)的注冊(cè)鬧鈴并發(fā)出廣播。?
??
?作用及例子:AlarmManage中文名鬧鐘,或者叫做“全局定時(shí)器”更合適,它的作用和Timer類似,有兩種使用方法:1、在特定時(shí)長后(特定時(shí)間)執(zhí)行某任務(wù);2、周期性的執(zhí)行某任務(wù),AlarmManager對(duì)象配合Intent使用,可以定時(shí)的開啟一個(gè)Activity,發(fā)送一個(gè)BroadCast,或者開啟一個(gè)Service.?
??
?(1)在指定時(shí)長后(特定時(shí)間)執(zhí)行某項(xiàng)操作?
??
??????
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //操作:發(fā)送一個(gè)廣播,廣播接收后Toast提示定時(shí)操作完成 Intent intent =newIntent(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); //或者以下面方式簡化 //alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5*1000, sender); Toast.makeText(Main.this,"五秒后alarm開啟", Toast.LENGTH_LONG).show(); |
??
??
?(2)周期性的執(zhí)行某項(xiàng)操作
| 1 2 3 4 5 6 7 8 9 10 11 12 | 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的取消:(其中需要注意的是取消的Intent必須與啟動(dòng)Intent保持絕對(duì)一致才能支持取消AlarmManager)?
??
| 1 2 3 4 5 6 | Intent intent =newIntent(Main.this, alarmreceiver.class); intent.setAction("repeating"); PendingIntent sender=PendingIntent .getBroadcast(Main.this,0, intent,0); AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE); alarm.cancel(sender); |
??
?AlarmManager還將鬧鐘分為五種類型:
?
| 1 | public??staticfinalintELAPSED_REALTIME???? ????? |
?? //當(dāng)系統(tǒng)進(jìn)入睡眠狀態(tài)時(shí),這種類型的鬧鈴不會(huì)喚醒系統(tǒng)。直到系統(tǒng)下次被喚醒才傳遞它,該鬧鈴所用的時(shí)間是相對(duì)時(shí)間,是從系統(tǒng)啟動(dòng)后開始計(jì)時(shí)的,包括睡眠???
?? 時(shí)間,可以通過調(diào)用SystemClock.elapsedRealtime()獲得。系統(tǒng)值是3 (0x00000003)。?
???
| 1 | publicstaticfinalintELAPSED_REALTIME_WAKEUP |
?//能喚醒系統(tǒng),用法同ELAPSED_REALTIME,系統(tǒng)值是2 (0x00000002) 。???
???
| 1 | public static final int RTC |
?? //當(dāng)系統(tǒng)進(jìn)入睡眠狀態(tài)時(shí),這種類型的鬧鈴不會(huì)喚醒系統(tǒng)。直到系統(tǒng)下次被喚醒才傳遞它,該鬧鈴所用的時(shí)間是絕對(duì)時(shí)間,所用時(shí)間是UTC時(shí)間,可以通過調(diào)用???
?? System.currentTimeMillis()獲得。系統(tǒng)值是1 (0x00000001) 。?
??
| 1 | publicstaticfinalintRTC_WAKEUP |
?//能喚醒系統(tǒng),用法同RTC類型,系統(tǒng)值為 0 (0x00000000) 。??
??
| 1 | PublicstaticfinalintPOWER_OFF_WAKEUP |
?? //能喚醒系統(tǒng),它是一種關(guān)機(jī)鬧鈴,就是說設(shè)備在關(guān)機(jī)狀態(tài)下也可以喚醒系統(tǒng),所以我們把它稱之為關(guān)機(jī)鬧鈴。使用方法同RTC類型,系統(tǒng)值為4 (0x00000004)。
綜上所述,感覺AlarmManage和NotificationManager差不多,NotificationManager例子請(qǐng)見文章http://my.oschina.net/helu/blog/141728
轉(zhuǎn)載于:https://www.cnblogs.com/Free-Thinker/p/6402361.html
總結(jié)
以上是生活随笔為你收集整理的android闹钟实现原理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做梦梦到被蛇咬没咬到是什么意思
- 下一篇: 梦到花预示着什么