harmony 鸿蒙@ohos.display (Display) (System API)

  • 2025-06-12
  • 浏览 (4)

@ohos.display (Display) (System API)

The Display module provides APIs for managing displays, such as obtaining information about the default display, obtaining information about all displays, and listening for the addition and removal of displays.

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.

  • This topic describes only system APIs provided by the module. For details about its public APIs, see @ohos.display (Display).

Modules to Import

import { display } from '@kit.ArkUI';

display.hasPrivateWindow9+

hasPrivateWindow(displayId: number): boolean

Checks whether there is a visible privacy window on a display. The privacy window can be set by calling setWindowPrivacyMode(). The content in the privacy window cannot be captured or recorded.

System API: This is a system API.

System capability: SystemCapability.WindowManager.WindowManager.Core

Parameters

Name Type Mandatory Description
displayId number Yes ID of the display. The value must be an integer greater than or equal to 0.

Return value

Type Description
boolean Whether there is a visible privacy window on the display. The value true means that there is a visible privacy window on the display, and false means the opposite.

Error codes

For details about the error codes, see Universal Error Codes and Display Error Codes.

ID Error Message
202 Permission verification failed. A non-system application calls a system API.
401 Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed.
1400003 This display manager service works abnormally.

Example

import { display } from '@kit.ArkUI';

let displayClass: display.Display|null = null;
try {
  displayClass = display.getDefaultDisplaySync();

  let ret: boolean = true;
  try {
    ret = display.hasPrivateWindow(displayClass.id);
  } catch (exception) {
    console.error('Failed to check has privateWindow or not. Code: ' + JSON.stringify(exception));
  }
  if (ret == undefined) {
    console.log("Failed to check has privateWindow or not.");
  }
  if (ret) {
    console.log("There has privateWindow.");
  } else if (!ret) {
    console.log("There has no privateWindow.");
  }
} catch (exception) {
  console.error('Failed to obtain the default display object. Code: ' + JSON.stringify(exception));
}

display.on(‘privateModeChange’)10+

on(type: ‘privateModeChange’, callback: Callback<boolean>): void

Subscribes to privacy mode changes of this display. When there is a privacy window in the foreground of the display, the display is in privacy mode, and the content in the privacy window cannot be captured or recorded.

System API: This is a system API.

System capability: SystemCapability.WindowManager.WindowManager.Core

Parameters

Name Type Mandatory Description
type string Yes Event type. The value is fixed at ‘privateModeChange’, indicating that the privacy mode of the display is changed.
callback Callback<boolean> Yes Callback used to return whether the privacy mode of the display is changed. The value true means that the display changes to the privacy mode, and false means the opposite.

Error codes

For details about the error codes, see Universal Error Codes.

ID Error Message
202 Permission verification failed. A non-system application calls a system API.
401 Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.

Example

import { Callback } from '@kit.BasicServicesKit';

let callback: Callback<boolean> = (data: boolean) => {
  console.info('Listening enabled. Data: ' + JSON.stringify(data));
};
try {
  display.on("privateModeChange", callback);
} catch (exception) {
  console.error('Failed to register callback. Code: ' + JSON.stringify(exception));
}

display.off(‘privateModeChange’)10+

off(type: ‘privateModeChange’, callback?: Callback&lt;boolean&gt;): void

Unsubscribes from privacy mode changes of this display. When there is a privacy window in the foreground of the display, the display is in privacy mode, and the content in the privacy window cannot be captured or recorded.

System API: This is a system API.

System capability: SystemCapability.WindowManager.WindowManager.Core

Parameters

Name Type Mandatory Description
type string Yes Event type. The value is fixed at ‘privateModeChange’, indicating that the privacy mode of the display is changed.
callback Callback&lt;boolean&gt; No Callback used to return whether the privacy mode of the display is changed. The value true means that the display changes to the privacy mode, and false means the opposite. If this parameter is not specified, all subscriptions to the specified event are canceled.

Error codes

For details about the error codes, see Universal Error Codes.

ID Error Message
202 Permission verification failed. A non-system application calls a system API.
401 Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.

Example

try {
  display.off("privateModeChange");
} catch (exception) {
  console.error('Failed to unregister callback. Code: ' + JSON.stringify(exception));
}

display.setFoldDisplayMode10+

setFoldDisplayMode(mode: FoldDisplayMode): void

