HarmonyOS鸿蒙Next中能否使用 ohos.permission.RUNNING_LOCK 保证应用程序不会被挂起

HarmonyOS鸿蒙Next中能否使用 ohos.permission.RUNNING_LOCK 保证应用程序不会被挂起 能否使用 ohos.permission.RUNNING_LOCK 保证应用程序不会被挂起

3 回复

可以使用这个权限:ohos.permission.KEEP_BACKGROUND_RUNNING

参考文档链接:‘https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/continuous-task-V5#%E6%A6%82%E8%BF%B0

module.json5 配置文件 声明ohos.permission.KEEP_BACKGROUND_RUNNING,以及长任务类型配置项,参考代码:

import { backgroundTaskManager } from '@kit.BackgroundTasksKit';
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { rpc } from '@kit.IPCKit'
import { BusinessError } from '@kit.BasicServicesKit';
import { wantAgent, WantAgent } from '@kit.AbilityKit';

@Entry
@Component
struct Index {
  @State message: string = 'ContinuousTask';
  // 通过getContext方法,来获取page所在的UIAbility上下文。
  private context: Context = this.getContext();

  startContinuousTask() {
    let wantAgentInfo: wantAgent.WantAgentInfo = {
      // 点击通知后,将要执行的动作列表
      // 添加需要被拉起应用的bundleName和abilityName
      wants: [
        {
          bundleName: "com.weiwei.account.demo",
          abilityName: "com.weiwei.account.demo.MainAbility"
        }
      ],
      // 指定点击通知栏消息后的动作是拉起ability
      actionType: wantAgent.OperationType.START_ABILITY,
      // 使用者自定义的一个私有值
      requestCode: 0,
      // 点击通知后,动作执行属性
      actionFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
    };

    wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj: WantAgent) => {
      backgroundTaskManager.startBackgroundRunning(
        this.context,
        backgroundTaskManager.BackgroundMode.AUDIO_RECORDING, 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() {
    Row() {
      Column() {
        Text("Index")
          .fontSize(50)
          .fontWeight(FontWeight.Bold)

        Button() {
          Text('申请长时任务').fontSize(25).fontWeight(FontWeight.Bold)
        }
        .type(ButtonType.Capsule)
        .margin({ top: 10 })
        .backgroundColor('#0D9FFB')
        .width(250)
        .height(40)
        .onClick(() => {
          // 通过按钮申请长时任务
          this.startContinuousTask();
        })

        Button() {
          Text('取消长时任务').fontSize(25).fontWeight(FontWeight.Bold)
        }
        .type(ButtonType.Capsule)
        .margin({ top: 10 })
        .backgroundColor('#0D9FFB')
        .width(250)
        .height(40)
        .onClick(() => {
          // 此处结束具体的长时任务的执行

          // 通过按钮取消长时任务
          this.stopContinuousTask();
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

更多关于HarmonyOS鸿蒙Next中能否使用 ohos.permission.RUNNING_LOCK 保证应用程序不会被挂起的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,ohos.permission.RUNNING_LOCK权限可以用于申请运行锁,确保应用程序在后台运行时不会被系统挂起。通过该权限,应用程序可以保持一定的活跃状态,执行必要的后台任务。需要注意的是,使用该权限可能会增加设备的电量消耗,因此应谨慎使用,并确保仅在必要时申请运行锁。

在HarmonyOS鸿蒙Next中,ohos.permission.RUNNING_LOCK权限允许应用程序申请运行锁,以防止系统在后台挂起应用。通过使用RunningLock API,应用程序可以保持后台运行状态,确保关键任务不被中断。但请注意,滥用此权限可能导致设备资源消耗过多,影响系统性能,因此建议仅在必要时使用,并遵循HarmonyOS的开发规范。

回到顶部