自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HarmonyOS - 實現(xiàn)消息定時提醒

系統(tǒng) OpenHarmony
對于提醒類ReminderRequest的3個子類來說,ReminderRequestTimer用于倒計時提醒,ReminderRequestAlarm用于鬧鐘類提醒,ReminderRequestCalendar類用于日歷類提醒。

??想了解更多關于開源的內容,請訪問:??

??51CTO 開源基礎軟件社區(qū)??

??https://ost.51cto.com??

前言

在一些應用中,需要通過時間設置提醒用戶操作,如鬧鐘,象棋步數(shù)倒計時等。

設計思路:將對時間的獲取與響應的結果拆分開,時間的獲取可以有倒計時與直接設計時間。響應結果可以是:震動,閃爍,通知欄,鈴聲。

項目涵蓋了IntetAgent,通知,以及JS FA調用PA等知識點,項目應用到了前面同事編寫的計時器組件??#夏日挑戰(zhàn)賽# HarmonyOS - 自定義組件之計時器??。

效果展示

如果需要展示震動和彈出效果,需要通過物理機進入通知管理,勾選橫幅通知和鎖屏通知并且修改震動為其他震動方式。

倒計時提醒如下圖:

#夏日挑戰(zhàn)賽# HarmonyOS - 實現(xiàn)消息定時提醒-開源基礎軟件社區(qū)


鬧鐘類提醒如下圖:

#夏日挑戰(zhàn)賽# HarmonyOS - 實現(xiàn)消息定時提醒-開源基礎軟件社區(qū)


實現(xiàn)步驟

1、聲明權限

"reqPermissions": [  {"name": "ohos.permission.PUBLISH_AGENT_REMINDER"  }]

2、構建前端頁面

前端頁面主要是獲取設置的倒計時以及設置的鬧鐘類的時間,封裝進一個對象ActionData通過調用接口傳遞給PA處理。

主要分為兩部分:一部分是倒計時提醒界面,一部分是鬧鐘類提醒界面。

在編寫頁面的時候遇到了一個問題:由于初步編寫hml頁面,不知道如何實現(xiàn)input輸入框的雙向綁定,后面請教后解決:通過change事件來觸發(fā)。

hml代碼如下:

<element name="countDown" src="../countDown/countDown.hml">
</element>
<div class="container">
<!--倒計時功能區(qū)域-->
<div class="box">
<text>倒計時: {{ leftTime }} s</text>
<input type="text" id="countDown" onchange="changeCountDown"></input>
<countDown time="{{ leftTime }}">
</countDown>
<div>
<button class="button" value="設置" onclick="alarmCountdown"></button>
</div>
</div>
<!--定時功能區(qū)域-->
<div class="box">
<form>
<text>時:</text>
<input type="text" id="hour" onchange="changeHour"></input>
<text>分:</text>
<input type="text" id="minute" onchange="changeMinute"></input>
<div>
<button class="button" value="設置" onclick="alarmClock"></button>
<button class="button" value="取消" onclick="cancel"></button>
</div>
</form>
</div>
</div>

主要js代碼如下:

通過不同的命令實現(xiàn)不同的調用。

//雙向綁定實現(xiàn)
changeHour(event){
this.hour=event.value;
},
changeMinute(event){
this.minute=event.value
},
changeCountDown(event){
this.countDown=event.value
},
//鬧鐘類提醒
alarmClock:function(){
this.notification(0x1003);
},
//倒計時提醒
alarmCountdown:function(){
this.leftTime=0;//重置倒計時的讀秒
this.leftTime=this.countDown;//同步倒計時讀秒
this.notification(0x1002);
},
//取消提醒
cancel:function(){
this.notification(0x1001);
},
//初始化action數(shù)據(jù)
initAction: function (code) {
var actionData = {hour:this.hour,minute:this.minute,countDown:this.countDown};
actionData.notify = "this actionData form JS ";
var action = {};
action.bundleName = "com.chinasoft.reminder";
action.abilityName = "ReminderAbility";
action.messageCode = code;
action.data = actionData;
action.abilityType = 1;
action.syncOption = 0;
return action;
},
//調用PA接口
notification: async function(code) {
try {
var action = this.initAction(code);
var result = await FeatureAbility.callAbility(action);
this.showToast(result);
} catch (pluginError) {
console.error("startNotification : Plugin Error = " + pluginError);
}
},

3、設置ReminderRequest類

(1)設置通知插槽NotificationSlot

這里有個坑,需要手動設置手機的通知管理的通知鈴聲,打開震動才有震動效果,打開橫幅通知才有通知彈出效果。設置如下圖:

#夏日挑戰(zhàn)賽# HarmonyOS - 實現(xiàn)消息定時提醒-開源基礎軟件社區(qū)


設置通知slot需要真實的手機才有震動效果,設置呼吸燈需要手機支持呼吸燈才有效果。