Sets the display mode of the foldable device. This API is unavailable for 2-in-1 devices.

System API: This is a system API.

System capability: SystemCapability.Window.SessionManager

Parameters

Name Type Mandatory Description
mode FoldDisplayMode Yes Display mode.

Error codes

For details about the error codes, see Universal Error Codes and Display Error Codes.

ID Error Message
202 Permission verification failed. A non-system application calls a system API.
401 Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
1400003 This display manager service works abnormally.

Example

import { display } from '@kit.ArkUI';

try {
  let mode: display.FoldDisplayMode = display.FoldDisplayMode.FOLD_DISPLAY_MODE_FULL;
  display.setFoldDisplayMode(mode);
} catch (exception) {
  console.error('Failed to change the fold display mode. Code: ' + JSON.stringify(exception));
}

display.setFoldDisplayMode19+

setFoldDisplayMode(mode: FoldDisplayMode, reason: string): void

Sets the display mode of the foldable device, with the reason for the change specified. This API is unavailable for 2-in-1 devices.

System API: This is a system API.

System capability: SystemCapability.Window.SessionManager

Parameters

Name Type Mandatory Description
mode FoldDisplayMode Yes Display mode.
reason string No Reason for changing the display mode. If this parameter is not set, an empty string is used by default.

Error codes

For details about the error codes, see Universal Error Codes and Display Error Codes.

ID Error Message
202 Permission verification failed. A non-system application calls a system API.
1400003 This display manager service works abnormally.

Example

import { display } from '@kit.ArkUI';

try {
  let mode: display.FoldDisplayMode = display.FoldDisplayMode.FOLD_DISPLAY_MODE_MAIN;
  display.setFoldDisplayMode(mode, 'backSelfie');
} catch (exception) {
  console.error(`Failed to change the fold display mode. Code: ${exception}`);
}

display.setFoldStatusLocked11+

setFoldStatusLocked(locked: boolean): void

Sets whether to lock the current fold status of the foldable device.

System API: This is a system API.

System capability: SystemCapability.Window.SessionManager

Parameters

Name Type Mandatory Description
locked boolean Yes Whether to lock the current fold status of the foldable device. The value true means to lock the current fold status, and false means the opposite.

Error codes

For details about the error codes, see Universal Error Codes and Display Error Codes.

ID Error Message
202 Permission verification failed. A non-system application calls a system API.
401 Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
1400003 This display manager service works abnormally.

Example

import { display } from '@kit.ArkUI';

try {
  let locked: boolean = false;
  display.setFoldStatusLocked(locked);
} catch (exception) {
  console.error('Failed to change the fold status locked mode. Code: ' + JSON.stringify(exception));
}

display.addVirtualScreenBlocklist18+

addVirtualScreenBlocklist(windowIds: Array<number>): Promise<void>

Adds windows to the list of windows that are not allowed to be displayed during casting. This API takes effect only for the main window of an application or system windows. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Window.SessionManager

Parameters

Name Type Mandatory Description
windowIds Array<number> Yes List of window IDs. If a child window ID is passed in, it will not take effect. The window ID is an integer greater than 0. You are advised to call getWindowProperties() to obtain the window ID.

Return value

Type Description
Promise&lt;void&gt; Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Display Error Codes.

ID Error Message
202 Permission verification failed. A non-system application calls a system API.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801 Capability not supported.Function addVirtualScreenBlocklist can not work correctly due to limited device capabilities.
1400003 This display manager service works abnormally.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { display, window } from '@kit.ArkUI';

export default class EntryAbility extends UIAbility {
  // ...
  onWindowStageCreate(windowStage: window.WindowStage) {
    // ...
    let windowId = windowStage.getMainWindowSync().getWindowProperties().id;
    let windowIds = [windowId];

    let promise = display.addVirtualScreenBlocklist(windowIds);
    promise.then(() => {
      console.info('Succeeded in adding virtual screen blocklist.');
    }).catch((err: BusinessError) => {
      console.error('Failed to add virtual screen blocklist. Code: ' + JSON.stringify(err));
    })
  }
}

display.removeVirtualScreenBlocklist18+

removeVirtualScreenBlocklist(windowIds: Array<number>): Promise<void>

Removes windows from the list of windows that are not allowed to be displayed during casting. This API takes effect only for the main window of an application or system windows. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Window.SessionManager

