HarmonyOS 鸿蒙Next 后台定位问题
HarmonyOS 鸿蒙Next 后台定位问题
使用鸿蒙的@ohos.geoLocationManager (位置服务),给了后台定位的权限,也在应用的设置的【位置信息】权限设置为【始终允许】,应用前台定位一切正常,切换到后台,还能定位几秒钟,就不再返回位置信息了。是受到什么省电策略之类的影响吗?还是什么问题呢(我用高德的定位sdk也有相同问题)
private systemRequestInfo: geoLocationManager.LocationRequest = {
'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX,
'timeInterval': 1,
'distanceInterval': 0,
'maxAccuracy': 0
};
private systemListener = (location: geoLocationManager.Location): void => {
console.error("正在定位-系统")
this.setMeSta(location)
}
geoLocationManager.on('locationChange', this.systemRequestInfo, this.systemListener)
更多关于HarmonyOS 鸿蒙Next 后台定位问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html
参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/continuous-task-V5
申请ohos.permission.LOCATION_IN_BACKGROUND权限并且申请LOCATION类型的长时任务是可以支持应用切换到后台,持续上报位置信息的;
注意检查下自己的【设置】-【隐私和安全】-【位置信息】,看看自己的应用是否设置了【始终允许】
更多关于HarmonyOS 鸿蒙Next 后台定位问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
参考下面代码,下面代码亲自测试过,没问题,通过下面代码可以实现HarmonyOS鸿蒙next后台定位
HarmonyOS鸿蒙next中可以实现后台定位需要配置后台长任务权限
"module": {
"abilities": [
{
"backgroundModes": [
"dataTransfer",
"location"
], // 后台模式类型
}
],
"requestPermissions": [
{
"name": "ohos.permission.KEEP_BACKGROUND_RUNNING" // 长时任务权限
},
{
"name": "ohos.permission.LOCATION_IN_BACKGROUND",
"reason": "$string:get_location_description",
"usedScene": {
"abilities": [
"EntryAbility"
],
"when": "inuse"
}
}
]
}
backgroundTaskManager申请配置后台任务实现后台定位
// 申请长时任务
async startBackgroundRunning() {
const context = getContext()
// 获取 bundle 应用信息
const bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION)
let wantAgentInfo : wantAgent.WantAgentInfo = {
wants: [
{
bundleName: bundleInfo.name,
abilityName: "EntryAbility"
}
],
operationType: wantAgent.OperationType.START_ABILITY,
requestCode: 0,
wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};
// 重点: 创建后台定位任务
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj : WantAgent) => {
backgroundTaskManager.startBackgroundRunning(context,
backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj, (err: BusinessError, data: void)=> {
if (err) {
console.error("Operation startBackgroundRunning failed Cause: " + JSON.stringify(err));
} else {
console.info("Operation startBackgroundRunning succeeded");
}
})
});
}
stopBackgroundRunningTask() {
const context = getContext()
backgroundTaskManager.stopBackgroundRunning(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}`);
});
}
针对HarmonyOS 鸿蒙Next后台定位问题,以下是一些专业的解决方案:
- 确认系统位置开关:首先确保系统的位置开关已开启,如果系统位置能力没有开启,任何应用都无法使用定位服务。
- 检查应用权限:应用需要向用户申请“ohos.permission.LOCATION”以获取精准位置,或申请“ohos.permission.APPROXIMATELY_LOCATION”以获取模糊位置。若应用需要后台定位,还需申请“ohos.permission.LOCATION_IN_BACKGROUND”。
- 定位策略与超时时间:在调用定位API时,应确保已正确配置定位策略和超时时间。例如,使用“geoLocationManager.getCurrentLocation”方法时,可以设置定位策略为精度优先或速度优先,并根据需要设置单次定位超时时间。
- 软件更新:检查鸿蒙系统及其定位软件是否有更新,及时安装最新版本,以修复可能存在的bug。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html 。