国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2019-10-22 18:25:02
字體:
來源:轉載
供稿:網友

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴彦淖尔市| 乌兰浩特市| 卢氏县| 井陉县| 阿拉善盟| 林州市| 兴业县| 拉萨市| 霞浦县| 奈曼旗| 万安县| 贵定县| 仙居县| 商洛市| 平陆县| 延津县| 仁寿县| 宿迁市| 米易县| 百色市| 昔阳县| 平山县| 丽江市| 扶风县| 岑巩县| 富锦市| 霍城县| 新巴尔虎左旗| 镇江市| 虎林市| 汉阴县| 昔阳县| 甘南县| 桐柏县| 瑞金市| 海盐县| 衡南县| 清苑县| 固原市| 蓝山县| 高碑店市|