Parameters

Name Type Mandatory Description
windowIds Array<number> Yes List of window IDs. If a child window ID is passed in, it will not take effect. The window ID is an integer greater than 0. You are advised to call getWindowProperties() to obtain the window ID.

Return value

Type Description
Promise&lt;void&gt; Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Display Error Codes.

ID Error Message
202 Permission verification failed. A non-system application calls a system API.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801 Capability not supported.Function removeVirtualScreenBlocklist can not work correctly due to limited device capabilities.
1400003 This display manager service works abnormally.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { display, window } from '@kit.ArkUI';

export default class EntryAbility extends UIAbility {
  // ...
  onWindowStageCreate(windowStage: window.WindowStage) {
    // ...
    let windowId = windowStage.getMainWindowSync().getWindowProperties().id;
    let windowIds = [windowId];

    let promise = display.addVirtualScreenBlocklist(windowIds);
    promise.then(() => {
      console.info('Succeeded in adding virtual screen blocklist.');
    }).catch((err: BusinessError) => {
      console.error('Failed to add virtual screen blocklist. Code: ' + JSON.stringify(err));
    })

    promise = display.removeVirtualScreenBlocklist(windowIds);
    promise.then(() => {
      console.info('Succeeded in removing virtual screen blocklist.');
    }).catch((err: BusinessError) => {
      console.error('Failed to remove virtual screen blocklist. Code: ' + JSON.stringify(err));
    })
  }
}

Display

Implements a Display instance, with properties and APIs defined.

Before calling any API in Display, you must use getAllDisplays() or getDefaultDisplaySync() to obtain a Display instance.

hasImmersiveWindow11+

hasImmersiveWindow(callback: AsyncCallback&lt;boolean&gt;): void

Checks whether this display contains an immersive window. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Window.SessionManager

Parameters

Name Type Mandatory Description
callback AsyncCallback&lt;boolean&gt; Yes Callback used to return the result. The value true means that the display contains an immersive window, and false means the opposite.

Error codes

For details about the error codes, see Universal Error Codes and Display Error Codes.

ID Error Message
202 Permission verification failed. A non-system application calls a system API.
801 Capability not supported on this device.
1400001 Invalid display or screen.
1400003 This display manager service works abnormally.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { display } from '@kit.ArkUI';

let displayClass: display.Display|null = null;
displayClass = display.getDefaultDisplaySync();
displayClass.hasImmersiveWindow((err: BusinessError, data) => {
    const errCode: number = err.code;
    if (errCode) {
      console.error('Failed to check whether there is immersive window. Code: ' + JSON.stringify(err));
      return;
    }
    console.info('Succeeded in checking whether there is immersive window. data: ' + JSON.stringify(data));
});

hasImmersiveWindow11+

hasImmersiveWindow(): Promise&lt;boolean&gt;

Checks whether this display contains an immersive window. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Window.SessionManager

Return value

Type Description
Promise&lt;boolean&gt; Promise used to return the result. The value true means that the display contains an immersive window, and false means the opposite.

Error codes

For details about the error codes, see Universal Error Codes and Display Error Codes.

ID Error Message
202 Permission verification failed. A non-system application calls a system API.
801 Capability not supported on this device.
1400001 Invalid display or screen.
1400003 This display manager service works abnormally.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { display } from '@kit.ArkUI';

let displayClass: display.Display|null = null;
displayClass = display.getDefaultDisplaySync();
let promise = displayClass.hasImmersiveWindow();
promise.then((data) => {
  console.info('Succeeded in checking whether there is immersive window. data: ' + JSON.stringify(data));
}).catch((err: BusinessError) => {
  console.error('Failed to check whether there is immersive window. Code: ' + JSON.stringify(err));
})

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkUI

harmony 鸿蒙ARKUI_TextPickerCascadeRangeContent

harmony 鸿蒙ARKUI_TextPickerRangeContent

harmony 鸿蒙ArkUI_AnimateCompleteCallback

harmony 鸿蒙ArkUI_AttributeItem

harmony 鸿蒙ArkUI_ColorStop

harmony 鸿蒙ArkUI_ContextCallback

harmony 鸿蒙ArkUI_EventModule

harmony 鸿蒙ArkUI_ExpectedFrameRateRange

harmony 鸿蒙ArkUI_IntOffset

0  赞