Android中AlarmManager基本用法分析
本文實(shí)例講述了Android中AlarmManager基本用法。分享給大家供大家參考,具體如下:
AlarmManager的作用文檔中的解釋是:在特定的時(shí)刻為我們廣播一個(gè)指定的Intent。簡(jiǎn)單的說(shuō)就是我們?cè)O(shè)定一個(gè)時(shí)間,然后在該時(shí)間到來(lái)時(shí),AlarmManager為我們廣播一個(gè)我們?cè)O(shè)定的Intent。
對(duì)應(yīng)AlarmManager更深層的了解可以參考:
http://chabaoo.cn/article/90491.htm
android提供了四種類型的鬧鐘:
① ELAPSED_REALTIME
在指定的延時(shí)過(guò)后,發(fā)送廣播,但不喚醒設(shè)備。
② ELAPSED_REALTIME_WAKEUP
在指定的演示后,發(fā)送廣播,并喚醒設(shè)備
延時(shí)是要把系統(tǒng)啟動(dòng)的時(shí)間SystemClock.elapsedRealtime()算進(jìn)去的,具體用法看代碼。
③ RTC
在指定的時(shí)刻,發(fā)送廣播,但不喚醒設(shè)備
④ RTC_WAKEUP
在指定的時(shí)刻,發(fā)送廣播,并喚醒設(shè)備
AlarmManager提供的方法:
1. void set(int type, long triggerAtTime, PendingIntent operation)
設(shè)置一個(gè)鬧鐘
2. void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
設(shè)置一個(gè)會(huì)重復(fù)的鬧鐘
3. void setInexactRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
設(shè)置一個(gè)重復(fù)鬧鐘的不精確版本,它相對(duì)而言更節(jié)能(power-efficient)一些,因?yàn)橄到y(tǒng)可能會(huì)將幾個(gè)差不多的鬧鐘合并為一個(gè)來(lái)執(zhí)行,減少設(shè)備的喚醒次數(shù)。
內(nèi)置的幾個(gè)interval為:
INTERVAL_FIFTEEN_MINUTES
INTERVAL_HALF_HOUR
INTERVAL_HOUR
INTERVAL_HALF_DAY
INTERVAL_DAY
如果你將其設(shè)為DAY,那么可能這一天中的所有鬧鐘都會(huì)被合并掉。
void cancel(PendingIntent operation)
取消一個(gè)設(shè)置的鬧鐘
void setTimeZone(String timeZone)
設(shè)置系統(tǒng)的默認(rèn)時(shí)區(qū)。需要android.permission.SET_TIME_ZONE權(quán)限
// 首先創(chuàng)建Receiver public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "alarm", Toast.LENGTH_SHORT).show(); } }
// manifest中申明,并不需要intent-filter,我們是明確指定發(fā)到哪個(gè)receiver的 <receiver android:name="yuan.receivers.AlarmReceiver" />
PendingIntent:簡(jiǎn)單的說(shuō)就是在Intent上在加個(gè)指定的動(dòng)作。Intent的話,我們還需要在執(zhí)行startActivity、startService或sendBroadcast才能使Intent有用。而PendingIntent的話就是將這個(gè)動(dòng)作包含在內(nèi)了,如PendingIntent.getBroadcast就包含了sendBroadcast的動(dòng)作。
5s后發(fā)送指定廣播
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class); int requestCode = 0; PendingIntent pendIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); // 5秒后發(fā)送廣播,只發(fā)送一次 int triggerAtTime = SystemClock.elapsedRealtime() + 5 * 1000; alarmMgr.set(AlarmManager.ELAPSED_REALTIME, triggerAtTime, pendIntent);
5s后發(fā)送指定廣播,然后每個(gè)10秒重復(fù)發(fā)送廣播
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class); int requestCode = 0; PendingIntent pendIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); // 5秒后發(fā)送廣播,然后每個(gè)10秒重復(fù)發(fā)廣播。廣播都是直接發(fā)到AlarmReceiver的 int triggerAtTime = SystemClock.elapsedRealtime() + 5 * 1000; int interval = 10 * 1000; alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, interval, pendIntent);
取消一個(gè)鬧鐘
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class); PendingIntent pendIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // 與上面的intent匹配(filterEquals(intent))的鬧鐘會(huì)被取消 alarmMgr.cancel(pendIntent);
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android自定義View之自定義評(píng)價(jià)打分控件RatingBar實(shí)現(xiàn)自定義星星大小和間距
Android開(kāi)發(fā)中,我們經(jīng)常會(huì)用到對(duì)商家或者商品的評(píng)價(jià),運(yùn)用星星進(jìn)行打分。這篇文章介紹了Android自定義View之自定義評(píng)價(jià)打分控件RatingBar可以自定義星星大小和間距的相關(guān)資料,感興趣的朋友一起看看吧2016-10-10Android開(kāi)發(fā)自定義TextView省略號(hào)樣式的方法
這篇文章主要介紹了Android開(kāi)發(fā)自定義TextView省略號(hào)樣式的方法,結(jié)合實(shí)例形式分析了Android文本控件TextView相關(guān)屬性與字符串操作技巧,需要的朋友可以參考下2017-10-10Android手勢(shì)密碼--設(shè)置和校驗(yàn)功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android手勢(shì)密碼--設(shè)置和校驗(yàn)功能的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考校驗(yàn)價(jià)值,需要的朋友可以參考下2018-05-05Android開(kāi)發(fā)之a(chǎn)ctiviti節(jié)點(diǎn)跳轉(zhuǎn)
這篇文章主要介紹了Android開(kāi)發(fā)之a(chǎn)ctiviti節(jié)點(diǎn)跳轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下2016-04-04Android強(qiáng)制設(shè)定橫屏?xí)r,SurfaceView一直黑屏
本文主要介紹了Android強(qiáng)制設(shè)定橫屏?xí)r,SurfaceView一直黑屏的方法。具有一定的參考作用,下面跟著小編一起來(lái)看下吧2017-01-01Android使用ContentResolver搜索手機(jī)通訊錄的方法
這篇文章主要介紹了Android使用ContentResolver搜索手機(jī)通訊錄的方法,結(jié)合實(shí)例形式分析了Android中ContentResolver操作手機(jī)通訊錄的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01Android實(shí)現(xiàn)在列表List中顯示半透明小窗體效果的控件用法詳解
這篇文章主要介紹了Android實(shí)現(xiàn)在列表List中顯示半透明小窗體效果的控件用法,結(jié)合實(shí)例形式分析了Android半透明提示框的實(shí)現(xiàn)與設(shè)置技巧,需要的朋友可以參考下2016-06-06