AlarmManager+Notification實(shí)現(xiàn)定時(shí)通知提醒功能

AlarmManager簡(jiǎn)介

AlarmManager實(shí)質(zhì)是一個(gè)全局的定時(shí)器,是Android中常用的一種系統(tǒng)級(jí)別的提示服務(wù),在指定時(shí)間或周期性啟動(dòng)其它組件(包括Activity,Service,BroadcastReceiver)。本文將講解一下如何使用AlarmManager實(shí)現(xiàn)定時(shí)提醒功能。

鬧鐘配置

周期鬧鐘

Intent intent = new Intent();
intent.setAction(GlobalValues.TIMER_ACTION_REPEATING);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5 * 1000, 3 * 1000, sender);
setRepeating(int type,long startTime,long intervalTime,PendingIntent pi)

該方法用于設(shè)置周期性執(zhí)行的定時(shí)服務(wù)。type:鬧鐘類(lèi)型,startTime:鬧鐘首次執(zhí)行時(shí)間,intervalTime:鬧鐘兩次執(zhí)行的間隔時(shí)間,pi:鬧鐘響應(yīng)動(dòng)作。

setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi)

該方法也用于設(shè)置周期定式服務(wù),與上一種類(lèi)似。不過(guò)其兩個(gè)鬧鐘執(zhí)行的間隔時(shí)間不是固定的。它相對(duì)而言更省電一些,因?yàn)橄到y(tǒng)可能會(huì)將幾個(gè)差不多的鬧鐘合并為一個(gè)來(lái)執(zhí)行,減少設(shè)備的喚醒次數(shù)。

intervalTime內(nèi)置變量

間隔一天:   INTERVAL_DAY
間隔半天:   INTERVAL_HALF_DAY
間隔15分鐘:  INTERVAL_FIFTEEN_MINUTES
間隔半個(gè)小時(shí): INTERVAL_HALF_HOUR
間隔一個(gè)小時(shí): INTERVAL_HOUR

定時(shí)鬧鐘

//獲得系統(tǒng)提供的AlarmManager服務(wù)的對(duì)象
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//Intent設(shè)置要啟動(dòng)的組件,這里啟動(dòng)廣播
Intent myIntent = new Intent();
myIntent.setAction(GlobalValues.TIMER_ACTION);
//PendingIntent對(duì)象設(shè)置動(dòng)作,啟動(dòng)的是Activity還是Service,或廣播!
PendingIntent sender = PendingIntent.getBroadcast(context, 0, myIntent,0);
//注冊(cè)鬧鐘
alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5 * 1000, sender);
set(int type,long startTime,PendingIntent pi)

該方法用于設(shè)置一次性定時(shí)服務(wù)。type:鬧鐘類(lèi)型,startTime:鬧鐘執(zhí)行時(shí)間,pi:鬧鐘響應(yīng)動(dòng)作。

取消鬧鐘

Intent myIntent = new Intent();
myIntent.setAction(GlobalValues.TIMER_ACTION);
//myIntent.setAction(GlobalValues.TIMER_ACTION_REPEATING);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, myIntent,0);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(sender);

設(shè)置多個(gè)鬧鐘:

若連續(xù)設(shè)置多個(gè)鬧鐘,則只有最后一個(gè)鬧鐘會(huì)生效,那么這種情況我們?cè)趺刺幚砟??其?shí)很簡(jiǎn)單。我們可以給每個(gè)鬧鐘設(shè)置唯一的id,傳入getBroadcast()第二個(gè)參數(shù)。在這里我是每設(shè)置一個(gè)id則自增1存入Shareprefrence里,保證id唯一性。

 //給每個(gè)鬧鐘設(shè)置不同ID防止覆蓋
int alarmId = SharedPreUtils.getInteger(context, "alarm_id", 0);
SharedPreUtils.setInteger(context, "alarm_id", ++alarmId);
PendingIntent sender = PendingIntent.getBroadcast(context, alarmId, myIntent, 0);

在取消鬧鐘時(shí)我們也可以根據(jù)這個(gè)id關(guān)閉不同的鬧鐘。

參數(shù)詳解

type:鬧鐘類(lèi)型

ELAPSED_REALTIME:
在指定的延時(shí)過(guò)后,發(fā)送廣播,但不喚醒設(shè)備(鬧鐘在睡眠狀態(tài)下不可用)。如果在系統(tǒng)休眠時(shí)鬧鐘觸發(fā),它將不會(huì)被傳遞,直到下一次設(shè)備喚醒。

ELAPSED_REALTIME_WAKEUP:
在指定的延時(shí)過(guò)后,發(fā)送廣播,并喚醒設(shè)備(即使關(guān)機(jī)也會(huì)執(zhí)行operation所對(duì)應(yīng)的組件) 。延時(shí)是要把系統(tǒng)啟動(dòng)的時(shí)間SystemClock.elapsedRealtime()算進(jìn)去的。

