文档管理中心
我的

您当前正在浏览HarmonyOS最新文档,覆盖已发布的所有API版本,可在API参考中筛选您使用的API版本。详细的版本配套关系请参考版本说明。

指南与API参考API参考系统基础功能Basic Services Kit(基础服务)ArkTS API设备管理@ohos.runningLock (RunningLock锁)

@ohos.runningLock (RunningLock锁)

Phone12+PC/2in113+Tablet12+TV23+Wearable18+

该模块为RunningLock锁相关操作的接口,提供使能接近光亮灭屏或者设备熄屏后阻止进入睡眠的能力,包括创建、查询、持锁、释放锁等操作,类型详情见RunningLockType。

说明

本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

收起
自动换行
深色代码主题
复制
  1. import {runningLock} from '@kit.BasicServicesKit';

runningLock.isSupported9+

Phone12+PC/2in113+Tablet12+TV23+Wearable18+

isSupported(type: RunningLockType): boolean

查询系统是否支持该类型的锁。

系统能力: SystemCapability.PowerManager.PowerManager.Core

参数:

展开
参数名 类型 必填 说明
type RunningLockType 是 需要查询的锁的类型;该参数必须是一个枚举类。

返回值:

展开
类型 说明
boolean 返回true表示支持,返回false表示不支持。

错误码:

以下错误码的详细介绍请参见通用错误码。

展开
错误码ID 错误信息
401 Parameter error. Possible causes: 1. Incorrect parameter types; 2. Parameter verification failed.

示例:

收起
自动换行
深色代码主题
复制
  1. try {
  2. let isSupported = runningLock.isSupported(runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL);
  3. console.info('BACKGROUND type supported: ' + isSupported);
  4. } catch(err) {
  5. console.error('check supported failed, err: ' + err);
  6. }

runningLock.create9+

Phone12+PC/2in113+Tablet12+TV23+Wearable18+

create(name: string, type: RunningLockType, callback: AsyncCallback<RunningLock>): void

创建RunningLock锁。使用callback异步回调。

系统能力: SystemCapability.PowerManager.PowerManager.Core

需要权限: ohos.permission.RUNNING_LOCK

参数:

展开
参数名 类型 必填 说明
name string 是 锁的名字;该参数必须为字符串类型。
type RunningLockType 是 要创建的锁的类型;该参数必须是一个枚举类。
callback AsyncCallback<RunningLock> 是 回调函数。当创建锁成功,err为undefined,data为创建的RunningLock;否则为错误对象;AsyncCallback封装了一个RunningLock类型的类。

错误码:

以下错误码的详细介绍请参见通用错误码。

展开
错误码ID 错误信息
401 Parameter error. Possible causes: 1. Parameter verification failed.
201 If the permission is denied.

示例:

收起
自动换行
深色代码主题
复制
  1. runningLock.create('running_lock_test', runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL, (err: Error, lock: runningLock.RunningLock) => {
  2. if (typeof err === 'undefined') {
  3. console.info('created running lock: ' + lock);
  4. } else {
  5. console.error('create running lock failed, err: ' + err);
  6. }
  7. });

runningLock.create9+

Phone12+PC/2in113+Tablet12+TV23+Wearable18+

create(name: string, type: RunningLockType): Promise<RunningLock>

创建RunningLock锁。使用Promise异步回调。

系统能力: SystemCapability.PowerManager.PowerManager.Core

需要权限: ohos.permission.RUNNING_LOCK

参数:

展开
参数名 类型 必填 说明
name string 是 锁的名字;该参数必须为字符串类型。
type RunningLockType 是 要创建的锁的类型;该参数必须是一个枚举类。

返回值:

展开
类型 说明
Promise<RunningLock> Promise对象,返回RunningLock锁对象。

错误码:

以下错误码的详细介绍请参见通用错误码。

展开
错误码ID 错误信息
401 Parameter error. Possible causes: 1. Parameter verification failed.
201 If the permission is denied.

示例:

收起
自动换行
深色代码主题
复制
  1. runningLock.create('running_lock_test', runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL)
  2. .then((lock: runningLock.RunningLock) => {
  3. console.info('created running lock: ' + lock);
  4. })
  5. .catch((err: Error) => {
  6. console.error('create running lock failed, err: ' + err);
  7. });

