开启后台定位功能地图功能希望在后台持续运行,后台运行超过一小段时间且未申请定位导航长时任务则会被冻结

开启后台定位功能

HarmonyOS
2024-05-20 21:19:19
1375浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
e_leaner

地图功能希望在后台持续运行,在目前的后台定位机制中,后台运行超过一小段时间且未申请定位导航长时任务则会被冻结。

在此情况下则需要申请后台长时任务保证应用的后台运行。

使用的核心API

backgroundTaskManager.startBackgroundRunning()

核心代码解释

创建长时任务,因为是定位服务,所以后台模式设置为BackgroundMode.LOCATION

 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] 
    }; 
 
    // 通过wantAgent模块下getWantAgent方法获取WantAgent对象 
    wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj: WantAgent) => { 
      backgroundTaskManager.startBackgroundRunning(this.context, 
        backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj).then(() => { 
        console.info(`Succeeded in 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 in 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%') 
  } 
}
  • 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.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.

实现效果

应用在后台定位运行时长超过10s。

注明适配的版本信息

IDE版本:4.1.3.500

SDK版本:OpenHarmony 4..5.6

分享
微博
QQ
微信
回复
2024-05-21 17:12:23


相关问题
时任务后台运行,保证应用不被挂起
1830浏览 • 1回复 待解决
如何让应用后台持续运行
18731浏览 • 2回复 待解决
鸿蒙手表后台持续定位不成功
1120浏览 • 0回复 待解决
HarmonyOS 后台定位问题
1249浏览 • 1回复 待解决
后台时任务启动失败
3012浏览 • 1回复 待解决