Android鬧鐘機(jī)制實(shí)現(xiàn)定時(shí)任務(wù)功能
Android的鬧鐘實(shí)現(xiàn)機(jī)制, 需要調(diào)用AlarmManager.set()將鬧鈴時(shí)間記錄到系統(tǒng)中,當(dāng)鬧鈴時(shí)間到后,系統(tǒng)會(huì)給應(yīng)用程序發(fā)送廣播,我們只需要去注冊(cè)廣播接收器就可以了。
本文分三部分講解如何實(shí)現(xiàn)鬧鐘:
目錄:
1. 設(shè)置鬧鈴時(shí)間;
2. 接收鬧鈴事件廣播;
3. 重開機(jī)后重新計(jì)算并設(shè)置鬧鈴時(shí)間;
1. 設(shè)置鬧鈴時(shí)間(毫秒)
private void setAlarmTime(Context context, long triggerAtMillis) {
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent("android.alarm.demo.action");
PendingIntent sender = PendingIntent.getBroadcast(
context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//鬧鈴間隔, 這里設(shè)為1分鐘鬧一次,在第2步我們將每隔1分鐘收到一次廣播
//int interval = 60 * 1000;
//am.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillis, interval, sender);
am.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, sender);
}
第二個(gè)參數(shù)它大致分為兩種類型 一種是相對(duì)時(shí)間 一種是絕對(duì)時(shí)間。
所以,根據(jù)使用的類型不同 triggerAtTime設(shè)置也有所不同。
如果使用ELAPSED_REALTIME_WAKEUP類型 應(yīng)該調(diào)用SystemClock.elapsedRealtime()獲取相對(duì)時(shí)間在加上你設(shè)定的延遲時(shí)間。
2. 接收鬧鈴事件廣播
public class AlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if ("android.alarm.demo.action".equals(intent.getAction())) {
// 第1步中設(shè)置的鬧鈴時(shí)間到,這里可以彈出鬧鈴提示并播放響鈴
Toast.makeText(context, "hello alarm", Toast.LENGTH_LONG).show();
System.out.println("hello alarm");
// 可以繼續(xù)設(shè)置下一次鬧鈴時(shí)間;
return;
}
}
}
當(dāng)然,Receiver是需要在Manifest.xml中注冊(cè)的:
<receiver android:name="AlarmReceiver">
<intent-filter>
<action android:name="android.alarm.demo.action" />
</intent-filter>
</receiver>
3. 重開機(jī)后重新計(jì)算并設(shè)置鬧鈴時(shí)間
當(dāng)然要有一個(gè)BootReceiver:
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
//重新計(jì)算鬧鈴時(shí)間,并調(diào)第一步的方法設(shè)置鬧鈴時(shí)間及鬧鈴間隔時(shí)間
}
}
}
當(dāng)然,也需要注冊(cè):
<receiver android:name="BootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
我在設(shè)置時(shí)鐘的時(shí)候遇到一點(diǎn)問題
我開始的代碼是這樣寫的
alarmManager.set(AlarmManager.RTC_WAKEUP, (5*1000), sender);
我的本意是設(shè)定五秒后啟動(dòng)鬧鐘 但是每次都是我設(shè)置完鬧鐘之后立馬就啟動(dòng)了。
后來我發(fā)現(xiàn)問題出在第二個(gè)參數(shù)上 我對(duì)他的理解是錯(cuò)誤的
我之前以為它是“延遲”時(shí)間,而實(shí)際它是“啟動(dòng)”時(shí)間。
要理解這個(gè)參數(shù)還要看type這個(gè)參數(shù)
public static final int ELAPSED_REALTIME //當(dāng)系統(tǒng)進(jìn)入睡眠狀態(tài)時(shí),這種類型的鬧鈴不會(huì)喚醒系統(tǒng)。直到系統(tǒng)下次被喚醒才傳遞它,該鬧鈴所用的時(shí)間是相對(duì)時(shí)間,是從系統(tǒng)啟動(dòng)后開始計(jì)時(shí)的,包括睡眠時(shí)間,可以通過調(diào)用SystemClock.elapsedRealtime()獲得。系統(tǒng)值是3 (0x00000003)。 public static final int ELAPSED_REALTIME_WAKEUP //能喚醒系統(tǒng),用法同ELAPSED_REALTIME,系統(tǒng)值是2 (0x00000002) 。 public static final int RTC //當(dāng)系統(tǒng)進(jìn)入睡眠狀態(tài)時(shí),這種類型的鬧鈴不會(huì)喚醒系統(tǒng)。直到系統(tǒng)下次被喚醒才傳遞它,該鬧鈴所用的時(shí)間是絕對(duì)時(shí)間,所用時(shí)間是UTC時(shí)間,可以通過調(diào)用 System.currentTimeMillis()獲得。系統(tǒng)值是1 (0x00000001) 。 public static final int RTC_WAKEUP //能喚醒系統(tǒng),用法同RTC類型,系統(tǒng)值為 0 (0x00000000) 。
它大致分為兩種類型 一種是相對(duì)時(shí)間 一種是絕對(duì)時(shí)間。
所以,根據(jù)使用的類型不同 triggerAtTime設(shè)置也有所不同。
如果使用ELAPSED_REALTIME_WAKEUP類型 應(yīng)該調(diào)用SystemClock.elapsedRealtime()獲取相對(duì)時(shí)間在加上你設(shè)定的延遲時(shí)間。
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+5000, sender);
如果使用RTC_WAKEUP類型 應(yīng)該調(diào)用System.currentTimeMillis()獲取從1970.1.1號(hào)以來的時(shí)間在加上你設(shè)定的延遲時(shí)間
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5000, sender);
setRepeating方法有4個(gè)參數(shù),這些參數(shù)的含義如下:
type:表示警報(bào)類型,一般可以取的值是AlarmManager.RTC和AlarmManager.RTC_WAKEUP。如果將type參數(shù)值設(shè)為AlarmManager.RTC,表示是一個(gè)正常的定時(shí)器,如果將type參數(shù)值設(shè)為AlarmManager.RTC_WAKEUP,除了有定時(shí)器的功能外,還會(huì)發(fā)出警報(bào)聲(例如,響鈴、震動(dòng))。
triggerAtTime:第1次運(yùn)行時(shí)要等待的時(shí)間,也就是執(zhí)行延遲時(shí)間,單位是毫秒。
interval:表示執(zhí)行的時(shí)間間隔,單位是毫秒。
operation:一個(gè)PendingIntent對(duì)象,表示到時(shí)間后要執(zhí)行的操作。PendingIntent與Intent類似,可以封裝Activity、BroadcastReceiver和Service。但與Intent不同的是,PendingIntent可以脫離應(yīng)用程序而存在。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android側(cè)滑菜單和輪播圖之滑動(dòng)沖突問題
這篇文章主要介紹了Android側(cè)滑菜單和輪播圖之滑動(dòng)沖突問題,需要的朋友可以參考下2017-06-06
Android中三種onClick的實(shí)現(xiàn)方式與對(duì)比
這篇文章主要為大家詳細(xì)介紹了Android中三種onClick的實(shí)現(xiàn)方式以及詳細(xì)對(duì)比,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
Android彈出DatePickerDialog并獲取值的方法
這篇文章主要為大家詳細(xì)介紹了Android彈出DatePickerDialog并獲取值的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
Android照片墻應(yīng)用實(shí)現(xiàn) 再多的圖片也不怕崩潰
這篇文章主要為大家詳細(xì)介紹了Android照片墻應(yīng)用實(shí)現(xiàn),再多的圖片也不怕崩潰,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android ProgressDialog進(jìn)度條使用詳解
這篇文章主要對(duì)Android開發(fā)之ProgressDialog讀取文件進(jìn)度進(jìn)行解析,感興趣的朋友可以參考一下2016-02-02
Android使用viewpager實(shí)現(xiàn)自動(dòng)無限輪播圖
這篇文章主要介紹了Android使用viewpager實(shí)現(xiàn)自動(dòng)無限輪播圖效果,實(shí)現(xiàn)方法大概有兩種,一種是viewpager+作為游標(biāo)的點(diǎn) 。另外一種是重寫viewpager,具體實(shí)現(xiàn)過程大家參考下本文2018-06-06
Android 使用fast-verification實(shí)現(xiàn)驗(yàn)證碼填寫功能的實(shí)例代碼
這篇文章主要介紹了Android 使用fast-verification實(shí)現(xiàn)驗(yàn)證碼填寫功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Android 中在有序廣播中添加自定義權(quán)限的實(shí)例
這篇文章主要介紹了Android 中在有序廣播中添加自定義權(quán)限的實(shí)例的相關(guān)資料,這里對(duì)有序廣播的用法進(jìn)行了詳細(xì)介紹并附有簡(jiǎn)單實(shí)例,需要的朋友可以參考下2017-07-07

