1. 程式人生 > >AlarmManager+Notification實現定時通知提醒功能

AlarmManager+Notification實現定時通知提醒功能

AlarmManager簡介

  AlarmManager實質是一個全域性的定時器,是Android中常用的一種系統級別的提示服務,在指定時間或週期性啟動其它元件(包括Activity,Service,BroadcastReceiver)。本文將講解一下如何使用AlarmManager實現定時提醒功能。

鬧鐘配置

週期鬧鐘

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)

  該方法用於設定週期性執行的定時服務。type:鬧鐘型別,startTime:鬧鐘首次執行時間,intervalTime:鬧鐘兩次執行的間隔時間,pi:鬧鐘響應動作。

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

  該方法也用於設定週期定式服務,與上一種類似。不過其兩個鬧鐘執行的間隔時間不是固定的。它相對而言更省電一些,因為系統可能會將幾個差不多的鬧鐘合併為一個來執行,減少裝置的喚醒次數。

intervalTime內建變數

間隔一天:   INTERVAL_DAY
間隔半天:   INTERVAL_HALF_DAY
間隔15分鐘:  INTERVAL_FIFTEEN_MINUTES
間隔半個小時: INTERVAL_HALF_HOUR
間隔一個小時: INTERVAL_HOUR

定時鬧鐘

//獲得系統提供的AlarmManager服務的物件
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//Intent設定要啟動的元件,這裡啟動廣播
Intent myIntent = new Intent();
myIntent.setAction(GlobalValues.TIMER_ACTION);
//PendingIntent物件設定動作,啟動的是Activity還是Service,或廣播!
PendingIntent sender = PendingIntent.getBroadcast(context, 0, myIntent,0);
//註冊鬧鐘
alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5 * 1000, sender);
set(int type,long startTime,PendingIntent pi)

  該方法用於設定一次性定時服務。type:鬧鐘型別,startTime:鬧鐘執行時間,pi:鬧鐘響應動作。

取消鬧鐘

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);

設定多個鬧鐘:

  若連續設定多個鬧鐘,則只有最後一個鬧鐘會生效,那麼這種情況我們怎麼處理呢?其實很簡單。我們可以給每個鬧鐘設定唯一的id,傳入getBroadcast()第二個引數。在這裡我是每設定一個id則自增1存入Shareprefrence裡,保證id唯一性。

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

  在取消鬧鐘時我們也可以根據這個id關閉不同的鬧鐘。

引數詳解

type:鬧鐘型別

ELAPSED_REALTIME:
  在指定的延時過後,傳送廣播,但不喚醒裝置(鬧鐘在睡眠狀態下不可用)。如果在系統休眠時鬧鐘觸發,它將不會被傳遞,直到下一次裝置喚醒。
ELAPSED_REALTIME_WAKEUP:
  在指定的延時過後,傳送廣播,並喚醒裝置(即使關機也會執行operation所對應的元件) 。延時是要把系統啟動的時間SystemClock.elapsedRealtime()算進去的。
RTC:
  指定當系統呼叫System.currentTimeMillis()方法返回的值與triggerAtTime相等時啟動operation所對應的裝置(在指定的時刻,傳送廣播,但不喚醒裝置)。如果在系統休眠時鬧鐘觸發,它將不會被傳遞,直到下一次裝置喚醒(鬧鐘在睡眠狀態下不可用)。
RTC_WAKEUP:
  指定當系統呼叫System.currentTimeMillis()方法返回的值與triggerAtTime相等時啟動operation所對應的裝置(在指定的時刻,傳送廣播,並喚醒裝置)。即使系統關機也會執行operation所對應的元件。
POWER_OFF_WAKEUP:
  表示鬧鐘在手機關機狀態下也能正常進行提示功能,所以是5個狀態中用的最多的狀態之一,該狀態下鬧鐘也是用絕對時間,狀態值為4;不過本狀態好像受SDK版本影響,某些版本並不支援。

long intervalTime:執行時間

  鬧鐘的第一次執行時間,以毫秒為單位,可以自定義時間,不過一般使用當前時間。需要注意的是,本屬性與第一個屬性(type)密切相關,如果第一個引數對應的鬧鐘使用的是相對時間(ELAPSED_REALTIME和ELAPSED_REALTIME_WAKEUP),那麼本屬性就得使用相對時間(相對於系統啟動時間來說),比如當前時間就表示為:SystemClock.elapsedRealtime();如果第一個引數對應的鬧鐘使用的是絕對時間(RTC、RTC_WAKEUP、POWER_OFF_WAKEUP),那麼本屬性就得使用絕對時間,比如當前時間就表示為:System.currentTimeMillis()

long startTime:間隔時間

  對於週期定時方式來說,存在本屬性,表示兩次鬧鐘執行的間隔時間,也是以毫秒為單位。

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)方法。如果這三種方法錯用了的話,雖然不會報錯,但是看不到鬧鐘提示效果。。

廣播配置

新建鬧鐘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", "定時鬧鐘");
                        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) // 設定狀態列中的小圖片,尺寸一般建議在24×24
                    .setLargeIcon(bitmap) // 這裡也可以設定大圖示
                    .setTicker("親情日曆") // 設定顯示的提示文字
                    .setContentTitle("親情日曆") // 設定顯示的標題
                    .setContentText("您有日記提醒哦") // 訊息的詳細內容
                    .setContentIntent(pendingIntent) // 關聯PendingIntent
                    .setNumber(1) // 在TextView的右方顯示的數字,可以在外部定義一個變數,點選累加setNumber(count),這時顯示的和
                    .getNotification(); // 需要注意build()是在API level16及之後增加的,在API11中可以使用getNotificatin()來
            notify.flags |= Notification.FLAG_AUTO_CANCEL;
            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIF
            manager.notify(NOTIFICATION_FLAG, notify);
            bitmap.recycle(); //回收bitmap
        }
    }
}

註冊BroadCastReceiver:

  最後別忘了在清單裡註冊廣播。

<!--鬧鐘接收廣播-->
<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";
    // 定時鬧鐘
    public final static String TIMER_ACTION = "com.e_eduspace.TIMER_ACTION";
}

工具類

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;

/**
 * 鬧鐘定時工具類
 *
 * @author xulei
 * @time 2016/12/13 10:03
 */

public class AlarmTimer {

    /**
     * 設定週期性鬧鐘
     *
     * @param context
     * @param firstTime
     * @param cycTime
     * @param action
     * @param AlarmManagerType 鬧鐘的型別,常用的有5個值: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:鬧鐘型別,param1:鬧鐘首次執行時間,param1:鬧鐘兩次執行的間隔時間,param1:鬧鐘響應動作。
    }

    /**
     * 設定定時鬧鐘
     *
     * @param context
     * @param cycTime
     * @param action
     * @param AlarmManagerType 鬧鐘的型別,常用的有5個值: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();
        //傳遞定時日期
        myIntent.putExtra("date", date);
        myIntent.setAction(action);
        //給每個鬧鐘設定不同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);
    }
}