1# Runninglock锁 2 3>  **说明:** 4> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 5 6该模块主要提供Runninglock锁相关操作的接口,包括创建、查询、持锁、释放锁等操作。 7 8 9## 导入模块 10 11``` 12import runningLock from '@ohos.runningLock'; 13``` 14 15 16## RunningLockType 17 18RunningLock锁的类型。 19 20**系统能力:** 以下各项对应的系统能力均为SystemCapability.PowerManager.PowerManager.Core 21 22| 名称 | 默认值 | 描述 | 23| ------------------------ | ---- | ------------------- | 24| BACKGROUND | 1 | 阻止系统休眠的锁。 | 25| PROXIMITY_SCREEN_CONTROL | 2 | 通过接近或者远离状态来控制亮灭屏的锁。 | 26 27 28## isRunningLockTypeSupported 29 30isRunningLockTypeSupported(type: RunningLockType, callback: AsyncCallback<boolean>): void 31 32查询系统是否支持该类型的锁。 33 34**系统能力:** SystemCapability.PowerManager.PowerManager.Core 35 36**参数:** 37 38| 参数名 | 类型 | 必填 | 说明 | 39| -------- | ---------------------------- | ---- | ---------------------------------------- | 40| type | RunningLockType | 是 | 需要查询的锁的类型。 | 41| callback | AsyncCallback<boolean> | 是 | 指定的callback回调方法,用于获取返回值。<br/>callback返回值:支持返回true,不支持返回false。 | 42 43**示例:** 44 45``` 46runningLock.isRunningLockTypeSupported(runningLock.RunningLockType.BACKGROUND, (error, supported) => { 47 if (typeof error === "undefined") { 48 console.info('BACKGROUND support status is ' + supported); 49 } else { 50 console.log('error: ' + error); 51 } 52}) 53``` 54 55 56## isRunningLockTypeSupported 57 58isRunningLockTypeSupported(type: RunningLockType): Promise<boolean> 59 60查询系统是否支持该类型的锁。 61 62**系统能力:** SystemCapability.PowerManager.PowerManager.Core 63 64**参数:** 65 66| 参数名 | 类型 | 必填 | 说明 | 67| ---- | --------------- | ---- | ---------- | 68| type | RunningLockType | 是 | 需要查询的锁的类型。 | 69 70**返回值:** 71 72| 类型 | 说明 | 73| ---------------------- | ---------------------------------------- | 74| Promise<boolean> | Promise实例,用于异步获取返回值,支持返回true,不支持返回false。 | 75 76**示例:** 77 78``` 79runningLock.isRunningLockTypeSupported(runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL) 80.then(supported => { 81 console.info('PROXIMITY_SCREEN_CONTROL support status is ' + supported); 82}) 83.catch(error => { 84 console.log('error: ' + error); 85}); 86``` 87 88 89## createRunningLock 90 91createRunningLock(name: string, type: RunningLockType, callback: AsyncCallback<RunningLock>): void 92 93创建RunningLock锁。 94 95**系统能力:** SystemCapability.PowerManager.PowerManager.Core 96 97**需要权限:** ohos.permission.RUNNING_LOCK 98 99**参数:** 100 101| 参数名 | 类型 | 必填 | 说明 | 102| -------- | ---------------------------------------- | ---- | -------------------------------------- | 103| name | string | 是 | 锁的名字。 | 104| type | RunningLockType | 是 | 要创建的锁的类型。 | 105| callback | AsyncCallback<[RunningLock](#runninglock)> | 是 | 指定的callback回调方法,用于获取返回的RunningLock锁对象。 | 106 107**示例:** 108 109``` 110runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND, (error, lockIns) => { 111 if (typeof error === "undefined") { 112 console.log('create runningLock test error: ' + error); 113 } else { 114 var used = lockIns.isUsed(); 115 console.info('runninglock is used: ' + used); 116 lockIns.lock(500); 117 used = lockIns.isUsed(); 118 console.info('after lock runninglock is used ' + used); 119 } 120}) 121``` 122 123 124## createRunningLock 125 126createRunningLock(name: string, type: RunningLockType): Promise<RunningLock> 127 128创建Runninglock锁。 129 130**系统能力:** SystemCapability.PowerManager.PowerManager.Core 131 132**需要权限:** ohos.permission.RUNNING_LOCK 133 134**参数:** 135 136| 参数名 | 类型 | 必填 | 说明 | 137| ---- | --------------- | ---- | --------- | 138| name | string | 是 | 锁的名字。 | 139| type | RunningLockType | 是 | 要创建的锁的类型。 | 140 141**返回值:** 142 143| 类型 | 说明 | 144| ---------------------------------------- | ---------------------------------- | 145| Promise<[RunningLock](#runninglock)> | Promise实例,用于异步获取返回的RunningLock锁对象。 | 146 147**示例:** 148 149``` 150runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND) 151.then(runninglock => { 152 console.info('create runningLock success'); 153}) 154.catch(error => { 155 console.log('create runningLock test error: ' + error); 156}) 157``` 158 159 160## RunningLock 161 162阻止系统休眠的锁。 163 164 165### lock 166 167lock(timeout: number): void 168 169锁定和持有RunningLock。 170 171**系统能力:** SystemCapability.PowerManager.PowerManager.Core 172 173**需要权限:** ohos.permission.RUNNING_LOCK 174 175**参数:** 176 177| 参数名 | 类型 | 必填 | 说明 | 178| ------- | ------ | ---- | -------------------------- | 179| timeout | number | 否 | 锁定和持有RunningLock的时长,单位:毫秒。 | 180 181**示例:** 182 183``` 184runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND) 185.then(runningLock => { 186 runningLock.lock(100) 187 console.info('create runningLock success') 188}) 189.catch(error => { 190 console.log('create runningLock test error: ' + error) 191}); 192``` 193 194 195### unlock 196 197unlock(): void 198 199释放Runninglock锁。 200 201**系统能力:** SystemCapability.PowerManager.PowerManager.Core 202 203**需要权限:** ohos.permission.RUNNING_LOCK 204 205**示例:** 206 207``` 208runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND) 209.then(runningLock => { 210 runningLock.unlock() 211 console.info('create and unLock runningLock success') 212}) 213.catch(error => { 214 console.log('create runningLock test error: ' + error) 215}); 216``` 217 218 219### isUsed 220 221isUsed(): boolean 222 223查询当前Runninglock是持有状态还是释放状态。 224 225**系统能力:** SystemCapability.PowerManager.PowerManager.Core 226 227**返回值:** 228| 类型 | 说明 | 229| ------- | ------------------------------------- | 230| boolean | 当前RunningLock是持有状态返回true,释放状态返回false。 | 231 232**示例:** 233 234``` 235runningLock.createRunningLock("running_lock_test", runningLock.RunningLockType.BACKGROUND) 236.then(runningLock => { 237 var used = runningLock.isUsed() 238 console.info('runningLock used status: ' + used) 239}) 240.catch(error => { 241 console.log('runningLock isUsed test error: ' + error) 242}); 243``` 244