HarmonyOS 长时任务执行几秒后,自动关闭

以下为核心代码在module.json5文件中,也同样声明了taskKeeping。

let wantAgentInfo: wantAgent.WantAgentInfo = { 
  // 点击通知后,将要执行的动作列表 
  wants: [ 
    { 
      bundleName: "com.xxx.xxx", 
      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), 
        backgroundTaskManager.BackgroundMode.TASK_KEEPING, wantAgentObj, ()=>{ 
          setInterval(()=>{console.log('success')},2200) 
        }) 
    } catch (error) { 
      console.error(`Operation 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}`); 
}
  • 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.

执行上述代码后,确实在短时间内,后台任务继续执行,但是在执行了几秒后,bg任务就自动退出了,请问后台任务为何会退出?

HarmonyOS
2024-08-12 14:31:26
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

taskKeeping手机不支持。

分享
微博
QQ
微信
回复
2024-08-12 19:54:12
相关问题
如何申请多个时任务
2844浏览 • 1回复 待解决
HarmonyOS 时任务启动失败9800005
838浏览 • 1回复 待解决
HarmonyOS 申请时任务报错9800006 -
662浏览 • 1回复 待解决
时任务是否阻止系统休眠
866浏览 • 1回复 待解决
HarmonyOS 音视频时任务使用
988浏览 • 1回复 待解决
时任务后台运行,保证应用不被挂起
1833浏览 • 1回复 待解决
音视频播放是否需要创建时任务
2793浏览 • 1回复 待解决
在arkts中如何执行时任务
296浏览 • 1回复 待解决