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裏註冊廣播接收器 
 
?
1
2
3
4
5
<receiver android:name="MyReceiver">
            <intent-filter>
                <action android: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)在指定時長後(特定時間)執行某項操作 
  
      
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//操作:發送一個廣播,廣播接收後Toast提示定時操作完成
     Intent intent =new Intent(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)週期性的執行某項操作


?
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);
    
    //開始時間
    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) 
  
?
1
2
3
4
5
6
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);
alarm.cancel(sender);
  

 AlarmManager還將鬧鐘分爲五種類型:
 

?
1
public   static final int ELAPSED_REALTIME          
   //當系統進入睡眠狀態時,這種類型的鬧鈴不會喚醒系統。直到系統下次被喚醒才傳遞它,該鬧鈴所用的時間是相對時間,是從系統啓動後開始計時的,包括睡眠   
   時間,可以通過調用SystemClock.elapsedRealtime()獲得。系統值是3 (0x00000003)。 
   
?
1
public static final int ELAPSED_REALTIME_WAKEUP
 //能喚醒系統,用法同ELAPSED_REALTIME,系統值是2 (0x00000002) 。   
   
?
1
public static final int RTC
   //當系統進入睡眠狀態時,這種類型的鬧鈴不會喚醒系統。直到系統下次被喚醒才傳遞它,該鬧鈴所用的時間是絕對時間,所用時間是UTC時間,可以通過調用   
   System.currentTimeMillis()獲得。系統值是1 (0x00000001) 。 
  
?
1
public static final int RTC_WAKEUP
 //能喚醒系統,用法同RTC類型,系統值爲 0 (0x00000000) 。  
  
?
1
Public static final int POWER_OFF_WAKEUP
   //能喚醒系統,它是一種關機鬧鈴,就是說設備在關機狀態下也可以喚醒系統,所以我們把它稱之爲關機鬧鈴。使用方法同RTC類型,系統值爲4 (0x00000004)。


綜上所述,感覺AlarmManage和NotificationManager差不多,NotificationManager例子請見文章http://my.oschina.net/helu/blog/141728

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章