Android中的AlarmManager的使用

AlarmManager是Android中的一種系統級別的提醒服務,它會爲咱們在特定的時刻廣播一個指定的Intent。而使用Intent的時候,咱們還須要它執行一個動做,如startActivity,startService,startBroadcast,才能使Intent有用。一般咱們使用PendingIntent,它能夠理解爲對Intent的封裝,包含了指定的動做。html

 

咱們能夠經過PendingIntent的靜態方法獲得一個PendingIntent對象,以下:java

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);  

 

使用PendingIntent的getBroadcast (Context context, int requestCode, Intent intent, int flags)方法能夠獲得一個發送廣播動做的PendingIntent對象。其中getBroadcast的第4個參數能夠爲如下4個常量或其餘支持使用Intent.fillIn()來控制它的變量:android

FLAG_CANCEL_CURRENT:若是描述的PendingIntent對象已經存在時,會先取消當前的PendingIntent對象再生成新的。ide

FLAG_NO_CREATE:若是描述的PendingIntent對象不存在,它會返回null而不是去建立它。spa

FLAG_ONE_SHOT:建立的PendingIntent對象只使用一次。.net

FLAG_UPDATE_CURRENT:若是描述的PendingIntent對象存在,則保留它,並將新的PendingIntent對象的數據替換進去。code

 

接下來看AlarmManager,咱們經過如下代碼來取得AlarmManager對象。xml

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);  

AlarmManager對象中經常使用的方法有三個:htm

 

一、set(int type,long startTime,PendingIntent pi),用於設置一次鬧鐘。對象

二、setRepeating(int type,long startTime,long intervalTime,PendingIntent pi),用於設置重複鬧鐘。

三、setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi),一樣是用於設置重複鬧鐘,可是它是不許確的,相對於第二個方法,也更加節能。由於系統會將差很少的鬧鐘進行合併,以免在沒必要要地喚醒設備。

在上面的三個方法中,type爲鬧鐘的類型,它可使用如下四個常量:

ELAPSED_REALTIME:鬧鐘在睡眠狀態下不可用,使用的是相對系統啓動時間。

ELAPSED_REALTIME_WAKEUP:鬧鐘在睡眠狀態下可用,使用的是相對系統啓動時間。

RTC:鬧鐘在睡眠狀態下不可用,使用的是真實時間。

RTC_WAKEUP:鬧鐘在睡眠狀態下可用,使用的是真實時間。

startTime爲鬧鐘開始時間。

intervalTime爲鬧鐘間隔,在第三個方法中,內置的幾個變量以下:

INTERVAL_FIFTEEN_MINUTES 
INTERVAL_HALF_HOUR 
INTERVAL_HOUR 
INTERVAL_HALF_DAY 
INTERVAL_DAY

 

若是咱們設定的是發送廣播的鬧鐘,咱們還須要寫一個廣播接收器,並對其進行註冊,它纔會在鬧鐘開始的時候接收到廣播。

若是要設定啓動Activity或Service的鬧鐘,則在建立PendingIntent的時候,首先Intent對象需設定指定的Activity或Service的class對象,而後對應的調用PendingIntent.getActivity()或PendingIntent.getService()方法。

下面以設置發送廣播的鬧鐘代碼實例來看AlarmManager的使用:

首先設定一個鬧鐘:

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. Intent intent = new Intent("pw.msdx.ACTION_SEND");  
  2. PendingIntent sendIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
  3. AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);  
  4. am.cancel(sendIntent);  
  5. am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60 * 10 * 1000, sendIntent);  

在上面的例子中,就會從當前的時間開始,每10分鐘啓動一次鬧鐘提醒。須要注意的是,若是設定的開始時間已通過去,它會立刻啓動鬧鐘提醒。

 

接下來須要寫一個廣播接收器來接收這個廣播並進行處理。

代碼以下:

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. public class SendReceiver extends BroadcastReceiver {  
  2.     public final static String ACTION_SEND = "pw.msdx.ACTION_SEND<span style="font-family: Arial, Helvetica, sans-serif;">";</span>  
  3.   
  4.     @Override  
  5.     public void onReceive(final Context context, Intent intent) {  
  6.         String action = intent.getAction();  
  7.         if (ACTION_SEND.equals(action)) {  
  8.             Log.i("SendReceiver", "send a message");  
  9.         }  
  10.     }  
  11. }  


而後咱們還須要註冊這個廣播接收器,這樣它才能接收到廣播。這裏採用的是靜態註冊的方法,在AndroidManifest裏進行註冊:

 

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. <receiver  
  2.     android:name=".SendReceiver"  
  3.     android:enabled="true"  
  4.     android:exported="true"  
  5.      >  
  6.     <intent-filter>  
  7.         <action android:name="pw.msdx.ACTION_SEND"/>  
  8.     </intent-filter>  
  9. </receiver>  
相關文章
相關標籤/搜索