为了持续同步,申请了常驻通知栏权限,在设置中开启后,可以常驻通知栏,进行后台同步。如果不需要,可以在设置中关闭。
This commit is contained in:
@@ -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<void> {
|
||||
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<void> {
|
||||
BackgroundSyncService.scheduleDeferredSync(context);
|
||||
const enabled: boolean = await AppSettings.getBackgroundSync(context);
|
||||
if (enabled) {
|
||||
try {
|
||||
await BackgroundSyncService.startContinuousTask(context);
|
||||
} catch (err) {
|
||||
// 启动时恢复失败不阻塞 App:下次开关切换或重启再试
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 设置页开关切换 */
|
||||
/** 设置页开关切换:失败抛错由页面提示并回滚 UI */
|
||||
static async setBackgroundSync(context: common.UIAbilityContext, enabled: boolean): Promise<void> {
|
||||
await AppSettings.setBackgroundSync(context, enabled);
|
||||
if (enabled) {
|
||||
// 先申请任务,成功才落盘设置(失败时设置页回滚开关)
|
||||
await BackgroundSyncService.startContinuousTask(context);
|
||||
await AppSettings.setBackgroundSync(context, true);
|
||||
} else {
|
||||
await AppSettings.setBackgroundSync(context, false);
|
||||
try {
|
||||
await BackgroundSyncService.stopContinuousTask(context);
|
||||
} catch (err) {
|
||||
// 停止失败(如任务本就不在运行)不影响设置保存
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<void> => {
|
||||
if (!enabled) {
|
||||
return notificationManager.requestEnableNotification(this.context)
|
||||
.catch((): void => {});
|
||||
}
|
||||
return Promise.resolve();
|
||||
})
|
||||
.catch((): void => {});
|
||||
}
|
||||
|
||||
onDestroy(): void {
|
||||
|
||||
@@ -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}` });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,9 @@
|
||||
{
|
||||
"name": "ohos.permission.INTERNET"
|
||||
},
|
||||
{
|
||||
"name": "ohos.permission.KEEP_BACKGROUND_RUNNING"
|
||||
},
|
||||
{
|
||||
"name": "ohos.permission.PUBLISH_AGENT_REMINDER"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user