HarmonyOS 鸿蒙Next:手表后台应用定位与运行问题,息屏停掉亮屏正常

发布于 1周前 作者 yibo5220 来自 鸿蒙OS

HarmonyOS 鸿蒙Next:手表后台应用定位与运行问题,息屏停掉亮屏正常

看了之前的帖子,有人也出现这个问题,在当年也解决了,但现在重新出现这个问题,按照方法“可以用定位(startLocating) + 前台服务的设置(keepBackgroundRunning(NOTIFICATION_ID, request))” 也不成功,想问一下有没有人知道解决方法


关于HarmonyOS 鸿蒙Next:手表后台应用定位与运行问题,息屏停掉亮屏正常的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。

3 回复
我也遇到了 请问怎么解决呢
通过长时任务来完成
  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) =&gt; {
  backgroundTaskManager.startBackgroundRunning(<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.context,
    backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj).then(() =&gt; {
    console.info(`Succeeded <span class="hljs-keyword"><span class="hljs-keyword">in</span></span> operationing startBackgroundRunning.`);
  }).catch((err: BusinessError) =&gt; {
    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)

顶顶顶,真的没人做手表的app吗?
回到顶部