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

  1. 使用长时任务,开启location的任务类型得到错误码(9800005)。
  2. 当一个UIAbility开启长时任务(配置了多种任务类型:location,audioPlayback,dataTransfer等等),可不可以同时使用这些任务的后台功能。
HarmonyOS
2024-12-23 16:20:33
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

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

model.json5中做如下添加:

"backgroundModes": [
// 长时任务类型的配置项
"location"
],
  • 1.
  • 2.
  • 3.
  • 4.

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

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}`);
  });
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
分享
微博
QQ
微信
回复
2024-12-23 18:44:49
相关问题
HarmonyOS 时任务启动失败9800005
861浏览 • 1回复 待解决
如何申请多个时任务
2861浏览 • 1回复 待解决
时任务是否阻止系统休眠
872浏览 • 1回复 待解决
HarmonyOS 申请时任务报错9800006 -
674浏览 • 1回复 待解决
HarmonyOS 音视频时任务使用
998浏览 • 1回复 待解决
后台长时任务启动失败
3029浏览 • 1回复 待解决
音视频播放是否需要创建时任务
2804浏览 • 1回复 待解决
时任务后台运行,保证应用不被挂起
1858浏览 • 1回复 待解决
HarmonyOS时任务
695浏览 • 1回复 待解决
后台长时任务启动失败报错码201
3399浏览 • 1回复 待解决