美文网首页
HarmonyOS NEXT 长时任务的学习和总结

HarmonyOS NEXT 长时任务的学习和总结

作者: 水滴石轩 | 来源:发表于2025-03-05 19:17 被阅读0次

想要使用鸿蒙的长时任务需要配置权限:ohos.permission.KEEP_BACKGROUND_RUNNING

并在module.json5中配置需要处理的长时任务类型,此处以定位为例:

{        ..."backgroundModes": [// 长时任务类型的配置项"location"]      }    ]

然后再合适的地方调用startBackgroundRunning方法开启长时任务,通过调用stopBackgroundRunning方法关闭长时任务

具体代码如下:

import { common, wantAgent, WantAgent } from '@kit.AbilityKit';

import { BusinessError } from '@kit.BasicServicesKit';

import { CSLogger } from './CSLogger';

const TAG: string = 'CSBackgroundTask';

export function startContinuousTask(context: common.UIAbilityContext) {

  let wantAgentInfo: wantAgent.WantAgentInfo = {

    // 点击通知后,将要执行的动作列表

    // 添加需要被拉起应用的bundleName和abilityName

    wants: [

      {

        bundleName: "com.example.csharmonyosdemo",

        abilityName: "EntryAbility"

      }

    ],

    // 指定点击通知栏消息后的动作是拉起ability

    actionType: wantAgent.OperationType.START_ABILITY,

    // 使用者自定义的一个私有值

    requestCode: 0,

    // 点击通知后,动作执行属性

    actionFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]

  };

  try {

    // 通过wantAgent模块下getWantAgent方法获取WantAgent对象

    wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj: WantAgent) => {

      try {

        let list: Array<string> = ["location"];

        backgroundTaskManager.startBackgroundRunning(context, list, wantAgentObj).then((res: backgroundTaskManager.ContinuousTaskNotification) => {

          CSLogger.info(TAG, "Operation startBackgroundRunning succeeded");

          // 此处执行具体的长时任务逻辑,如录音,录制等。

          setInterval(() => {

            CSLogger.info(TAG, "backgroundRunning task continue");

          }, 1000*60);

        }).catch((error: BusinessError) => {

          CSLogger.info(TAG, `Failed to Operation startBackgroundRunning. code is ${error.code} message is ${error.message}`);

        });

      } catch (error) {

        CSLogger.info(TAG, `Failed to Operation startBackgroundRunning. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`);

      }

    });

  } catch (error) {

    CSLogger.info(TAG, `Failed to Operation getWantAgent. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`);

  }

}

export function stopContinuousTask(context: common.UIAbilityContext) {

  backgroundTaskManager.stopBackgroundRunning(context).then(() => {

    CSLogger.info(TAG, `Succeeded in operationing stopBackgroundRunning.`);

  }).catch((err: BusinessError) => {

    CSLogger.info(TAG, `Failed to operation stopBackgroundRunning. Code is ${err.code}, message is ${err.message}`);

  });

}`

此处CSLogger是自定义日志工具,详情可以查看我的其他文章

----------------- end ---------------

后面会继续补充不足之处。

相关文章

  • python yield和yield from用法总结

    python yield和yield from用法总结 yield 作用: 注: generator的next()...

  • 程序媛在公司的进修之路(三)

    这次隔了两周,因为是双周任务和总结,学习任务如下:学习vertx框架,重点学习core、web、data acce...

  • 7月总结&8月计划

    7月计划&总结 工作:提前规划工作计划和任务并按时完成,当日事情当日毕; 总结:工作完成 学习:每天保证4小时的学...

  • 【HarmonyOS 专题】01 基础 Mac 环境安装配置

    HarmonyOS 已于 2020 年 12 更新到 2.0 版本;小菜周围的人都在学习和研究,小菜也想学习一下;...

  • Scanner用法

    一、常用方法next()、nextInt()和nextLine()的使用总结 例一 输出结果: nextInt()...

  • Hexo-修改Hexo主题

    学习目标 本文主要学习的内容如下: 如何去获取到 NexT 主题 如何去安装和配置 NexT 主题 设置菜单,如分...

  • 鸿蒙OS初识

    学习官网:https://www.harmonyos.com/cn/develop[https://www.har...

  • 任务2学习总结

    高效命令行操作指令 1.基本单词 2.命令行常见操作 pwd(print work directory)打印出用户...

  • 2023-01-06

    今日计划完成情况 反思总结 虽然玩的时间比学习的时间长,都是一种喜爱的生活。学习任务完成即可。本周快要过去了,电子...

  • 鸿蒙应用从无到有

    好像是上周天我的mate30自己升级到harmonyos系统了,用下来基本和安卓一样,操作上略有差异。正好公司任务...

网友评论

      本文标题:HarmonyOS NEXT 长时任务的学习和总结

      本文链接:https://www.haomeiwen.com/subject/htdlmjtx.html