HarmonyOS 长时任务(location任务类型失败,返回9800005)?

  1. 使用长时任务,开启location的任务类型得到错误码(9800005)。
  2. 当一个UIAbility开启长时任务(配置了多种任务类型:location,audioPlayback,dataTransfer等等),可不可以同时使用这些任务的后台功能。
阅读 427
1 个回答

日志上看The bgMode is invalid,应该是长时任务的类型配置错误。

model.json5中做如下添加:

"backgroundModes": [
// 长时任务类型的配置项
"location"
],

开启定位长时任务和关闭长时任务的接口:

startLocationContinuousTask(){
  let wantAgentInfo: wantAgent.WantAgentInfo = {
    // 点击通知后,将要执行的动作列表
    wants: [
      {
        bundleName: "com.example.backgroundtask",
        abilityName: "EntryAbility"
      }
    ],
    // 点击通知后,动作类型
    actionType: wantAgent.OperationType.START_ABILITY,
    // 使用者自定义的一个私有值
    requestCode: 0,
    // 点击通知后,动作执行属性
    wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
  };

  try {
    //通过wantAgent模块下getWantAgent方法获取WantAgent对象
    wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj: WantAgent) => {
      try {
        backgroundTaskManager.startBackgroundRunning(getContext(this) as common.UIAbilityContext,
          backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj).then(() => {
          console.info("Operation startBackgroundRunning succeeded");
        }).catch((error: BusinessError) => {
          console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
        });
      } catch (error) {
        console.error(` startBackgroundRunning failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`);
      }
    });
  } catch (error) {
    console.error(`Operation getWantAgent failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`);
  }
}

//关闭长时任务
stopLocationContinuousTask() {
  backgroundTaskManager.stopBackgroundRunning(getContext(this) as common.UIAbilityContext).then(() => {
    console.info(`Succeeded in operationing stopBackgroundRunning.`);
  }).catch((error: BusinessError) => {
    console.error(`Failed to operation stopBackgroundRunning. Code is ${error.code}, message is ${error.message}`);
  });
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