1、AlarmManager,顧名思義,就是“提醒”,是Android中常用的一種系統級別的提示服務,在特定的時刻爲我們廣播一個指定的Intent。簡單的說就是我們設定一個時間,然後在該時間到來時,AlarmManager爲我們廣播一個我們設定的Intent,通常我們使用 PendingIntent,PendingIntent可以理解爲Intent的封裝包,簡單的說就是在Intent上在加個指定的動作。在使用Intent的時候,我們還需要在執行startActivity、startService或sendBroadcast才能使Intent有用。而PendingIntent的話就是將這個動作包含在內了。
定義一個PendingIntent對象。
PendingIntent pi = PendingIntent.getBroadcast(this,0,intent,0);
2、AlarmManager的常用方法有三個:
(1)set(int type,long startTime,PendingIntent pi);
該方法用於設置一次性鬧鐘,第一個參數表示鬧鐘類型,第二個參數表示鬧鐘執行時間,第三個參數表示鬧鐘響應動作。
(2)setRepeating(int type,long startTime,long intervalTime,PendingIntent pi);
該方法用於設置重複鬧鐘,第一個參數表示鬧鐘類型,第二個參數表示鬧鐘首次執行時間,第三個參數表示鬧鐘兩次執行的間隔時間,第三個參數表示鬧鐘響應動作。
(3)setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi);
該方法也用於設置重複鬧鐘,與第二個方法相似,不過其兩個鬧鐘執行的間隔時間不是固定的而已。
3、三個方法各個參數詳悉:
(1)int type: 鬧鐘的類型,常用的有5個值:AlarmManager.ELAPSED_REALTIME、 AlarmManager.ELAPSED_REALTIME_WAKEUP、AlarmManager.RTC、 AlarmManager.RTC_WAKEUP、AlarmManager.POWER_OFF_WAKEUP。
AlarmManager.ELAPSED_REALTIME表示鬧鐘在手機睡眠狀態下不可用,該狀態下鬧鐘使用相對時間(相對於系統啓動開始),狀態值爲3;
AlarmManager.ELAPSED_REALTIME_WAKEUP表示鬧鐘在睡眠狀態下會喚醒系統並執行提示功能,該狀態下鬧鐘也使用相對時間,狀態值爲2;
AlarmManager.RTC表示鬧鐘在睡眠狀態下不可用,該狀態下鬧鐘使用絕對時間,即當前系統時間,狀態值爲1;
AlarmManager.RTC_WAKEUP表示鬧鐘在睡眠狀態下會喚醒系統並執行提示功能,該狀態下鬧鐘使用絕對時間,狀態值爲0;
AlarmManager.POWER_OFF_WAKEUP表示鬧鐘在手機關機狀態下也能正常進行提示功能,所以是5個狀態中用的最多的狀態之一,該狀態下鬧鐘也是用絕對時間,狀態值爲4;不過本狀態好像受SDK版本影響,某些版本並不支持;
(2)long startTime: 鬧鐘的第一次執行時間,以毫秒爲單位,可以自定義時間,不過一般使用當前時間。需要注意的是,本屬性與第一個屬性(type)密切相關,如果第一個參數對 應的鬧鐘使用的是相對時間(ELAPSED_REALTIME和ELAPSED_REALTIME_WAKEUP),那麼本屬性就得使用相對時間(相對於 系統啓動時間來說),比如當前時間就表示爲:SystemClock.elapsedRealtime();如果第一個參數對應的鬧鐘使用的是絕對時間 (RTC、RTC_WAKEUP、POWER_OFF_WAKEUP),那麼本屬性就得使用絕對時間,比如當前時間就表示 爲:System.currentTimeMillis()。
(3)long intervalTime:對於後兩個方法來說,存在本屬性,表示兩次鬧鐘執行的間隔時間,也是以毫秒爲單位。
(4)PendingIntent pi: 綁定了鬧鐘的執行動作,比如發送一個廣播、給出提示等等。PendingIntent是Intent的封裝類。需要注意的是,如果是通過啓動服務來實現鬧鐘提 示的話,PendingIntent對象的獲取就應該採用Pending.getService(Context c,int i,Intent intent,int j)方法;如果是通過廣播來實現鬧鐘提示的話,PendingIntent對象的獲取就應該採用 PendingIntent.getBroadcast(Context c,int i,Intent intent,int j)方法;如果是採用Activity的方式來實現鬧鐘提示的話,PendingIntent對象的獲取就應該採用 PendingIntent.getActivity(Context c,int i,Intent intent,int j)方法。如果這三種方法錯用了的話,雖然不會報錯,但是看不到鬧鐘提示效果。
4.舉例說明:定義一個鬧鐘,5秒鐘重複響應。
(1)MainActivity,在onCreate中完成:
- //創建Intent對象,action爲ELITOR_CLOCK,附加信息爲字符串“你該打醬油了”
- Intent intent = new Intent("ELITOR_CLOCK");
- intent.putExtra("msg","你該打醬油了");
- //定義一個PendingIntent對象,PendingIntent.getBroadcast包含了sendBroadcast的動作。
- //也就是發送了action 爲"ELITOR_CLOCK"的intent
- PendingIntent pi = PendingIntent.getBroadcast(this,0,intent,0);
- //AlarmManager對象,注意這裏並不是new一個對象,Alarmmanager爲系統級服務
- AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
- //設置鬧鐘從當前時間開始,每隔5s執行一次PendingIntent對象pi,注意第一個參數與第二個參數的關係
- // 5秒後通過PendingIntent pi對象發送廣播
- am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),5*1000,pi);
- //創建Intent對象,action爲ELITOR_CLOCK,附加信息爲字符串“你該打醬油了”
- Intent intent = new Intent("ELITOR_CLOCK");
- intent.putExtra("msg","你該打醬油了");
- //定義一個PendingIntent對象,PendingIntent.getBroadcast包含了sendBroadcast的動作。
- //也就是發送了action 爲"ELITOR_CLOCK"的intent
- PendingIntent pi = PendingIntent.getBroadcast(this,0,intent,0);
- //AlarmManager對象,注意這裏並不是new一個對象,Alarmmanager爲系統級服務
- AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
- //設置鬧鐘從當前時間開始,每隔5s執行一次PendingIntent對象pi,注意第一個參數與第二個參數的關係
- // 5秒後通過PendingIntent pi對象發送廣播
- am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),5*1000,pi);
那麼啓動MainActivity之後,由於定義了AlarmManager am,並且調用了am.setRepeating(...)函數,則系統每隔5s將會通過pi啓動intent發送廣播,其action爲ELITOR_CLOCK。所以我們需要在Manifest.xml中註冊一個receiver,同時自己定義一個廣播接收器類。
(2)定義一個廣播接收器類MyReceiver,重寫onReceive()函數。
- public class MyReceiver extends BroadcastReceiver
- {
- @Override
- public void onReceive(Context context, Intent intent)
- {
- // TODO Auto-generated method stub
- Log.d("MyTag", "onclock......................");
- String msg = intent.getStringExtra("msg");
- Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
- }
- }
- public class MyReceiver extends BroadcastReceiver
- {
- @Override
- public void onReceive(Context context, Intent intent)
- {
- // TODO Auto-generated method stub
- Log.d("MyTag", "onclock......................");
- String msg = intent.getStringExtra("msg");
- Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
- }
- }
(3)在Manifest.xml中註冊廣播接收器:
- <receiver android:name=".MyReceiver">
- <intent-filter>
- <action android:name="ELITOR_CLOCK" />
- </intent-filter>
- </receiver>
- <receiver android:name=".MyReceiver">
- <intent-filter>
- <action android:name="ELITOR_CLOCK" />
- </intent-filter>
- </receiver>
補充:設置指定的時間啓動廣播。
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class AlarmActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Calendar c=Calendar.getInstance();
c.set(Calendar.YEAR,2011);
c.set(Calendar.MONTH,Calendar.JUNE);//也可以填數字,0-11,一月爲0
c.set(Calendar.DAY_OF_MONTH, 28);
c.set(Calendar.HOUR_OF_DAY, 19);
c.set(Calendar.MINUTE, 50);
c.set(Calendar.SECOND, 0);
//設定時間爲 2011年6月28日19點50分0秒
//c.set(2011, 05,28, 19,50, 0);
//也可以寫在一行裏
Intent intent=new Intent(this,AlarmReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(this, 0, intent,0);
//設置一個PendingIntent對象,發送廣播
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
//獲取AlarmManager對象
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
//時間到時,執行PendingIntent,只執行一次
//AlarmManager.RTC_WAKEUP休眠時會運行,如果是AlarmManager.RTC,在休眠時不會運行
//am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 10000, pi);
//如果需要重複執行,使用上面一行的setRepeating方法,倒數第二參數爲間隔時間,單位爲毫秒
}
}