生活随笔
收集整理的這篇文章主要介紹了
HarmonyOS之后台代理定时提醒的功能使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、簡(jiǎn)介
在應(yīng)用開發(fā)時(shí),可以調(diào)用后臺(tái)代理提醒類 ReminderRequest 去創(chuàng)建定時(shí)提醒,包括倒計(jì)時(shí)、日歷、鬧鐘三種提醒類型。 使用后臺(tái)代理提醒能力后,應(yīng)用可以被凍結(jié)或退出,計(jì)時(shí)和彈出提醒的功能將被后臺(tái)系統(tǒng)服務(wù)代理。
二、API 說明
ReminderRequest 涉及的基礎(chǔ)類包括 ReminderHelper、ReminderRequestTimer、ReminderRequestCalendar、ReminderRequestAlarm,基礎(chǔ)類之間的關(guān)系如下圖所示:
ReminderHelper:封裝了發(fā)布、取消提醒類通知的方法。ReminderHelper 主要接口:
接口名描述 public static int publishReminder(ReminderRequest reminderReq) throws RemoteException, ReminderManager.AppLimitExceedsException, ReminderManager.SysLimitExceedsException 發(fā)布一個(gè)定時(shí)提醒類通知 ReminderManager.AppLimitExceedsException 系統(tǒng)中保存的當(dāng)前應(yīng)用有效的提醒個(gè)數(shù)超出最大限制數(shù)量30個(gè)時(shí)拋出(不包括已經(jīng)超時(shí),即后續(xù)不會(huì)再提醒的提醒實(shí)例) ReminderManager.SysLimitExceedsException 系統(tǒng)中保存的整個(gè)系統(tǒng)有效的提醒個(gè)數(shù)超出最大限制數(shù)量2000個(gè)時(shí)拋出(不包括已經(jīng)超時(shí),即后續(xù)不會(huì)再提醒的提醒實(shí)例) public static void addNotificationSlot(NotificationSlot slot) throws RemoteException 注冊(cè)一個(gè)提醒類需要使用的NotificationSlot public static void cancelReminder(int reminderId) throws RemoteException 取消一個(gè)指定的提醒類通知。(reminderId從publishReminder的返回值獲取) public static void removeNotificationSlot(String slotId) throws RemoteException 刪除一個(gè)slot實(shí)例 public static List getValidReminders() throws RemoteException 獲取當(dāng)前應(yīng)用設(shè)置的所有有效的提醒 public static void cancelAllReminders() throws RemoteException 取消當(dāng)前應(yīng)用設(shè)置的所有提醒
ReminderRequest:后臺(tái)代理提醒類基類,封裝了提醒相關(guān)的屬性查詢和設(shè)置的操作。ReminderRequest 主要接口:
接口名描述 public long getRingDuration() 獲取設(shè)置的提醒時(shí)長(zhǎng),單位秒。如設(shè)置的開始響鈴后的響鈴時(shí)長(zhǎng) public int getSnoozeTimes() 獲取設(shè)置的延遲提醒次數(shù) public long getTimeInterval() 獲取設(shè)置的延遲提醒間隔 public ReminderRequest setRingDuration(long ringDurationInSeconds) 設(shè)置提醒時(shí)長(zhǎng),單位秒,如設(shè)置開始響鈴后的響鈴時(shí)長(zhǎng) public ReminderRequest setSnoozeTimes(int snoozeTimes) 設(shè)置延遲提醒的次數(shù)。(倒計(jì)時(shí)設(shè)置延遲提醒無效) public ReminderRequest setTimeInterval(long timeIntervalInSeconds) 設(shè)置延遲提醒的時(shí)間間隔。(倒計(jì)時(shí)設(shè)置延遲提醒無效) public ReminderRequest setActionButton(String title, int type) 在提醒彈出的通知界面中添加NotificationActionButton public ReminderRequest setIntentAgent(String pkgName, String abilityName) 設(shè)置點(diǎn)擊通知信息后需要跳轉(zhuǎn)的目標(biāo)包的信息 public ReminderRequest setMaxScreenIntentAgent(String pkgName, String abilityName) 設(shè)置提醒到達(dá)時(shí)跳轉(zhuǎn)的目標(biāo)包。如果設(shè)備正在使用中,則彈出一個(gè)通知框 public String getTitle() 獲取提醒的標(biāo)題 public ReminderRequest setTitle(String title) 設(shè)置提醒的標(biāo)題 public String getContent() 獲取提醒的內(nèi)容 public ReminderRequest setContent(String content) 設(shè)置提醒的內(nèi)容 public String getExpiredContent() 獲取提醒“過期”時(shí)顯示的擴(kuò)展內(nèi)容 public ReminderRequest setExpiredContent(String expiredContent) 設(shè)置提醒“過期”時(shí)顯示的擴(kuò)展內(nèi)容 public String getSnoozeContent() 獲取提醒“再響”時(shí)顯示的擴(kuò)展內(nèi)容 public ReminderRequest setSnoozeContent(String snoozeContent) 設(shè)置提醒“再響”時(shí)顯示的擴(kuò)展內(nèi)容 public int getNotificationId() 獲取提醒使用的notificationRequest的id,參見NotificationRequest.setNotificationId(int id) public ReminderRequest setNotificationId(int notificationId) 設(shè)置提醒使用的notificationRequest的id public String getSlotId() 獲取提醒使用的slot id public String SetSlotId(String slotId) 設(shè)置提醒使用的slot id
ReminderRequestTimer:提醒類子類,用于倒計(jì)時(shí)提醒。ReminderRequestTimer 主要接口:
接口名描述 public ReminderRequestTimer(long triggerTimeInSeconds) 創(chuàng)建一個(gè)倒計(jì)時(shí)提醒實(shí)例。經(jīng)過指定時(shí)間后觸發(fā)提醒
ReminderRequestCalendar:提醒類子類,用于日歷類提醒。可以指定提醒時(shí)間精確為:年月日時(shí)分,可以指定哪些月份的哪些天的同一時(shí)間重復(fù)提醒。ReminderRequestCalendar 主要接口:
接口名描述 public ReminderRequestCalendar(LocalDateTime dateTime, int[] repeatMonths, int[] repeatDays) 創(chuàng)建一個(gè)日歷類提醒實(shí)例,在指定的時(shí)間觸發(fā)提醒
ReminderRequestAlarm:提醒類子類,用于鬧鐘類提醒,可以指定幾點(diǎn)幾分提醒,或者每周哪幾天指定時(shí)間提醒。ReminderRequestAlarm 主要接口:
接口名描述 public ReminderRequestAlarm(int hour, int minute, int[] daysOfWeek) 創(chuàng)建一個(gè)鬧鐘類提醒實(shí)例,在指定的時(shí)間觸發(fā)提醒
三、后臺(tái)代理定時(shí)提醒
聲明使用權(quán)限:使用后臺(tái)代理提醒需要在配置文件中聲明需要此權(quán)限:
"reqPermissions" : [ { "name" : "ohos.permission.PUBLISH_AGENT_REMINDER" } ]
NotificationSlot slot
= new
NotificationSlot ( "slot_id" , "slot_name" , NotificationSlot
. LEVEL_HIGH
) ; slot
. setEnableLight ( false
) ; slot
. setEnableVibration ( true
) ; try
{ ReminderHelper
. addNotificationSlot ( slot
) ; } catch ( RemoteException e
) { e
. printStackTrace ( ) ; } int [ ] repeatDay
= { } ; ReminderRequest reminder
= new
ReminderRequestAlarm ( 10 , 30 , repeatDay
) ; reminder
. setTitle ( "set title here" ) . setContent ( "set content here" ) ; reminder
. setSnoozeTimes ( 1 ) . setTimeInterval ( 5 * 60 ) . setRingDuration ( 10 ) ; reminder
. setIntentAgent ( "com.ohos.aaa" , FirstAbility
. class
. getName ( ) ) ; reminder
. setActionButton ( "snooze" , ReminderRequest
. ACTION_BUTTON_TYPE_SNOOZE
) . setActionButton ( "close" , ReminderRequest
. ACTION_BUTTON_TYPE_CLOSE
) ; try
{ ReminderHelper
. publishReminder ( reminder
) ; } catch ( ReminderManager
. AppLimitExceedsException e
) { e
. printStackTrace ( ) ; } catch ( ReminderManager
. SysLimitExceedsException e
) { e
. printStackTrace ( ) ; } catch ( RemoteException e
) { e
. printStackTrace ( ) ; }
創(chuàng)建一個(gè)倒計(jì)時(shí)提醒示例如下:
ReminderRequest reminderRequestTimer
= new
ReminderRequestTimer ( 60 ) ;
創(chuàng)建一個(gè)一次性日歷提醒的示例如下:
int [ ] repeatMonths
= { } ; int [ ] repeatDays
= { } ; ReminderRequestCalendar reminderRequestCalendar
= new
ReminderRequestCalendar ( LocalDateTime
. of ( 2021 , 3 , 2 , 14 , 30 ) , repeatMonths
, repeatDays
) ;
創(chuàng)建一個(gè)重復(fù)的日歷提醒的示例如下:
int [ ] repeatMonths
= { 3 , 5 } ; int [ ] repeatDaysOfMonth
= { 9 , 15 } ; ReminderRequestCalendar reminderRequestCalender
= new
ReminderRequestCalendar ( LocalDateTime
. of ( 2021 , 3 , 2 , 14 , 30 ) , repeatMonths
, repeatDaysOfMonth
) ; reminderRequestCalender
. setTimeInterval ( 10 * 60 ) ;
創(chuàng)建一個(gè)一次性鬧鐘提醒的示例如下:
int [ ] repeatDay
= { } ; ReminderRequest reminderRequestAlarm
= new
ReminderRequestAlarm ( 13 , 59 , repeatDay
) ;
創(chuàng)建一個(gè)重復(fù)的鬧鐘提醒的示例如下:
int [ ] repeatDay
= { 1 , 2 , 3 , 4 } ; ReminderRequest reminderRequestAlarm
= new
ReminderRequestAlarm ( 13 , 59 , repeatDay
) ;
創(chuàng)建一個(gè)用于延遲提醒的 ActionButton 界面的示例如下:
reminderRequest
. setActionButton ( "snooze" , ReminderRequest
. ACTION_BUTTON_TYPE_SNOOZE
) ;
創(chuàng)建一個(gè)用于關(guān)閉提醒的 ActionButton 界面的示例如下:
reminderRequest
. setActionButton ( "close" , ReminderRequest
. ACTION_BUTTON_TYPE_CLOSE
) ;
注意: notificationId 相同的不同 NotificationRequest 請(qǐng)求,在通知欄展示的內(nèi)容會(huì)被覆蓋,對(duì)于提醒來說,可能不希望被覆蓋,開發(fā)時(shí)可以注意設(shè)置不同的 notificationId; 倒計(jì)時(shí)不支持持久化,系統(tǒng)重啟后,所有倒計(jì)時(shí)失效。
總結(jié)
以上是生活随笔 為你收集整理的HarmonyOS之后台代理定时提醒的功能使用 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。