Android AlarmManager鬧鐘實現

來源:互聯網
上載者:User

標籤:android 鬧鐘服務   android alarm   alarmmanager   

什麼是AlarmManager?AlarmManager是Android中常用的一種系統層級的提示服務,在特定的時刻為我們廣播一個指定的Intent。簡單的說就是我們設定一個時間,然後在該時間到來時,AlarmManager為我們廣播一個我們設定的Intent,通常我們使用 PendingIntent,PendingIntent可以理解為Intent的封裝包,簡單的說就是在Intent上在加個指定的動作。在使用Intent的時候,我們還需要在執行startActivity、startService或sendBroadcast才能使Intent有用。而PendingIntent的話就是將這個動作包含在內了
定義一個PendingIntent對象PendingIntent pi = PendingIntent.getBroadcast(this,0,intent,0);
AlarmManager的常用API1、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)該方法也用於設定重複鬧鐘,與第二個方法相似,不過其兩個鬧鐘執行的間隔時間不是固定的而已。
參數詳解:int type鬧鐘的類型,常用的有5個值:AlarmManager.ELAPSED_REALTIME、 AlarmManager.ELAPSED_REALTIME_WAKEUP、AlarmManager.RTC、 AlarmManager.RTC_WAKEUP、AlarmManager.POWER_OFF_WAKEUP。
larmManager.ELAPSED_REALTIME表示鬧鐘在手機睡眠狀態下不可用,該狀態下鬧鐘使用相對時間(相對於系統啟動開始),狀態值為3;
AlarmManager.ELAPSED_REALTIME_WAKEUP表示鬧鐘在睡眠狀態下會喚醒系統並執行提示功能,該狀態下鬧鐘也使用相對時間,狀態值為2;
AlarmManager.RTC表示鬧鐘在睡眠狀態下不可用,該狀態下鬧鐘使用絕對時間,即當前系統時間,狀態值為1;
AlarmManager.RTC_WAKEUP表示鬧鐘在睡眠狀態下會喚醒系統並執行提示功能,該狀態下鬧鐘使用絕對時間,狀態值為0;
AlarmManager.POWER_OFF_WAKEUP表示鬧鐘在手機關機狀態下也能正常進行提示功能,所以是5個狀態中用的最多的狀態之一,該狀態下鬧鐘也是用絕對時間,狀態值為4;不過本狀態好像受SDK版本影響,某些版本並不支援;
long startTime鬧鐘的第一次執行時間,以毫秒為單位,可以自訂時間,不過一般使用目前時間。需要注意的是,本屬性與第一個屬性(type)密切相關,如果第一個參數對 應的鬧鐘使用的是相對時間(ELAPSED_REALTIME和ELAPSED_R EALTIME_WAKEUP),那麼本屬性就得使用相對時間(相對於 系統啟動時間來說),比如目前時間就表示為:SystemClock.elapsedRealtime();如果第一個參數對應的鬧鐘使用的是絕對時間 (RTC、RTC_WAKEUP、POWER_OFF_WAKEUP),那麼本屬性就得使用絕對時間,比如目前時間就表示 為:System.currentTimeMillis()。
long intervalTime對於後兩個方法來說,存在本屬性,表示兩次鬧鐘執行的間隔時間,也是以毫秒為單位。
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)方法。如果這三種方法錯用了的話,雖然不會報錯,但是看不到鬧鐘提示效果。

寫一個簡單的Demo

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //建立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);    }}

廣播接收者:MyReceiver.java

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();    }}
資訊清單檔如下:AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.alarmdemo" >    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <receiver android:name=".MyReceiver">            <intent-filter>                <action android:name="ELITOR_CLOCK" />            </intent-filter>        </receiver>    </application></manifest>
運行結果:


下載Demo請猛戳



Android AlarmManager鬧鐘實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.