HarmonyOS鸿蒙Next中如何使用flutter的倒计时,等到倒计时结束后执行原生的代理提醒
HarmonyOS鸿蒙Next中如何使用flutter的倒计时,等到倒计时结束后执行原生的代理提醒 我在我鸿蒙原生里面写了个简单点代理提醒的代码他能正常执行,
const TAG: string = '[Page2]';
const DOMAIN_NUMBER: number = 0xFF00;
const REMINDER_ID: number = 100;
@Component
struct Page2 {
@State message: string = 'Reminder Controller';
private context: common.UIAbilityContext = this.getContext(this) as common.UIAbilityContext;
private reminderId: number = REMINDER_ID;
// 提醒代理配置
private targetReminderAgent: reminderAgentManager.ReminderRequestTimer = {
reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_TIMER,
triggerTimeInSeconds: 10,
actionButton: [
{
title: '关闭',
type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
}
],
wantAgent: {
pkgName: 'com.example.habit_flutter',
abilityName: 'EntryAbility'
},
maxScreenWantAgent: {
pkgName: 'com.example.habit_flutter',
abilityName: 'EntryAbility'
},
title: '定时提醒',
content: '您设置的10秒倒计时已到!',
expiredContent: '提醒已过期',
notificationId: REMINDER_ID,
slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION
};
// 生命周期函数:组件创建时自动检查权限
aboutToAppear() {
this.checkAndRequestNotificationPermission();
}
// 检查通知权限
private async checkAndRequestNotificationPermission() {
try {
const isEnabled = await notificationManager.isNotificationEnabled();
hilog.info(DOMAIN_NUMBER, TAG, `通知权限状态: ${isEnabled}`);
if (!isEnabled) {
await notificationManager.requestEnableNotification(this.context);
hilog.info(DOMAIN_NUMBER, TAG, '已请求通知权限');
}
} catch (err) {
hilog.error(DOMAIN_NUMBER, TAG, `权限检查失败: ${err.code} - ${err.message}`);
}
}
// 发布提醒
private async publishReminder() {
try {
this.reminderId = await reminderAgentManager.publishReminder(this.targetReminderAgent);
hilog.info(DOMAIN_NUMBER, TAG, `提醒发布成功,ID: ${this.reminderId}`);
this.message = '提醒已设置(10秒)';
} catch (err) {
hilog.error(DOMAIN_NUMBER, TAG, `发布失败: ${err.code} - ${err.message}`);
this.message = '设置提醒失败';
}
}
// 取消提醒
private async cancelReminder() {
try {
await reminderAgentManager.cancelReminder(this.reminderId);
hilog.info(DOMAIN_NUMBER, TAG, '提醒已取消');
this.message = '提醒已取消';
} catch (err) {
hilog.error(DOMAIN_NUMBER, TAG, `取消失败: ${err.code} - ${err.message}`);
this.message = '取消提醒失败';
}
}
build() {
Column() {
Text(this.message)
.fontSize(20)
.margin(20)
Button('设置10秒提醒')
.width(200)
.height(60)
.margin(10)
.onClick(() => {
this.publishReminder();
})
Button('取消提醒')
.width(200)
.height(60)
.margin(10)
.onClick(() => {
this.cancelReminder();
})
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
}
}
但是我不知道如何和我flutter那边的倒计时联立,当我flutter那边的倒计时结束后他就执行原生的代理提醒。
更多关于HarmonyOS鸿蒙Next中如何使用flutter的倒计时,等到倒计时结束后执行原生的代理提醒的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于HarmonyOS鸿蒙Next中如何使用flutter的倒计时,等到倒计时结束后执行原生的代理提醒的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在HarmonyOS鸿蒙Next中,使用Flutter实现倒计时并在结束后执行原生代理提醒,可以通过以下步骤实现:
-
Flutter倒计时:使用
Timer
类实现倒计时功能。例如:Timer(Duration(seconds: 10), () { // 倒计时结束后调用原生方法 _invokeNativeMethod(); });
-
原生方法调用:通过
MethodChannel
与原生代码通信。在Flutter中定义MethodChannel
:static const platform = MethodChannel('com.example.app/reminder'); void _invokeNativeMethod() async { try { await platform.invokeMethod('showReminder'); } catch (e) { print('Failed to invoke native method: $e'); } }
-
原生实现:在HarmonyOS原生代码中实现
showReminder
方法,执行代理提醒逻辑。
通过这种方式,可以在Flutter中实现倒计时,并在结束后调用原生代码执行提醒。