From e62416e5ce35275ce5d01332c3805fc19e51ce3e Mon Sep 17 00:00:00 2001 From: Yang Yongquan Date: Sun, 13 Sep 2026 22:02:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E4=BA=86=E6=8C=81=E7=BB=AD=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=EF=BC=8C=E7=94=B3=E8=AF=B7=E4=BA=86=E5=B8=B8=E9=A9=BB?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=E6=A0=8F=E6=9D=83=E9=99=90=EF=BC=8C=E5=9C=A8?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E4=B8=AD=E5=BC=80=E5=90=AF=E5=90=8E=EF=BC=8C?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E5=B8=B8=E9=A9=BB=E9=80=9A=E7=9F=A5=E6=A0=8F?= =?UTF-8?q?=EF=BC=8C=E8=BF=9B=E8=A1=8C=E5=90=8E=E5=8F=B0=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E3=80=82=E5=A6=82=E6=9E=9C=E4=B8=8D=E9=9C=80=E8=A6=81=EF=BC=8C?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E5=9C=A8=E8=AE=BE=E7=BD=AE=E4=B8=AD=E5=85=B3?= =?UTF-8?q?=E9=97=AD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/ets/common/BackgroundSyncService.ets | 23 ++++++++++++++----- .../main/ets/entryability/EntryAbility.ets | 11 +++++++++ entry/src/main/ets/pages/SettingsPage.ets | 4 +++- entry/src/main/module.json5 | 3 +++ 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/entry/src/main/ets/common/BackgroundSyncService.ets b/entry/src/main/ets/common/BackgroundSyncService.ets index 2be7a3d..b2a7b8c 100644 --- a/entry/src/main/ets/common/BackgroundSyncService.ets +++ b/entry/src/main/ets/common/BackgroundSyncService.ets @@ -12,7 +12,7 @@ import { LogUtil } from './LogUtil'; const WORK_ID: number = 1001; export class BackgroundSyncService { - /** 申请长时任务(DATA_TRANSFER):申请成功后进程在后台不被挂起 */ + /** 申请长时任务(DATA_TRANSFER):申请成功后进程在后台不被挂起,失败抛错由调用方提示用户 */ static async startContinuousTask(context: common.UIAbilityContext): Promise { try { const info: wantAgent.WantAgentInfo = { @@ -30,6 +30,7 @@ export class BackgroundSyncService { } catch (err) { const e = err as BusinessError; LogUtil.write(`长时任务申请失败: ${e.code} - ${e.message}`); + throw new Error(`长时任务申请失败(错误码 ${e.code})`); } } @@ -83,22 +84,32 @@ export class BackgroundSyncService { } } - /** App 启动时统一入口:注册延迟任务 + 按设置恢复长时任务 */ + /** App 启动时统一入口:注册延迟任务 + 按设置恢复长时任务(恢复失败静默,不影响启动) */ static async initOnLaunch(context: common.UIAbilityContext): Promise { BackgroundSyncService.scheduleDeferredSync(context); const enabled: boolean = await AppSettings.getBackgroundSync(context); if (enabled) { - await BackgroundSyncService.startContinuousTask(context); + try { + await BackgroundSyncService.startContinuousTask(context); + } catch (err) { + // 启动时恢复失败不阻塞 App:下次开关切换或重启再试 + } } } - /** 设置页开关切换 */ + /** 设置页开关切换:失败抛错由页面提示并回滚 UI */ static async setBackgroundSync(context: common.UIAbilityContext, enabled: boolean): Promise { - await AppSettings.setBackgroundSync(context, enabled); if (enabled) { + // 先申请任务,成功才落盘设置(失败时设置页回滚开关) await BackgroundSyncService.startContinuousTask(context); + await AppSettings.setBackgroundSync(context, true); } else { - await BackgroundSyncService.stopContinuousTask(context); + await AppSettings.setBackgroundSync(context, false); + try { + await BackgroundSyncService.stopContinuousTask(context); + } catch (err) { + // 停止失败(如任务本就不在运行)不影响设置保存 + } } } } diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index f974a4c..3b39239 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -1,6 +1,7 @@ import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; +import { notificationManager } from '@kit.NotificationKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { BackgroundSyncService } from '../common/BackgroundSyncService'; @@ -11,6 +12,16 @@ export default class EntryAbility extends UIAbility { hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); // 后台同步:注册延迟任务兜底 + 按设置恢复长时任务 BackgroundSyncService.initOnLaunch(this.context); + // 申请通知权限:日程提醒、后台同步常驻通知都依赖它(系统只弹一次授权框) + notificationManager.isNotificationEnabled() + .then((enabled: boolean): Promise => { + if (!enabled) { + return notificationManager.requestEnableNotification(this.context) + .catch((): void => {}); + } + return Promise.resolve(); + }) + .catch((): void => {}); } onDestroy(): void { diff --git a/entry/src/main/ets/pages/SettingsPage.ets b/entry/src/main/ets/pages/SettingsPage.ets index f8c2e9b..0c6545d 100644 --- a/entry/src/main/ets/pages/SettingsPage.ets +++ b/entry/src/main/ets/pages/SettingsPage.ets @@ -234,7 +234,9 @@ struct SettingsPage { }); } catch (err) { this.backgroundSync = !value; // 失败回滚 UI - this.getUIContext().getPromptAction().showToast({ message: '设置失败,请重试' }); + const msg: string = (err as BusinessError).message !== '' + ? (err as BusinessError).message : '请重试'; + this.getUIContext().getPromptAction().showToast({ message: `开启失败:${msg}` }); } } diff --git a/entry/src/main/module.json5 b/entry/src/main/module.json5 index 7f8e471..527a1d2 100644 --- a/entry/src/main/module.json5 +++ b/entry/src/main/module.json5 @@ -12,6 +12,9 @@ { "name": "ohos.permission.INTERNET" }, + { + "name": "ohos.permission.KEEP_BACKGROUND_RUNNING" + }, { "name": "ohos.permission.PUBLISH_AGENT_REMINDER" },