我使用的地图加sdk。
export class BackgroundUtil {
/**
* Start background task.
*
* @param context
*/
public static startContinuousTask(context: common.UIAbilityContext): void {
let wantAgentInfo: wantAgent.WantAgentInfo = {
wants: [
{
bundleName: context.abilityInfo.bundleName,
abilityName: context.abilityInfo.name
}
],
operationType: wantAgent.OperationType.START_ABILITY,
requestCode: 0,
wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
try {
backgroundTaskManager.startBackgroundRunning(context,
backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj).then(() => {
Logger.info(TAG, 'startBackgroundRunning succeeded');
}).catch((err: Error) => {
Logger.error(TAG, `startBackgroundRunning failed Cause: ${JSON.stringify(err)}`);
});
} catch (error) {
Logger.error(TAG, `stopBackgroundRunning failed. error: ${JSON.stringify(error)} `);
}
});
}
/**
* Stop background task.
*
* @param context
*/
public static stopContinuousTask(context: common.UIAbilityContext): void {
try {
backgroundTaskManager.stopBackgroundRunning(context).then(() => {
Logger.info(TAG, 'stopBackgroundRunning succeeded');
}).catch((err: Error) => {
Logger.error(TAG, `stopBackgroundRunning failed Cause: ${JSON.stringify(err)}`);
});
} catch (error) {
Logger.error(TAG, `stopBackgroundRunning failed. error: ${JSON.stringify(error)} `);
}
}
}
使用长时定位服务时,看起来不是正常调用
可参考以下代码:
PermissionsUtil.ets代码:
module.json5文件中申请定位相关权限和后台任务权限
ohos.permission.LOCATION
ohos.permission.APPROXIMATELY\_LOCATION
ohos.permission.LOCATION\_IN\_BACKGROUND
ohos.permission.KEEP\_BACKGROUND\_RUNNING
module.json5配置文件中为需要使用长时任务的UIAbility声明相应的长时任务类型(配置文件中填写长时任务类型的配置项)
参考文档:
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/continuous-task-V5