runningLock.isRunningLockTypeSupported(deprecated)

Phone12+PC/2in113+Tablet12+TV23+Wearable18+

isRunningLockTypeSupported(type: RunningLockType, callback: AsyncCallback<boolean>): void

查询系统是否支持该类型的锁。使用callback异步回调。

说明

从API version 7开始支持,从API version 9开始不再维护,建议使用runningLock.isSupported替代。

系统能力: SystemCapability.PowerManager.PowerManager.Core

参数:

展开
参数名 类型 必填 说明
type RunningLockType 是 需要查询的锁的类型。
callback AsyncCallback<boolean> 是 回调函数。当查询成功,err为undefined,data为获取到的支持情况,返回true表示支持,返回false表示不支持;否则为错误对象。

示例:

收起
自动换行
深色代码主题
复制
  1. runningLock.isRunningLockTypeSupported(runningLock.RunningLockType.BACKGROUND, (err: Error, data: boolean) => {
  2. if (typeof err === 'undefined') {
  3. console.info('BACKGROUND lock support status: ' + data);
  4. } else {
  5. console.error('check BACKGROUND lock support status failed, err: ' + err);
  6. }
  7. });

runningLock.isRunningLockTypeSupported(deprecated)

Phone12+PC/2in113+Tablet12+TV23+Wearable18+

isRunningLockTypeSupported(type: RunningLockType): Promise<boolean>

查询系统是否支持该类型的锁。使用Promise异步回调。

说明

从API version 7开始支持,从API version 9开始不再维护,建议使用runningLock.isSupported替代。

系统能力: SystemCapability.PowerManager.PowerManager.Core

参数:

展开
参数名 类型 必填 说明
type RunningLockType 是 需要查询的锁的类型。

返回值:

展开
类型 说明
Promise<boolean> Promise对象。返回true表示支持;返回false表示不支持。

示例:

收起
自动换行
深色代码主题
复制
  1. runningLock.isRunningLockTypeSupported(runningLock.RunningLockType.BACKGROUND)
  2. .then((data: boolean) => {
  3. console.info('BACKGROUND lock support status: ' + data);
  4. })
  5. .catch((err: Error) => {
  6. console.error('check BACKGROUND lock support status failed, err: ' + err);
  7. });

runningLock.createRunningLock(deprecated)

Phone12+PC/2in113+Tablet12+TV23+Wearable18+

createRunningLock(name: string, type: RunningLockType, callback: AsyncCallback<RunningLock>): void

创建RunningLock锁。使用callback异步回调。

说明

从API version 7开始支持,从API version 9开始不再维护,建议使用runningLock.create替代。

系统能力: SystemCapability.PowerManager.PowerManager.Core

需要权限: ohos.permission.RUNNING_LOCK

参数:

展开
参数名 类型 必填 说明
name string 是 锁的名字。建议使用包名或类名加后缀的方式命名。
type RunningLockType 是 要创建的锁的类型。
callback AsyncCallback<RunningLock> 是 回调函数。当创建锁成功,err为undefined,data为创建的RunningLock;否则为错误对象。AsyncCallback封装了一个RunningLock类型的类。

示例:

收起
自动换行
深色代码主题
复制
  1. runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND, (err: Error, lock: runningLock.RunningLock) => {
  2. if (typeof err === 'undefined') {
  3. console.info('created running lock: ' + lock);
  4. } else {
  5. console.error('create running lock failed, err: ' + err);
  6. }
  7. });

runningLock.createRunningLock(deprecated)

Phone12+PC/2in113+Tablet12+TV23+Wearable18+

createRunningLock(name: string, type: RunningLockType): Promise<RunningLock>

创建RunningLock锁。使用Promise异步回调。

说明

从API version 7开始支持,从API version 9开始不再维护,建议使用runningLock.create替代。

系统能力: SystemCapability.PowerManager.PowerManager.Core

需要权限: ohos.permission.RUNNING_LOCK

参数:

展开
参数名 类型 必填 说明
name string 是 锁的名字。建议使用包名或类名加后缀的方式命名。
type RunningLockType 是 要创建的锁的类型。

