harmony 鸿蒙@ohos.screenLock (Screen Lock)

  • 2022-08-09
  • 浏览 (699)

@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 Type Description
beginWakeUp Wakeup starts.
endWakeUp Wakeup ends.
beginScreenOn Screen turn-on starts.
endScreenOn Screen turn-on ends.
beginScreenOff Screen turn-off starts.
endScreenOff Screen turn-off ends.
unlockScreen The screen is unlocked.
lockScreen The screen is locked.
beginExitAnimation Exit animation starts.
beginSleep The device enters sleep mode.
endSleep The device exits sleep mode.
changeUser The user is switched.
screenlockEnabled Screen lock is enabled.
serviceRestart The 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.

Name Type Mandatory Description
eventType EventType Yes System event type.
params string Yes System 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

Type Description
boolean Returns 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

Name Type Mandatory Description
callback AsyncCallback<boolean> Yes Callback 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.

ID Error Message
13200002 the 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

Type Description
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.

ID Error Message
13200002 the 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

Name Type Mandatory Description
callback AsyncCallback<boolean> Yes Callback 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.

ID Error Message
13200002 the 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

Type Description
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.

ID Error Message
13200002 the 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

Name Type Mandatory Description
callback Callback<SystemEvent> Yes Callback for system events related to screen locking.

Return value

Type Description
boolean Returns true if the callback is registered successfully; returns false otherwise.

Error codes

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

ID Error Message
13200002 the 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&lt;boolean&gt;): 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

Name Type Mandatory Description
event String Yes Event type.
- “unlockScreenResult”: Screen unlock result.
- “lockScreenResult”: Screen lock result.
- “screenDrawDone”: Screen drawing is complete.
parameter number Yes Result.
- 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.
callback AsyncCallback<boolean> Yes Callback 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.

ID Error Message
13200002 the 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&lt;boolean&gt;

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

Name Type Mandatory Description
event String Yes Event type.
- “unlockScreenResult”: Screen unlock result.
- “lockScreenResult”: Screen lock result.
- “screenDrawDone”: Screen drawing is complete.
parameter number Yes Result.
- 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

Type Description
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.

ID Error Message
13200002 the 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&lt;boolean&gt;): 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

Name Type Mandatory Description
callback AsyncCallback&lt;boolean&gt; Yes Callback 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&lt;boolean&gt;

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

Type Description
Promise&lt;boolean&gt; 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&lt;boolean&gt;): 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

Name Type Mandatory Description
callback AsyncCallback&lt;boolean&gt; Yes Callback 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&lt;boolean&gt;

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

Type Description
Promise&lt;boolean&gt; 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&lt;void&gt;): 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

Name Type Mandatory Description
callback AsyncCallback&lt;void&gt; Yes Callback 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&lt;void&gt;

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

Type Description
Promise&lt;void&gt; 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)

0  赞