Android中AlarmManager+Notification實現(xiàn)定時通知提醒功能
AlarmManager簡介
AlarmManager實質(zhì)是一個全局的定時器,是Android中常用的一種系統(tǒng)級別的提示服務,在指定時間或周期性啟動其它組件(包括Activity,Service,BroadcastReceiver)。本文將講解一下如何使用AlarmManager實現(xiàn)定時提醒功能。
鬧鐘配置
周期鬧鐘
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)
該方法用于設置周期性執(zhí)行的定時服務。type:鬧鐘類型,startTime:鬧鐘首次執(zhí)行時間,intervalTime:鬧鐘兩次執(zhí)行的間隔時間,pi:鬧鐘響應動作。
setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi)
該方法也用于設置周期定式服務,與上一種類似。不過其兩個鬧鐘執(zhí)行的間隔時間不是固定的。它相對而言更省電一些,因為系統(tǒng)可能會將幾個差不多的鬧鐘合并為一個來執(zhí)行,減少設備的喚醒次數(shù)。
intervalTime內(nèi)置變量
間隔一天: INTERVAL_DAY
間隔半天: INTERVAL_HALF_DAY
間隔15分鐘: INTERVAL_FIFTEEN_MINUTES
間隔半個小時: INTERVAL_HALF_HOUR
間隔一個小時: INTERVAL_HOUR
定時鬧鐘
//獲得系統(tǒng)提供的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:鬧鐘執(zhí)行時間,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);
設置多個鬧鐘:
若連續(xù)設置多個鬧鐘,則只有最后一個鬧鐘會生效,那么這種情況我們怎么處理呢?其實很簡單。我們可以給每個鬧鐘設置唯一的id,傳入getBroadcast()第二個參數(shù)。在這里我是每設置一個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);
在取消鬧鐘時我們也可以根據(jù)這個id關閉不同的鬧鐘。
參數(shù)詳解
type:鬧鐘類型
ELAPSED_REALTIME:在指定的延時過后,發(fā)送廣播,但不喚醒設備(鬧鐘在睡眠狀態(tài)下不可用)。如果在系統(tǒng)休眠時鬧鐘觸發(fā),它將不會被傳遞,直到下一次設備喚醒。
ELAPSED_REALTIME_WAKEUP:在指定的延時過后,發(fā)送廣播,并喚醒設備(即使關機也會執(zhí)行operation所對應的組件) 。延時是要把系統(tǒng)啟動的時間SystemClock.elapsedRealtime()算進去的。
RTC:指定當系統(tǒng)調(diào)用System.currentTimeMillis()方法返回的值與triggerAtTime相等時啟動operation所對應的設備(在指定的時刻,發(fā)送廣播,但不喚醒設備)。如果在系統(tǒng)休眠時鬧鐘觸發(fā),它將不會被傳遞,直到下一次設備喚醒(鬧鐘在睡眠狀態(tài)下不可用)。
RTC_WAKEUP:指定當系統(tǒng)調(diào)用System.currentTimeMillis()方法返回的值與triggerAtTime相等時啟動operation所對應的設備(在指定的時刻,發(fā)送廣播,并喚醒設備)。即使系統(tǒng)關機也會執(zhí)行operation所對應的組件。
POWER_OFF_WAKEUP:表示鬧鐘在手機關機狀態(tài)下也能正常進行提示功能,所以是5個狀態(tài)中用的最多的狀態(tài)之一,該狀態(tài)下鬧鐘也是用絕對時間,狀態(tài)值為4;不過本狀態(tài)好像受SDK版本影響,某些版本并不支持。
long intervalTime:執(zhí)行時間
鬧鐘的第一次執(zhí)行時間,以毫秒為單位,可以自定義時間,不過一般使用當前時間。需要注意的是,本屬性與第一個屬性(type)密切相關,如果第一個參數(shù)對應的鬧鐘使用的是相對時間(ELAPSED_REALTIME和ELAPSED_REALTIME_WAKEUP),那么本屬性就得使用相對時間(相對于系統(tǒng)啟動時間來說),比如當前時間就表示為:SystemClock.elapsedRealtime();如果第一個參數(shù)對應的鬧鐘使用的是絕對時間(RTC、RTC_WAKEUP、POWER_OFF_WAKEUP),那么本屬性就得使用絕對時間,比如當前時間就表示為:System.currentTimeMillis()
long startTime:間隔時間
對于周期定時方式來說,存在本屬性,表示兩次鬧鐘執(zhí)行的間隔時間,也是以毫秒為單位。
PendingIntent pi:執(zhí)行動作
是鬧鐘的執(zhí)行動作,比如發(fā)送一個廣播、給出提示等等。PendingIntent是Intent的封裝類。需要注意的是,如果是通過啟動服務來實現(xiàn)鬧鐘提示的話,PendingIntent對象的獲取就應該采用Pending.getService(Context c,int i,Intent intent,int j)方法;如果是通過廣播來實現(xiàn)鬧鐘提示的話,PendingIntent對象的獲取就應該采用PendingIntent.getBroadcast(Context c,int i,Intent intent,int j)方法;如果是采用Activity的方式來實現(xiàn)鬧鐘提示的話,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) // 設置狀態(tài)欄中的小圖片,尺寸一般建議在24×24 .setLargeIcon(bitmap) // 這里也可以設置大圖標 .setTicker("親情日歷") // 設置顯示的提示文字 .setContentTitle("親情日歷") // 設置顯示的標題 .setContentText("您有日記提醒哦") // 消息的詳細內(nèi)容 .setContentIntent(pendingIntent) // 關聯(lián)PendingIntent .setNumber(1) // 在TextView的右方顯示的數(shù)字,可以在外部定義一個變量,點擊累加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:鬧鐘首次執(zhí)行時間,param1:鬧鐘兩次執(zhí)行的間隔時間,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); } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android中通過Notification&NotificationManager實現(xiàn)消息通知
- Android編程實現(xiàn)google消息通知功能示例
- Android之開發(fā)消息通知欄
- Android消息通知欄的實現(xiàn)方法介紹
- Android自定義Notification添加點擊事件
- Android 中Notification彈出通知實現(xiàn)代碼
- Android編程使用Service實現(xiàn)Notification定時發(fā)送功能示例
- Android 通知使用權(NotificationListenerService)的使用
- android使用NotificationListenerService監(jiān)聽通知欄消息
- Android消息通知Notification常用方法(發(fā)送消息和接收消息)
相關文章
詳解Android中使用OkHttp發(fā)送HTTP的post請求的方法
OkHttp(github.com/square/okhttp)是近來人氣迅速攀升的一款第三方安卓HTTP支持包,這里我們就來詳解Android中使用OkHttp發(fā)送HTTP的post請求的方法2016-07-07Flutter中跨組件數(shù)據(jù)傳遞的方法總結(jié)
Flutter中的數(shù)據(jù)傳遞一般包括:父->子,子->父,父->父,也就是說嵌套時的傳遞以及跨頁面的傳遞,本文整理了三種我們通常使用的方法,需要的可以參考一下2023-06-06詳解Android 利用Iptables實現(xiàn)網(wǎng)絡黑白名單(防火墻)
這篇文章主要介紹了詳解Android 利用Iptables實現(xiàn)網(wǎng)絡黑白名單(防火墻),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08Android 關于ExpandableListView去掉里頭分割線的方法
下面小編就為大家?guī)硪黄狝ndroid 關于ExpandableListView去掉里頭分割線的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12Android學習之使用SharedPreferences存儲應用程序數(shù)據(jù)
這篇文章主要為大家詳細介紹了Android學習之使用SharedPreferences保存應用程序數(shù)據(jù)的相關資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05實例詳解android studio如何導入.so文件的方法
通過實例給大家詳細講解了如何在android studio如何導入.so文件以及中間遇到的問題解決辦法,需要的讀者們可以仔細學習一下。2017-12-12