HarmonyOS 鸿蒙Next:手表后台应用定位与运行问题,息屏停掉亮屏正常
HarmonyOS 鸿蒙Next:手表后台应用定位与运行问题,息屏停掉亮屏正常
关于HarmonyOS 鸿蒙Next:手表后台应用定位与运行问题,息屏停掉亮屏正常的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。
更多关于HarmonyOS 鸿蒙Next:手表后台应用定位与运行问题,息屏停掉亮屏正常的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于HarmonyOS 鸿蒙Next:手表后台应用定位与运行问题,息屏停掉亮屏正常的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
startContinuousTask() { let ctx = this.context as common.UIAbilityContext; let wantAgentInfo: wantAgent.WantAgentInfo = { // 点击通知后,将要执行的动作列表 // 添加需要被拉起应用的bundleName和abilityName wants: [ { bundleName: ctx.abilityInfo.bundleName, abilityName: ctx.abilityInfo.name } ], // 指定点击通知栏消息后的动作是拉起ability operationType: wantAgent.OperationType.START_ABILITY, // 使用者自定义的一个私有值 requestCode: 0, // 点击通知后,动作执行属性 wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] };
<span class="hljs-comment"><span class="hljs-comment">// 通过wantAgent模块下getWantAgent方法获取WantAgent对象</span></span> wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj: WantAgent) => { backgroundTaskManager.startBackgroundRunning(<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.context, backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj).then(() => { console.info(`Succeeded <span class="hljs-keyword"><span class="hljs-keyword">in</span></span> operationing startBackgroundRunning.`); }).catch((err: BusinessError) => { console.error(`Failed to operation startBackgroundRunning. Code is ${err.code}, message is ${err.message}`); }); });
}
stopContinuousTask() { backgroundTaskManager.stopBackgroundRunning(this.context).then(() => { console.info(
Succeeded <span class="hljs-keyword"><span class="hljs-keyword">in</span></span> operationing stopBackgroundRunning.
); }).catch((err: BusinessError) => { console.error(Failed to operation stopBackgroundRunning. Code is ${err.code}, message is ${err.message}
); }); }
build() { Column({ space: 50 }) { Text(this.message) Button() { Text(‘申请长时任务’).fontSize(25).fontWeight(FontWeight.Bold) } .type(ButtonType.Capsule) .margin({ top: 10 }) .backgroundColor(’#0D9FFB’) .width(250) .height(40) .onClick(() => { // 通过按钮申请长时任务 this.startContinuousTask(); // 此处执行具体的长时任务逻辑,如放音等。 }) } .width(‘100%’).height(‘100%’) } }
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
文档链接:长时任务-Background Tasks Kit(后台任务开发服务)-开发 | 华为开发者联盟 (huawei.com)