国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android AlarmManager設(shè)置鬧鐘

這篇具有很好參考價(jià)值的文章主要介紹了Android AlarmManager設(shè)置鬧鐘。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

官網(wǎng)鎮(zhèn)樓:設(shè)置重復(fù)鬧鈴時(shí)間

鬧鐘具有以下特征:

  • 它們可讓您按設(shè)定的時(shí)間和/或間隔觸發(fā) intent。
  • 您可以將它們與廣播接收器結(jié)合使用,以啟動(dòng)服務(wù)以及執(zhí)行其他操作。
  • 它們?cè)趹?yīng)用外部運(yùn)行,因此即使應(yīng)用未運(yùn)行,或設(shè)備本身處于休眠狀態(tài),您也可以使用它們來(lái)觸發(fā)事件或操作。
  • 它們可以幫助您最大限度地降低應(yīng)用的資源要求。您可以安排定期執(zhí)行操作,而無(wú)需依賴(lài)定時(shí)器或持續(xù)運(yùn)行后臺(tái)服務(wù)

簡(jiǎn)單來(lái)說(shuō),要在設(shè)定的時(shí)間執(zhí)行具體的任務(wù),可以用 AlarmManager 來(lái)實(shí)現(xiàn)。

注:這里說(shuō)的應(yīng)用通過(guò) AlarmManager 設(shè)置的鬧鐘 和 系統(tǒng)鬧鐘應(yīng)用里設(shè)置的鬧鐘 不一樣,不要混淆了。

舉個(gè)例子,
應(yīng)用通過(guò) AlarmManager 設(shè)置鬧鐘,到某個(gè)時(shí)間點(diǎn)獲取版本更新信息,簡(jiǎn)稱(chēng) A 。
系統(tǒng)鬧鐘應(yīng)用里設(shè)置的每天早上8點(diǎn)的起床鬧鐘,簡(jiǎn)稱(chēng) B 。

添加 A 時(shí),不會(huì)添加到系統(tǒng)鬧鐘應(yīng)用里,觸發(fā) A 時(shí) ,只會(huì)觸發(fā)應(yīng)用里的事件(如 Receiver),不會(huì)有系統(tǒng)鬧鐘的聲音、震動(dòng)、UI 彈窗等東西。

觸發(fā) B 時(shí),有系統(tǒng)鬧鐘的聲音、震動(dòng)、UI 彈窗等東西。

初始化

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

設(shè)置單次鬧鐘

set

30s 后喚醒設(shè)備并觸發(fā)鬧鐘
模擬器測(cè)試:

  • 設(shè)置鬧鐘后不退出應(yīng)用,鎖屏,到時(shí)間可以觸發(fā),不會(huì)喚醒屏幕;
  • 設(shè)置鬧鐘后退出應(yīng)用,并清理進(jìn)程,到時(shí)間可以觸發(fā);
  • 設(shè)置鬧鐘后退出應(yīng)用,并清理進(jìn)程,鎖屏,到時(shí)間可以觸發(fā),不會(huì)喚醒屏幕;

使用 PendingIntent 來(lái)實(shí)現(xiàn),到時(shí)間后會(huì)觸發(fā) AlarmReceiver.onReceive() ,

Intent intent = new Intent(mContext, AlarmReceiver.class);
pendingIntent1 = PendingIntent.getBroadcast(mContext, 0, intent, 0);

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + 30 * 1000,
            pendingIntent1);

setExact

setExact 和 set 用法是一樣的,差別是 setExact 比 set 時(shí)間更精確。大白話,setExact 是 30 秒后觸發(fā),set 可能 30秒后觸發(fā),也可能 35 秒后觸發(fā)。

30s 后喚醒設(shè)備并觸發(fā)鬧鐘

Intent intent = new Intent(mContext, ExactAlarmReceiver.class);
pendingIntent6 = PendingIntent.getBroadcast(mContext, 6, intent, 0);

alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + 30 * 1000,
            pendingIntent6);

設(shè)置重復(fù)鬧鐘

設(shè)置重復(fù)鬧鐘用 setInexactRepeating 和 setRepeating 。

  • setInexactRepeating :必須使用指定的時(shí)間間隔常量,此方法會(huì)同步多個(gè)不精確的重復(fù)鬧鐘,并同時(shí)觸發(fā)它們。這樣可以減少耗電量。
  • setRepeating :精確鬧鐘,可以自定義時(shí)間間隔。

setInexactRepeating

使用 PendingIntent 和 Calendar 來(lái)實(shí)現(xiàn),到時(shí)間后會(huì)觸發(fā) AlarmReceiver.onReceive() ,

在上午 10:10 左右喚醒設(shè)備并觸發(fā)鬧鐘,

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 10);

Intent intent = new Intent(mContext, AlarmReceiver.class);
pendingIntent3 = PendingIntent.getBroadcast(mContext, 0, intent, 0);

// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent3);

指定的時(shí)間間隔有 15 分鐘、半小時(shí)、一小時(shí)、半天、一天,分別對(duì)應(yīng) :

  • INTERVAL_FIFTEEN_MINUTES
  • INTERVAL_HALF_HOUR
  • INTERVAL_HOUR
  • INTERVAL_HALF_DAY
  • INTERVAL_DAY

setRepeating

使用 PendingIntent 和 Calendar 來(lái)實(shí)現(xiàn),到時(shí)間后會(huì)觸發(fā) AlarmReceiver.onReceive() ,

在上午 9:25 準(zhǔn)時(shí)喚醒設(shè)備并觸發(fā)鬧鐘,此后每 1 分鐘觸發(fā)一次:

Intent intent = new Intent(mContext, AlarmReceiver.class);
pendingIntent4 = PendingIntent.getBroadcast(mContext, 0, intent, 0);

// Set the alarm to start at 9:25 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 25);

// setRepeating() lets you specify a precise custom interval--in this case,
// 1 minutes.
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            1000 * 60 * 1, pendingIntent4);

取消鬧鐘

使用 cancel() 方法,傳入不想觸發(fā)的 PendingIntent 即可。

if (alarmManager != null) {
   if (null != pendingIntent1) {
        alarmManager.cancel(pendingIntent1);
    }
    if (null != pendingIntent2) {
       alarmManager.cancel(pendingIntent2);
    }
    if (null != pendingIntent3) {
       alarmManager.cancel(pendingIntent3);
    }
  }

獲取系統(tǒng)鬧鐘應(yīng)用里設(shè)置的鬧鐘

獲取系統(tǒng)鬧鐘應(yīng)用里設(shè)置的鬧鐘,獲取最臨近的一個(gè),返回一個(gè) AlarmClockInfo ,

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
AlarmManager.AlarmClockInfo info = alarmManager.getNextAlarmClock();

AlarmClockInfo.getTriggerTime() 方法返回的是 UTC 時(shí)間,轉(zhuǎn)為日期形式更直觀,文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-659841.html

/**
* 1692453600000 ==> Sat, 2023 08 19 22:00:00 GMT
* */
private String utc2Date(long utcTime){
    Calendar calendar= Calendar.getInstance();
    calendar.setTimeInMillis(utcTime);

    SimpleDateFormat sdfGmt = new SimpleDateFormat("EEE, yyyy MM d HH:mm:ss 'GMT'", Locale.getDefault());//星期三, 2023 08 16 22:00:00 GMT
    sdfGmt.setTimeZone(TimeZone.getDefault());
    return sdfGmt.format(calendar.getTime());
 }

到了這里,關(guān)于Android AlarmManager設(shè)置鬧鐘的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包