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
<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)在指定時長后(特定時間)執行某項操作 
  
      

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//操作:發送一個廣播,廣播接收后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)周期性的執行某項操作


?
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 =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          

   //當系統進入睡眠狀態時,這種類型的鬧鈴不會喚醒系統。直到系統下次被喚醒才傳遞它,該鬧鈴所用的時間是相對時間,是從系統啟動后開始計時的,包括睡眠   
   時間,可以通過調用SystemClock.elapsedRealtime()獲得。系統值是3 (0x00000003)。 
   

?
1
publicstaticfinalintELAPSED_REALTIME_WAKEUP

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

?
1
public static final int RTC

   //當系統進入睡眠狀態時,這種類型的鬧鈴不會喚醒系統。直到系統下次被喚醒才傳遞它,該鬧鈴所用的時間是絕對時間,所用時間是UTC時間,可以通過調用   
   System.currentTimeMillis()獲得。系統值是1 (0x00000001) 。 
  

?
1
publicstaticfinalintRTC_WAKEUP

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

?
1
PublicstaticfinalintPOWER_OFF_WAKEUP

   //能喚醒系統,它是一種關機鬧鈴,就是說設備在關機狀態下也可以喚醒系統,所以我們把它稱之為關機鬧鈴。使用方法同RTC類型,系統值為4 (0x00000004)。
綜上所述,感覺AlarmManage和NotificationManager差不多,NotificationManager例子請見文章http://my.oschina.net/helu/blog/141728


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM