定時通知在我們的日常生活中是非常實用的,不管是網站還是APP都将這個功能加入到項目中,這個功能之是以收到大家的喜愛,是因為可以在特定的時間提醒我們該做什麼了,在生活中給大家帶來了很大的便利,今天愛站技術頻道帶給大家Android中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:
最後别忘了在清單裡注冊廣播。
附件
常量:
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;
public class AlarmTimer {
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:鬧鐘響應動作。
}
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);
}
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);
}
}
上述介紹的Android中AlarmManager+Notification實作定時通知提醒功能,就為大家介紹到這裡了,更多的技術知識盡在js.aizhan.com。