harmony 鸿蒙@ohos.screenLock (Screen Lock)

2022-08-09 浏览 (918)

@ohos.screenLock (Screen Lock)

The screenlock module is a system module in OpenHarmony. It provides APIs for screen lock applications to subscribe to screen lock status changes as well as callbacks for them to receive the results. It also provides APIs for third-party applications to unlock the screen, obtain the screen locked status, and check whether a lock screen password has been set.

NOTE

The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import screenLock from '@ohos.screenLock';

EventType9+

Defines the system event type.

System capability: SystemCapability.MiscServices.ScreenLock

System API: This is a system API.

Event TypeDescription
beginWakeUpWakeup starts.
endWakeUpWakeup ends.
beginScreenOnScreen turn-on starts.
endScreenOnScreen turn-on ends.
beginScreenOffScreen turn-off starts.
endScreenOffScreen turn-off ends.
unlockScreenThe screen is unlocked.
lockScreenThe screen is locked.
beginExitAnimationExit animation starts.
beginSleepThe device enters sleep mode.
endSleepThe device exits sleep mode.
changeUserThe user is switched.
screenlockEnabledScreen lock is enabled.
serviceRestartThe screen lock service is restarted.

SystemEvent9+

Defines the structure of the system event callback.

System capability: SystemCapability.MiscServices.ScreenLock

System API: This is a system API.

NameTypeMandatoryDescription
eventTypeEventTypeYesSystem event type.
paramsstringYesSystem event parameters.

screenLock.isLocked9+

isLocked(): boolean

Checks whether the screen is locked.

System capability: SystemCapability.MiscServices.ScreenLock

System API: This is a system API.

Return value

TypeDescription
booleanReturns true if the screen is locked; returns false otherwise.

Example

let isLocked = screenLock.isLocked();

screenLock.unlock9+

unlock(callback: AsyncCallback<boolean>): void

Unlocks the screen. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.ScreenLock

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<boolean>YesCallback used to return the result. The value true means that the screen is unlocked successfully, and false means that screen unlocked is canceled.

Error codes

For details about the error codes, see Screen Lock Management Error Codes.

IDError Message
13200002the screenlock management service is abnormal.

Example

import { BusinessError } from '@ohos.base';