/*
* 設置通知slot
* */
private NotificationSlot setSlot() {
// 1. 設置渠道信息
NotificationSlot slot = new NotificationSlot("slot_id", "slot_name", NotificationSlot.LEVEL_HIGH);
slot.setDescription("slot_description");//設置NotificationSlot的描述信息。
slot.enableBypassDnd(true);//設置是否繞過系統(tǒng)的免打擾模式
slot.setEnableLight(false);//設置收到通知時是否開啟呼吸燈,前提是當前硬件支持呼吸燈。
slot.setEnableVibration(true);//設置收到通知時是否使能振動。
slot.setLedLightColor(123456);
return slot;
}
// 2. 向代理服務添加渠道對象
ReminderHelper.addNotificationSlot(setSlot());

(2)創(chuàng)建提醒類對象

ReminderRequest分為3個子類:1.ReminderRequestTimer ,2.ReminderRequestCalendar ,3.ReminderRequestAlarm。

ReminderRequestTimer 對象創(chuàng)建。

用于倒計時提醒,需要傳遞進一個倒計時的秒的參數(shù)。這個數(shù)據(jù)是從前端頁面?zhèn)鬟^來的。

ReminderRequest reminderRequestTimer = new ReminderRequestTimer(countDown);

ReminderRequestAlarm對象創(chuàng)建。

用于鬧鐘類提醒,需要傳遞時間數(shù)據(jù)。需要三個參數(shù):int hour,int minute,int[] repeatDay。

ReminderRequest reminder = new ReminderRequestAlarm(hour, minute, repeatDay);

(3)設置提醒標題和內容

提醒是以通知形式來表現(xiàn)的。設置通知展示的標題和內容。

reminderRequestTimer.setTitle("倒計時").setContent("炸彈");      //設置標題和內容

(4)設置提醒時長屬性

提醒默認提醒一次,如果需要提醒能長時間提醒,進行如下設置:

reminder.setRingDuration(10);//設置提醒時長

(5)設置IntentAgent

需要設置BundleName和AbilityName。且AbilityName需要全類名(加上包名),才能實現(xiàn)頁面的跳轉。點擊提醒彈出的通知就會跳轉到對應的頁面。

reminder.setIntentAgent(BUNDLE_NAME, ABILITY_NAME);

(6)設置延時提醒,功能按鈕

延時提醒和設置延時的時間,設置延時后提醒的次數(shù)。

當提醒時會發(fā)出通知,設置延遲提醒和關閉按鈕。

reminder.setSnoozeTimes(2)     //設置延遲提醒次數(shù)
.setTimeInterval(1 * 60);//設置一分鐘,實際為5分鐘
reminder.setActionButton("延遲", ReminderRequest.ACTION_BUTTON_TYPE_SNOOZE)
.setActionButton("關閉", ReminderRequest.ACTION_BUTTON_TYPE_CLOSE);

在這里有一個坑:通過測試得,延遲提醒的最短時間為300s(5分鐘)如果設置時間小于300s則為默認的。

注意:對于倒計時提醒設置延遲提醒不起作用,只有設置提醒時長有效。

(7)發(fā)布和取消提醒

發(fā)布和取消提醒都是通過ReminderHelper這個類來完成的,取消提醒需要獲取發(fā)布提醒后返回的一個整型id來識別需要取消哪一個提醒。

int reminderId = ReminderHelper.publishReminder(reminder);//發(fā)布提醒
ReminderHelper.cancelReminder(this.reminderId);//取消提醒

總結

對于提醒類ReminderRequest的3個子類來說,ReminderRequestTimer用于倒計時提醒,ReminderRequestAlarm用于鬧鐘類提醒,ReminderRequestCalendar類用于日歷類提醒。本項目還有日歷類提醒(ReminderRequestCalendar)的功能沒有實現(xiàn),通過API可以發(fā)現(xiàn)其與鬧鐘類提醒的功能實現(xiàn)大致一樣。在運用時需要注意其中的一些細節(jié),才能正確使用。

??想了解更多關于開源的內容,請訪問:??

??51CTO 開源基礎軟件社區(qū)??

??https://ost.51cto.com??。

責任編輯:jianghua 來源: 鴻蒙社區(qū)
相關推薦

2013-03-11 10:11:21

Windows 8消息提醒

2009-12-14 13:42:13

Linux桌面Linux蘋果

2022-11-07 10:43:20

RocketMQConsumer場景

2019-02-25 15:44:16

開源RabbitMQSpring Clou

2022-07-01 17:14:03

消息通知鴻蒙

2021-08-04 10:22:27

鴻蒙HarmonyOS應用

2017-03-20 09:50:35

消息隊列架構消息

2011-09-05 17:40:40

MTK定時器

2010-06-09 15:15:34

MySQL定時執(zhí)行

2019-12-26 09:38:57

GitHub工具 wxpy

2022-01-26 07:01:00

開源社區(qū)項目

2024-12-27 08:24:55

2020-12-21 07:31:23

實現(xiàn)單機JDK

2024-11-04 16:01:01

2009-10-12 14:32:40

VB.NET實現(xiàn)定時關

2011-09-16 11:08:10

IOS應用易提醒

2024-03-22 12:10:39

Redis消息隊列數(shù)據(jù)庫

2025-01-10 08:20:00

MQ消息架構

2020-08-26 07:17:19

通信

2023-12-11 09:50:35

Linux定時器
點贊
收藏

51CTO技術棧公眾號