RTC:
指定當(dāng)系統(tǒng)調(diào)用System.currentTimeMillis()方法返回的值與triggerAtTime相等時(shí)啟動(dòng)operation所對(duì)應(yīng)的設(shè)備(在指定的時(shí)刻,發(fā)送廣播,但不喚醒設(shè)備)。如果在系統(tǒng)休眠時(shí)鬧鐘觸發(fā),它將不會(huì)被傳遞,直到下一次設(shè)備喚醒(鬧鐘在睡眠狀態(tài)下不可用)。

RTC_WAKEUP:
指定當(dāng)系統(tǒng)調(diào)用System.currentTimeMillis()方法返回的值與triggerAtTime相等時(shí)啟動(dòng)operation所對(duì)應(yīng)的設(shè)備(在指定的時(shí)刻,發(fā)送廣播,并喚醒設(shè)備)。即使系統(tǒng)關(guān)機(jī)也會(huì)執(zhí)行operation所對(duì)應(yīng)的組件。

POWER_OFF_WAKEUP:
表示鬧鐘在手機(jī)關(guān)機(jī)狀態(tài)下也能正常進(jìn)行提示功能,所以是5個(gè)狀態(tài)中用的最多的狀態(tài)之一,該狀態(tài)下鬧鐘也是用絕對(duì)時(shí)間,狀態(tài)值為4;不過(guò)本狀態(tài)好像受SDK版本影響,某些版本并不支持。

long intervalTime:執(zhí)行時(shí)間

鬧鐘的第一次執(zhí)行時(shí)間,以毫秒為單位,可以自定義時(shí)間,不過(guò)一般使用當(dāng)前時(shí)間。需要注意的是,本屬性與第一個(gè)屬性(type)密切相關(guān)。

如果第一個(gè)參數(shù)對(duì)應(yīng)的鬧鐘使用的是相對(duì)時(shí)間(ELAPSED_REALTIME和ELAPSED_REALTIME_WAKEUP),那么本屬性就得使用相對(duì)時(shí)間(相對(duì)于系統(tǒng)啟動(dòng)時(shí)間來(lái)說(shuō)),比如當(dāng)前時(shí)間就表示為:SystemClock.elapsedRealtime();

如果第一個(gè)參數(shù)對(duì)應(yīng)的鬧鐘使用的是絕對(duì)時(shí)間(RTC、RTC_WAKEUP、POWER_OFF_WAKEUP),那么本屬性就得使用絕對(duì)時(shí)間,比如當(dāng)前時(shí)間就表示為:System.currentTimeMillis()

long startTime:間隔時(shí)間

對(duì)于周期定時(shí)方式來(lái)說(shuō),存在本屬性,表示兩次鬧鐘執(zhí)行的間隔時(shí)間,也是以毫秒為單位。

PendingIntent pi:執(zhí)行動(dòng)作

是鬧鐘的執(zhí)行動(dòng)作,比如發(fā)送一個(gè)廣播、給出提示等等。PendingIntent是Intent的封裝類(lèi),需要注意的是:

  • 如果是通過(guò)啟動(dòng)服務(wù)來(lái)實(shí)現(xiàn)鬧鐘提示的話,PendingIntent對(duì)象的獲取就應(yīng)該采用Pending.getService(Context c,int i,Intent intent,int j)方法。
  • 如果是通過(guò)廣播來(lái)實(shí)現(xiàn)鬧鐘提示的話,PendingIntent對(duì)象的獲取就應(yīng)該采用PendingIntent.getBroadcast(Context c,int i,Intent intent,int j)方法。
  • 如果是采用Activity的方式來(lái)實(shí)現(xiàn)鬧鐘提示的話,PendingIntent對(duì)象的獲取就應(yīng)該采用PendingIntent.getActivity(Context c,int i,Intent intent,int j)方法。
  • 如果這三種方法錯(cuò)用了的話,雖然不會(huì)報(bào)錯(cuò),但是看不到鬧鐘提示效果。

廣播配置

新建鬧鐘BroadCastReceiver:

public class AlarmReceiver extends BroadcastReceiver {
    private NotificationManager m_notificationMgr = null;
    private static final int NOTIFICATION_FLAG = 3;
    @Override
    public void onReceive(Context context, Intent intent) {
        m_notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVIC
        if (intent.getAction().equals(GlobalValues.TIMER_ACTION_REPEATING)) {
            Log.e("alarm_receiver", "周期鬧鐘");
        } else if (intent.getAction().equals(GlobalValues.TIMER_ACTION)) {
            Log.e("alarm_receiver", "定時(shí)鬧鐘");
                        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.logo);
            Intent intent1 = new Intent(context, WriteDiaryActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent1, 0);
            Notification notify = new Notification.Builder(context)
                    .setSmallIcon(R.drawable.logo) // 設(shè)置狀態(tài)欄中的小圖片,尺寸一般建議在24×24
                    .setLargeIcon(bitmap) // 這里也可以設(shè)置大圖標(biāo)
                    .setTicker("親情日歷") // 設(shè)置顯示的提示文字
                    .setContentTitle("親情日歷") // 設(shè)置顯示的標(biāo)題
                    .setContentText("您有日記提醒哦") // 消息的詳細(xì)內(nèi)容
                    .setContentIntent(pendingIntent) // 關(guān)聯(lián)PendingIntent
                    .setNumber(1) // 在TextView的右方顯示的數(shù)字,可以在外部定義一個(gè)變量,點(diǎn)擊累加setNumber(count),這時(shí)顯示的和
                    .getNotification(); // 需要注意build()是在API level16及之后增加的,在API11中可以使用getNotificatin()來(lái)
            notify.flags |= Notification.FLAG_AUTO_CANCEL;
            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIF
            manager.notify(NOTIFICATION_FLAG, notify);
            bitmap.recycle(); //回收bitmap
        }
    }
}

注冊(cè)BroadCastReceiver:

最后別忘了在清單里注冊(cè)廣播。

<!--鬧鐘接收廣播-->
<receiver android:name=".util.service.AlarmReceiver">
    <intent-filter>
        <action android:name="com.e_eduspace.TIMER_ACTION_REPEATING" />
        <action android:name="com.e_eduspace.TIMER_ACTION" />
    </intent-filter>
</receiver>

附件

常量:

public class GlobalValues {
    // 周期性的鬧鐘
    public final static String TIMER_ACTION_REPEATING = "com.e_eduspace.TIMER_ACTION_REPEATING";
    // 定時(shí)鬧鐘
    public final static String TIMER_ACTION = "com.e_eduspace.TIMER_ACTION";
}

工具類(lèi)

package com.e_eduspace.familycalendar.util;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.prolificinteractive.materialcalendarview.CalendarDay;

/**
 * 鬧鐘定時(shí)工具類(lèi)
 *
 * @author xulei
 * @time 2016/12/13 10:03
 */

public class AlarmTimer {

    /**
     * 設(shè)置周期性鬧鐘
     *
     * @param context
     * @param firstTime
     * @param cycTime
     * @param action
     * @param AlarmManagerType 鬧鐘的類(lèi)型,常用的有5個(gè)值:AlarmManager.ELAPSED_REALTIME、
     *                         AlarmManager.ELAPSED_REALTIME_WAKEUP、AlarmManager.RTC、
     *                         AlarmManager.RTC_WAKEUP、AlarmManager.POWER_OFF_WAKEUP
     */
    public static void setRepeatingAlarmTimer(Context context, long firstTime,
                                              long cycTime, String action, int AlarmManagerType) {
        Intent myIntent = new Intent();
        myIntent.setAction(action);
        PendingIntent sender = PendingIntent.getBroadcast(context, 0, myIntent, 0);
        AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManagerType, firstTime, cycTime, sender);
        //param1:鬧鐘類(lèi)型,param1:鬧鐘首次執(zhí)行時(shí)間,param1:鬧鐘兩次執(zhí)行的間隔時(shí)間,param1:鬧鐘響應(yīng)動(dòng)作。
    }

    /**
     * 設(shè)置定時(shí)鬧鐘
     *
     * @param context
     * @param cycTime
     * @param action
     * @param AlarmManagerType 鬧鐘的類(lèi)型,常用的有5個(gè)值:AlarmManager.ELAPSED_REALTIME、
     *                         AlarmManager.ELAPSED_REALTIME_WAKEUP、AlarmManager.RTC、
     *                         AlarmManager.RTC_WAKEUP、AlarmManager.POWER_OFF_WAKEUP
     */
    public static void setAlarmTimer(Context context, long cycTime,
                                     String action, int AlarmManagerType, CalendarDay date) {
        Intent myIntent = new Intent();
        //傳遞定時(shí)日期
        myIntent.putExtra("date", date);
        myIntent.setAction(action);
        //給每個(gè)鬧鐘設(shè)置不同ID防止覆蓋
        int alarmId = SharedPreUtils.getInteger(context, "alarm_id", 0);
        SharedPreUtils.setInteger(context, "alarm_id", ++alarmId);
        PendingIntent sender = PendingIntent.getBroadcast(context, alarmId, myIntent, 0);
        AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarm.set(AlarmManagerType, cycTime, sender);
    }

    /**
     * 取消鬧鐘
     *
     * @param context
     * @param action
     */
    public static void cancelAlarmTimer(Context context, String action) {
        Intent myIntent = new Intent();
        myIntent.setAction(action);
        PendingIntent sender = PendingIntent.getBroadcast(context, 0, myIntent,0);
        AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarm.cancel(sender);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容