返回值:

展开
类型 说明
Promise<RunningLock> Promise对象,返回RunningLock锁对象。

示例:

收起
自动换行
深色代码主题
复制
  1. runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND)
  2. .then((lock: runningLock.RunningLock) => {
  3. console.info('created running lock: ' + lock);
  4. })
  5. .catch((err: Error) => {
  6. console.error('create running lock failed, err: ' + err);
  7. });

RunningLock

Phone12+PC/2in113+Tablet12+TV23+Wearable18+

阻止系统睡眠的锁。

hold9+

Phone12+PC/2in113+Tablet12+TV23+Wearable18+

hold(timeout: number): void

锁定和持有RunningLock。

系统能力: SystemCapability.PowerManager.PowerManager.Core

需要权限: ohos.permission.RUNNING_LOCK

参数:

展开
参数名 类型 必填 说明
timeout number 是

锁定和持有RunningLock的时长,单位:毫秒。

该参数必须为数字类型:

-1:永久持锁,需要主动释放。

0:默认3s后超时释放。

>0:按传入值超时释放。

错误码:

以下错误码的详细介绍请参见通用错误码。

展开
错误码ID 错误信息
401 Parameter error. Possible causes: 1. Incorrect parameter types.
201 If the permission is denied.

示例:

收起
自动换行
深色代码主题
复制
  1. // RunningLockTest.ets
  2. class RunningLockTest {
  3. public static recordLock: runningLock.RunningLock;
  4. public static holdRunningLock(): void {
  5. if (RunningLockTest.recordLock) {
  6. RunningLockTest.recordLock.hold(500);
  7. console.info('hold running lock success');
  8. } else {
  9. runningLock.create('running_lock_test', runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL, (err: Error, lock: runningLock.RunningLock) => {
  10. if (typeof err === 'undefined') {
  11. console.info('create running lock: ' + lock);
  12. RunningLockTest.recordLock = lock;
  13. try {
  14. lock.hold(500);
  15. console.info('hold running lock success');
  16. } catch(err) {
  17. console.error('hold running lock failed, err: ' + err);
  18. }
  19. } else {
  20. console.error('create running lock failed, err: ' + err);
  21. }
  22. });
  23. }
  24. }
  25. }

unhold9+

Phone12+PC/2in113+Tablet12+TV23+Wearable18+

unhold(): void

释放RunningLock锁。

系统能力: SystemCapability.PowerManager.PowerManager.Core

需要权限: ohos.permission.RUNNING_LOCK

错误码:

以下错误码的详细介绍请参见通用错误码。

展开
错误码ID 错误信息
201 If the permission is denied.

示例:

收起
自动换行
深色代码主题
复制
  1. // RunningLockTest.ets
  2. class RunningLockTest {
  3. public static recordLock: runningLock.RunningLock;
  4. public static unholdRunningLock(): void {
  5. if (RunningLockTest.recordLock) {
  6. RunningLockTest.recordLock.unhold();
  7. console.info('unhold running lock success');
  8. } else {
  9. runningLock.create('running_lock_test', runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL, (err: Error, lock: runningLock.RunningLock) => {
  10. if (typeof err === 'undefined') {
  11. console.info('create running lock: ' + lock);
  12. RunningLockTest.recordLock = lock;
  13. try {
  14. lock.unhold();
  15. console.info('unhold running lock success');
  16. } catch(err) {
  17. console.error('unhold running lock failed, err: ' + err);
  18. }
  19. } else {
  20. console.error('create running lock failed, err: ' + err);
  21. }
  22. });
  23. }
  24. }
  25. }

isHolding9+

Phone12+PC/2in113+Tablet12+TV23+Wearable18+

isHolding(): boolean

查询当前RunningLock是持有状态还是释放状态。

系统能力: SystemCapability.PowerManager.PowerManager.Core

返回值:

展开
类型 说明
boolean 返回true表示当前RunningLock是持有状态,返回false表示当前RunningLock是释放状态。

示例:

收起
自动换行
深色代码主题
复制
  1. // RunningLockTest.ets
  2. class RunningLockTest {
  3. public static recordLock: runningLock.RunningLock;
  4. public static isHoldingRunningLock(): void {
  5. if (RunningLockTest.recordLock) {
  6. let isHolding = RunningLockTest.recordLock.isHolding();
  7. console.info('check running lock holding status: ' + isHolding);
  8. } else {
  9. runningLock.create('running_lock_test', runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL, (err: Error, lock: runningLock.RunningLock) => {
  10. if (typeof err === 'undefined') {
  11. console.info('create running lock: ' + lock);
  12. RunningLockTest.recordLock = lock;
  13. let isHolding = lock.isHolding();
  14. console.info('check running lock holding status: ' + isHolding);
  15. } else {
  16. console.error('create running lock failed, err: ' + err);
  17. }
  18. });
  19. }
  20. }
  21. }

lock(deprecated)

Phone12+PC/2in113+Tablet12+TV23+Wearable18+

lock(timeout: number): void

锁定和持有RunningLock。

说明

从API version 7开始支持,从API version 9开始不再维护,建议使用RunningLock.hold替代。

系统能力: SystemCapability.PowerManager.PowerManager.Core

需要权限: ohos.permission.RUNNING_LOCK

参数:

展开
参数名 类型 必填 说明
timeout number 是 锁定和持有RunningLock的时长,单位:毫秒。

示例:

收起
自动换行
深色代码主题
复制
  1. runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND)
  2. .then((lock: runningLock.RunningLock) => {
  3. lock.lock(500);
  4. console.info('create running lock and lock success');
  5. })
  6. .catch((err: Error) => {
  7. console.error('create running lock failed, err: ' + err);
  8. });

unlock(deprecated)

Phone12+PC/2in113+Tablet12+TV23+Wearable18+

unlock(): void

释放RunningLock锁。

说明

从API version 7开始支持,从API version 9开始不再维护,建议使用RunningLock.unhold替代。

系统能力: SystemCapability.PowerManager.PowerManager.Core

需要权限: ohos.permission.RUNNING_LOCK

示例:

收起
自动换行
深色代码主题
复制
  1. runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND)
  2. .then((lock: runningLock.RunningLock) => {
  3. lock.unlock();
  4. console.info('create running lock and unlock success');
  5. })
  6. .catch((err: Error) => {
  7. console.error('create running lock failed, err: ' + err);
  8. });

isUsed(deprecated)

Phone12+PC/2in113+Tablet12+TV23+Wearable18+

isUsed(): boolean

查询当前RunningLock是持有状态还是释放状态。

说明

从API version 7开始支持,从API version 9开始不再维护,建议使用RunningLock.isHolding替代。

系统能力: SystemCapability.PowerManager.PowerManager.Core

返回值:

展开
类型 说明
boolean 返回true表示当前RunningLock是持有状态,返回false表示当前RunningLock是释放状态。

示例:

收起
自动换行
深色代码主题
复制
  1. runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND)
  2. .then((lock: runningLock.RunningLock) => {
  3. let isUsed = lock.isUsed();
  4. console.info('check running lock used status: ' + isUsed);
  5. })
  6. .catch((err: Error) => {
  7. console.error('check running lock used status failed, err: ' + err);
  8. });

RunningLockType

Phone12+PC/2in113+Tablet12+TV23+Wearable18+

RunningLock锁的类型。

系统能力: SystemCapability.PowerManager.PowerManager.Core

展开
名称 值 说明
BACKGROUND(deprecated) 1

阻止系统睡眠的锁。

说明: 从API version 7开始支持,从API version 10开始废弃。

PROXIMITY_SCREEN_CONTROL 2 接近光锁,使能接近光传感器,并根据传感器与障碍物的距离远近发起亮灭屏流程。
BACKGROUND_USER_IDLE23+ 129

阻止系统自动睡眠的后台闲时任务锁,持锁能保证一段时间用户不活动后系统不进入自动睡眠。

注意: 不能阻止如PC合盖等场景系统进入强制睡眠,使用方必须监听COMMON_EVENT_ENTER_FORCE_SLEEP,监听到事件后释放该锁。该类型锁行为存在设备差异,使用该类型锁请参考阻止系统闲时进入睡眠开发指南。