W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
該模塊主要提供RunningLock鎖相關(guān)操作的接口,包括創(chuàng)建、查詢、持鎖、釋放鎖等操作。
本模塊首批接口從API version 7開(kāi)始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。
isSupported(type: RunningLockType): boolean;
查詢系統(tǒng)是否支持該類型的鎖。
系統(tǒng)能力: SystemCapability.PowerManager.PowerManager.Core
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
type | RunningLockType | 是 | 需要查詢的鎖的類型。 |
返回值:
類型 | 說(shuō)明 |
---|---|
boolean | 返回true表示支持,返回false表示不支持。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)RunningLock鎖錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
4900101 | 連接服務(wù)失敗。 |
示例:
- try {
- var isSupported = runningLock.isSupported(runningLock.RunningLockType.BACKGROUND);
- console.info('BACKGROUND type supported: ' + isSupported);
- } catch(err) {
- console.error('check supported failed, err: ' + err);
- }
create(name: string, type: RunningLockType, callback: AsyncCallback<RunningLock>): void
創(chuàng)建RunningLock鎖。
系統(tǒng)能力: SystemCapability.PowerManager.PowerManager.Core
需要權(quán)限: ohos.permission.RUNNING_LOCK
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
name | string | 是 | 鎖的名字。 |
type | RunningLockType | 是 | 要?jiǎng)?chuàng)建的鎖的類型。 |
callback | AsyncCallback<RunningLock> | 是 | 回調(diào)函數(shù)。當(dāng)創(chuàng)建鎖成功,err為undefined,data為創(chuàng)建的RunningLock;否則為錯(cuò)誤對(duì)象。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)RunningLock鎖錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
4900101 | 連接服務(wù)器失敗。 |
示例:
- runningLock.create('running_lock_test', runningLock.RunningLockType.BACKGROUND, (err, lock) => {
- if (typeof err === 'undefined') {
- console.info('created running lock: ' + lock);
- } else {
- console.error('create running lock failed, err: ' + err);
- }
- });
create(name: string, type: RunningLockType): Promise<RunningLock>
創(chuàng)建RunningLock鎖。
系統(tǒng)能力: SystemCapability.PowerManager.PowerManager.Core
需要權(quán)限: ohos.permission.RUNNING_LOCK
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
name | string | 是 | 鎖的名字。 |
type | RunningLockType | 是 | 要?jiǎng)?chuàng)建的鎖的類型。 |
返回值:
類型 | 說(shuō)明 |
---|---|
Promise<RunningLock> | Promise對(duì)象,返回RunningLock鎖對(duì)象。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)RunningLock鎖錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
4900101 | 連接服務(wù)器失敗。 |
示例:
- runningLock.create('running_lock_test', runningLock.RunningLockType.BACKGROUND)
- .then(lock => {
- console.info('created running lock: ' + lock);
- })
- .catch(err => {
- console.error('create running lock failed, error: ' + err);
- });
isRunningLockTypeSupported(type: RunningLockType, callback: AsyncCallback<boolean>): void
從API version 9開(kāi)始不再維護(hù),建議使用runningLock.isSupported替代。
查詢系統(tǒng)是否支持該類型的鎖。使用callback異步回調(diào)。
系統(tǒng)能力: SystemCapability.PowerManager.PowerManager.Core
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
type | RunningLockType | 是 | 需要查詢的鎖的類型。 |
callback | AsyncCallback<boolean> | 是 | 回調(diào)函數(shù)。當(dāng)查詢成功,err為undefined,data為獲取到的支持情況,返回true表示支持,返回false表示不支持;否則為錯(cuò)誤對(duì)象。 |
示例:
- runningLock.isRunningLockTypeSupported(runningLock.RunningLockType.BACKGROUND, (err, data) => {
- if (typeof err === 'undefined') {
- console.info('BACKGROUND lock support status: ' + data);
- } else {
- console.log('check BACKGROUND lock support status failed, err: ' + err);
- }
- });
isRunningLockTypeSupported(type: RunningLockType): Promise<boolean>
從API version 9開(kāi)始不再維護(hù),建議使用runningLock.isSupported替代。
查詢系統(tǒng)是否支持該類型的鎖。使用Promise異步回調(diào)。
系統(tǒng)能力: SystemCapability.PowerManager.PowerManager.Core
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
type | RunningLockType | 是 | 需要查詢的鎖的類型。 |
返回值:
類型 | 說(shuō)明 |
---|---|
Promise<boolean> | Promise對(duì)象。返回true表示支持;返回false表示不支持。 |
示例:
- runningLock.isRunningLockTypeSupported(runningLock.RunningLockType.BACKGROUND)
- .then(data => {
- console.info('BACKGROUND lock support status: ' + data);
- })
- .catch(err => {
- console.log('check BACKGROUND lock support status failed, err: ' + err);
- });
createRunningLock(name: string, type: RunningLockType, callback: AsyncCallback<RunningLock>): void
從API version 9開(kāi)始不再維護(hù),建議使用runningLock.create替代。
創(chuàng)建RunningLock鎖。
系統(tǒng)能力: SystemCapability.PowerManager.PowerManager.Core
需要權(quán)限: ohos.permission.RUNNING_LOCK
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
name | string | 是 | 鎖的名字。 |
type | RunningLockType | 是 | 要?jiǎng)?chuàng)建的鎖的類型。 |
callback | AsyncCallback<RunningLock> | 是 | 回調(diào)函數(shù)。當(dāng)創(chuàng)建鎖成功,err為undefined,data為創(chuàng)建的RunningLock;否則為錯(cuò)誤對(duì)象。 |
示例:
- runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND, (err, lock) => {
- if (typeof err === 'undefined') {
- console.info('created running lock: ' + lock);
- } else {
- console.error('create running lock failed, err: ' + err);
- }
- });
createRunningLock(name: string, type: RunningLockType): Promise<RunningLock>
從API version 9開(kāi)始不再維護(hù),建議使用runningLock.create替代。
創(chuàng)建RunningLock鎖。
系統(tǒng)能力: SystemCapability.PowerManager.PowerManager.Core
需要權(quán)限: ohos.permission.RUNNING_LOCK
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
name | string | 是 | 鎖的名字。 |
type | RunningLockType | 是 | 要?jiǎng)?chuàng)建的鎖的類型。 |
返回值:
類型 | 說(shuō)明 |
---|---|
Promise<RunningLock> | Promise對(duì)象,返回RunningLock鎖對(duì)象。 |
示例:
- runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND)
- .then(lock => {
- console.info('created running lock: ' + lock);
- })
- .catch(err => {
- console.log('create running lock failed, err: ' + err);
- });
hold(timeout: number): void
鎖定和持有RunningLock。
系統(tǒng)能力: SystemCapability.PowerManager.PowerManager.Core
需要權(quán)限: ohos.permission.RUNNING_LOCK
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
timeout | number | 是 | 鎖定和持有RunningLock的時(shí)長(zhǎng),單位:毫秒。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)RunningLock鎖錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
4900101 | 連接服務(wù)器失敗。 |
示例:
- runningLock.create('running_lock_test', runningLock.RunningLockType.BACKGROUND)
- .then(lock => {
- console.info('create running lock success');
- try {
- lock.hold(500);
- console.info('hold running lock success');
- } catch(err) {
- console.error('hold running lock failed, err: ' + err);
- }
- })
- .catch(err => {
- console.error('create running lock failed, err: ' + err);
- });
unhold(): void
釋放RunningLock鎖。
系統(tǒng)能力: SystemCapability.PowerManager.PowerManager.Core
需要權(quán)限: ohos.permission.RUNNING_LOCK
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)RunningLock鎖錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
4900101 | 連接服務(wù)器失敗。 |
示例:
- runningLock.create('running_lock_test', runningLock.RunningLockType.BACKGROUND)
- .then(lock => {
- console.info('create running lock success');
- try {
- lock.unhold();
- console.info('unhold running lock success');
- } catch(err) {
- console.error('unhold running lock failed, err: ' + err);
- }
- })
- .catch(err => {
- console.error('create running lock failed, err: ' + err);
- });
isHolding(): boolean
查詢當(dāng)前RunningLock是持有狀態(tài)還是釋放狀態(tài)。
系統(tǒng)能力: SystemCapability.PowerManager.PowerManager.Core
返回值:
類型 | 說(shuō)明 |
---|---|
boolean | 返回true表示當(dāng)前RunningLock是持有狀態(tài),返回false表示當(dāng)前RunningLock是釋放狀態(tài)。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)RunningLock鎖錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
4900101 | 連接服務(wù)失敗。 |
示例:
- runningLock.create('running_lock_test', runningLock.RunningLockType.BACKGROUND)
- .then(lock => {
- console.info('create running lock success');
- try {
- var isHolding = lock.isHolding();
- console.info('check running lock holding status: ' + isHolding);
- } catch(err) {
- console.error('check running lock holding status failed, err: ' + err);
- }
- })
- .catch(err => {
- console.error('create running lock failed, err: ' + err);
- });
lock(timeout: number): void
從API version 9開(kāi)始不再維護(hù),建議使用RunningLock.hold替代。
鎖定和持有RunningLock。
系統(tǒng)能力: SystemCapability.PowerManager.PowerManager.Core
需要權(quán)限: ohos.permission.RUNNING_LOCK
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
timeout | number | 是 | 鎖定和持有RunningLock的時(shí)長(zhǎng),單位:毫秒。 |
示例:
- runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND)
- .then(lock => {
- lock.lock(500);
- console.info('create running lock and lock success');
- })
- .catch(err => {
- console.error('create running lock failed, err: ' + err);
- });
unlock(): void
從API version 9開(kāi)始不再維護(hù),建議使用RunningLock.unhold替代。
釋放RunningLock鎖。
系統(tǒng)能力: SystemCapability.PowerManager.PowerManager.Core
需要權(quán)限: ohos.permission.RUNNING_LOCK
示例:
- runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND)
- .then(lock => {
- lock.unlock();
- console.info('create running lock and unlock success');
- })
- .catch(err => {
- console.error('create running lock failed, err: ' + err);
- });
isUsed(): boolean
從API version 9開(kāi)始不再維護(hù),建議使用RunningLock.isHolding替代。
查詢當(dāng)前RunningLock是持有狀態(tài)還是釋放狀態(tài)。
系統(tǒng)能力: SystemCapability.PowerManager.PowerManager.Core
返回值:
類型 | 說(shuō)明 |
---|---|
boolean | 返回true表示當(dāng)前RunningLock是持有狀態(tài),返回false表示當(dāng)前RunningLock是釋放狀態(tài)。 |
示例:
- runningLock.createRunningLock('running_lock_test', runningLock.RunningLockType.BACKGROUND)
- .then(lock => {
- var isUsed = lock.isUsed();
- console.info('check running lock used status: ' + isUsed);
- })
- .catch(err => {
- console.error('check running lock used status failed, err: ' + err);
- });
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: