天天看點

android鬧鐘實作原理

鬧鐘的原理可用下面我自己畫的一幅圖來概括:(不對的地方,盡管吐槽)

 ​​

android鬧鐘實作原理

​​

  我們來看看建立鬧鐘到鬧鐘響鈴的步驟:

 1、建立一個鬧鐘:

?

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執行個體

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

       // 執行個體化Intent

       Intent intent = new Intent();

       // 設定Intent action屬性

       intent.setAction("com.test.BC_ACTION");

       intent.putExtra("msg", "該去開會啦!");

       // 執行個體化PendingIntent

       final PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0,

               intent, 0);

       // 獲得系統時間

       final long time = System.currentTimeMillis();

 am.set(AlarmManager.RTC_WAKEUP, time+5000, sender);//5秒後鬧鈴

       // 設定按鈕單擊事件

       setBtn.setOnClickListener(new OnClickListener() {

           @Override

           public void onClick(View v) {

               // 重複提示,從目前時間開始,間隔5秒

               am.setRepeating(AlarmManager.RTC_WAKEUP, time,

                       5 * 1000, pi);

           }

       });

  在AndroidMainfest.xml裡注冊廣播接收器 

<receiverandroid:name="MyReceiver">

            <intent-filter>

                <actionandroid:name="com.test.BC_ACTION"/>

            </intent-filter>

        </receiver>

 2、定義一個AlarmReceiver extends BroadcastReceiver接收廣播,并彈出鬧鐘提醒視圖。 

 上面用到一個AlarmManage,我們分别來看看它的處理鬧鐘流程和作用及例子。 

 處理鬧鐘流程:對應AlarmManage有一個AlarmManagerServie服務程式,該服務程式才是正真提供鬧鈴服務的,它主要周遊鬧鈴清單并設定即将觸發的鬧鈴給鬧鈴裝置,并且一直監聽鬧鈴裝置,一旦有鬧鈴觸發或者是鬧鈴事件發生,AlarmManagerServie服務程式就會周遊鬧鈴清單找到相應的注冊鬧鈴并發出廣播。 

 作用及例子:AlarmManage中文名鬧鐘,或者叫做“全局定時器”更合适,它的作用和Timer類似,有兩種使用方法:1、在特定時長後(特定時間)執行某任務;2、周期性的執行某任務,AlarmManager對象配合Intent使用,可以定時的開啟一個Activity,發送一個BroadCast,或者開啟一個Service. 

 (1)在指定時長後(特定時間)執行某項操作 

//操作:發送一個廣播,廣播接收後Toast提示定時操作完成

     Intent intent =newIntent(Main.this, alarmreceiver.class);

    intent.setAction("short");

    PendingIntent sender=

        PendingIntent.getBroadcast(Main.this,0, intent,0);

    //設定一個五秒後的時間

    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)周期性的執行某項操作

Intent intent =new Intent(Main.this, alarmreceiver.class);

    intent.setAction("repeating");

    PendingIntent sender=PendingIntent

        .getBroadcast(Main.this, 0, intent, 0);

    //開始時間

    long firstime=SystemClock.elapsedRealtime();

    AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);

  //5秒一個周期,不停的發送廣播

    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP

            , firstime, 5*1000, sender);

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

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還将鬧鐘分為五種類型:

public  staticfinalintELAPSED_REALTIME          

   //當系統進入睡眠狀态時,這種類型的鬧鈴不會喚醒系統。直到系統下次被喚醒才傳遞它,該鬧鈴所用的時間是相對時間,是從系統啟動後開始計時的,包括睡眠   

   時間,可以通過調用SystemClock.elapsedRealtime()獲得。系統值是3 (0x00000003)。 

publicstaticfinalintELAPSED_REALTIME_WAKEUP

 //能喚醒系統,用法同ELAPSED_REALTIME,系統值是2 (0x00000002) 。   

public static final int RTC

   //當系統進入睡眠狀态時,這種類型的鬧鈴不會喚醒系統。直到系統下次被喚醒才傳遞它,該鬧鈴所用的時間是絕對時間,所用時間是UTC時間,可以通過調用   

   System.currentTimeMillis()獲得。系統值是1 (0x00000001) 。 

publicstaticfinalintRTC_WAKEUP

 //能喚醒系統,用法同RTC類型,系統值為 0 (0x00000000) 。  

PublicstaticfinalintPOWER_OFF_WAKEUP

   //能喚醒系統,它是一種關機鬧鈴,就是說裝置在關機狀态下也可以喚醒系統,是以我們把它稱之為關機鬧鈴。使用方法同RTC類型,系統值為4 (0x00000004)。

綜上所述,感覺AlarmManage和NotificationManager差不多,NotificationManager例子請見文章