screenLock.unlock((err: BusinessError, data: Boolean) => {
  if (err) {
    console.error(`Failed to unlock the screen, Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in unlocking the screen. result: ${data}`);
});

screenLock.unlock9+

unlock(): Promise<boolean>

Unlocks the screen. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.ScreenLock

System API: This is a system API.

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true means that the screen is unlocked successfully, and false means that screen unlocked is canceled.

Error codes

For details about the error codes, see Screen Lock Management Error Codes.

IDError Message
13200002the screenlock management service is abnormal.

Example

import { BusinessError } from '@ohos.base';

screenLock.unlock().then((data: Boolean) => {
  console.info(`Succeeded in unlocking the screen. result: ${data}`);
}).catch((err: BusinessError) => {
  console.error(`Failed to unlock the screen, Code: ${err.code}, message: ${err.message}`);
});

screenLock.lock9+

lock(callback: AsyncCallback<boolean>): void

Locks the screen. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.ScreenLock

Required permissions: ohos.permission.ACCESS_SCREEN_LOCK_INNER

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<boolean>YesCallback used to return the result. The value true means that the screen is locked successfully, and false means the opposite.

Error codes

For details about the error codes, see Screen Lock Management Error Codes.

IDError Message
13200002the screenlock management service is abnormal.

Example

import { BusinessError } from '@ohos.base';

screenLock.lock((err: BusinessError, data: Boolean) => {
  if (err) {
    console.error(`Failed to lock the screen, Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in locking the screen. result: ${data}`);
});

screenLock.lock9+

lock(): Promise<boolean>

Locks the screen. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.ScreenLock

Required permissions: ohos.permission.ACCESS_SCREEN_LOCK_INNER

System API: This is a system API.

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true means that the screen is locked successfully, and false means the opposite.

Error codes

For details about the error codes, see Screen Lock Management Error Codes.

IDError Message
13200002the screenlock management service is abnormal.

Example

import { BusinessError } from '@ohos.base';

screenLock.lock().then((data: Boolean) => {
  console.info(`Succeeded in locking the screen. result: ${data}`);
}).catch((err: BusinessError) => {
  console.error(`Failed to lock the screen, Code: ${err.code}, message: ${err.message}`);
});

screenLock.onSystemEvent9+

onSystemEvent(callback: Callback<SystemEvent>): boolean

Registers a callback for system events related to screen locking. This API can be called only by screen lock applications.

System capability: SystemCapability.MiscServices.ScreenLock

Required permissions: ohos.permission.ACCESS_SCREEN_LOCK_INNER

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
callbackCallback<SystemEvent>YesCallback for system events related to screen locking.

Return value

TypeDescription
booleanReturns true if the callback is registered successfully; returns false otherwise.

Error codes

For details about the error codes, see Screen Lock Management Error Codes.

IDError Message
13200002the screenlock management service is abnormal.

Example

try {
  let isSuccess = screenLock.onSystemEvent((event: screenLock.SystemEvent) => {
    console.log(`Succeeded in Registering the system event which related to screenlock. eventType: ${event.eventType}`)
  });
} catch (err) {
  let error = err as BusinessError;
  console.error(`Failed to register the system event which related to screenlock, Code: ${error.code}, message: ${error.message}`)
}

screenLock.sendScreenLockEvent9+

sendScreenLockEvent(event: String, parameter: number, callback: AsyncCallback<boolean>): void

Sends an event to the screen lock service. This API can be called only by screen lock applications. It uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.ScreenLock

Required permissions: ohos.permission.ACCESS_SCREEN_LOCK_INNER

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
eventStringYesEvent type.
- "unlockScreenResult": Screen unlock result.
- "lockScreenResult": Screen lock result.
- "screenDrawDone": Screen drawing is complete.
parameternumberYesResult.
- 0: The operation is successful. For example, the screen is locked or unlocked successfully.
- 1, the operation fails. For example, screen locking or unlocking fails.
- 2: The operation is canceled. For example, screen locking or unlocking is canceled.
callbackAsyncCallback<boolean>YesCallback used to return the result. The value true means that the event is sent successfully, and false means the opposite.

Error codes

For details about the error codes, see Screen Lock Management Error Codes.

IDError Message
13200002the screenlock management service is abnormal.

Example

import { BusinessError } from '@ohos.base';

screenLock.sendScreenLockEvent('unlockScreenResult', 0, (err: BusinessError, result: Boolean) => {
  if (err) {
    console.error(`Failed to send screenlock event, Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in Sending screenlock event. result: ${result}`);
});

screenLock.sendScreenLockEvent9+

sendScreenLockEvent(event: String, parameter: number): Promise<boolean>

Sends an event to the screen lock service. This API can be called only by screen lock applications. It uses a promise to return the result.

System capability: SystemCapability.MiscServices.ScreenLock

Required permissions: ohos.permission.ACCESS_SCREEN_LOCK_INNER

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
eventStringYesEvent type.
- "unlockScreenResult": Screen unlock result.
- "lockScreenResult": Screen lock result.
- "screenDrawDone": Screen drawing is complete.
parameternumberYesResult.
- 0: The operation is successful. For example, the screen is locked or unlocked successfully.
- 1, the operation fails. For example, screen locking or unlocking fails.
- 2: The operation is canceled. For example, screen locking or unlocking is canceled.

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true means that the event is sent successfully, and false means the opposite.

Error codes

For details about the error codes, see Screen Lock Management Error Codes.

IDError Message
13200002the screenlock management service is abnormal.

Example

import { BusinessError } from '@ohos.base';

screenLock.sendScreenLockEvent('unlockScreenResult', 0).then((result: Boolean) => {
  console.info(`Succeeded in Sending screenlock event. result: ${result}`);
}).catch((err: BusinessError) => {
  console.error(`Failed to send screenlock event, Code: ${err.code}, message: ${err.message}`);
});

screenLock.isScreenLocked(deprecated)

isScreenLocked(callback: AsyncCallback<boolean>): void

Checks whether the screen is locked. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9.

System capability: SystemCapability.MiscServices.ScreenLock

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<boolean>YesCallback used to return the result. The value true means that the screen is locked, and false means the opposite.

Example

import { BusinessError } from '@ohos.base';

screenLock.isScreenLocked((err: BusinessError, data: Boolean)=>{      
  if (err) {
    console.error(`Failed to obtain whether the screen is locked, Code: ${err.code}, message: ${err.message}`);
    return;    
  }
  console.info(`Succeeded in Obtaining whether the screen is locked. result: ${data}`);
});

screenLock.isScreenLocked(deprecated)

isScreenLocked(): Promise<boolean>

Checks whether the screen is locked. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9.

System capability: SystemCapability.MiscServices.ScreenLock

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true means that the screen is locked, and false means the opposite.

Example

import { BusinessError } from '@ohos.base';

screenLock.isScreenLocked().then((data: Boolean) => {
  console.info(`Succeeded in Obtaining whether the screen is locked. result: ${data}`);
}).catch((err: BusinessError) => {
  console.error(`Failed to obtain whether the screen is locked, Code: ${err.code}, message: ${err.message}`);
});

screenLock.isSecureMode(deprecated)

isSecureMode(callback: AsyncCallback<boolean>): void

Checks whether the device is in secure mode. When the device is in secure mode, its screen requires a password, unlock pattern, or other user credentials to unlock. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9.

System capability: SystemCapability.MiscServices.ScreenLock

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<boolean>YesCallback used to return the result. The value true means that the device is in secure mode, and false means the opposite.

Example

import { BusinessError } from '@ohos.base';

screenLock.isSecureMode((err: BusinessError, data: Boolean)=>{
  if (err) {
    console.error(`Failed to obtain whether the device is in secure mode, Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in Obtaining whether the device is in secure mode. result: ${data}`);
});

screenLock.isSecureMode(deprecated)

isSecureMode(): Promise<boolean>

Checks whether the device is in secure mode. When the device is in secure mode, its screen requires a password, unlock pattern, or other user credentials to unlock. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9.

System capability: SystemCapability.MiscServices.ScreenLock

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true means that the device is in secure mode, and false means the opposite.

Example

import { BusinessError } from '@ohos.base';

screenLock.isSecureMode().then((data: Boolean) => {
  console.info(`Succeeded in Obtaining whether the device is in secure mode. result: ${data}`);
}).catch((err: BusinessError) => {
  console.error(`Failed to obtain whether the device is in secure mode, Code: ${err.code}, message: ${err.message}`);
});

screenLock.unlockScreen(deprecated)

unlockScreen(callback: AsyncCallback<void>): void

Unlocks the screen. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9.

System capability: SystemCapability.MiscServices.ScreenLock

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the screen is unlocked successfully, err is undefined; otherwise, err is an error object.

Example

import { BusinessError } from '@ohos.base';

screenLock.unlockScreen((err: BusinessError) => {      
  if (err) {
    console.error(`Failed to unlock the screen, Code: ${err.code}, message: ${err.message}`);
    return;    
  }
  console.info(`Succeeded unlocking the screen.`);
});

screenLock.unlockScreen(deprecated)

unlockScreen(): Promise<void>

Unlocks the screen. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9.

System capability: SystemCapability.MiscServices.ScreenLock

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import { BusinessError } from '@ohos.base';

screenLock.unlockScreen().then(() => {
  console.info('Succeeded unlocking the screen.');
}).catch((err: BusinessError) => {
  console.error(`Failed to unlock the screen, Code: ${err.code}, message: ${err.message}`);
});

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/bb82d5f40a5c4805b6ffc4377ee95ece