1# @ohos.arkui.uiExtension (uiExtension)(系统接口) 2 3用于EmbeddedUIExtensionAbility(或UIExtensionAbility)中获取宿主应用的窗口信息或对应的EmbeddedComponent(或UIExtensionComponent)组件的信息。 4 5> **说明** 6> 7> 从API Version 12开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8> 9> 本文仅介绍当前模块的系统接口,其他公开接口参见[@ohos.arkui.uiExtension (uiExtension)](js-apis-arkui-uiExtension.md)。 10 11## 导入模块 12 13``` 14import { uiExtension } from '@kit.ArkUI' 15``` 16 17## WindowProxy 18 19### hideNonSecureWindows 20 21hideNonSecureWindows(shouldHide: boolean): Promise\<void> 22 23设置是否隐藏不安全窗口。 24 25> **说明:** 26> 27> 不安全窗口是指可能遮挡EmbeddedComponent(或UIExtensionComponent)组件的窗口,如全局悬浮窗、宿主子窗口和宿主创建的Dialog窗口(不包括系统应用创建的上述类型窗口)。当EmbeddedComponent(或UIExtensionComponent)组件被用来显示敏感操作提示内容时,可以选择隐藏不安全窗口,保护敏感操作提示内容不会被遮挡。当EmbeddedComponent(或UIExtensionComponent)组件不显示或销毁时需要让不安全窗口重新显示。使用CreateModalUIExtension接口创建的UIExtensionComponent会默认隐藏不安全窗口,若要取消隐藏,需要申请ohos.permission.ALLOW_SHOW_NON_SECURE_WINDOWS权限,并调用本接口将shouldHide设为false。 28> 29> 此接口在2in1设备上调用不生效。 30 31**系统能力**:SystemCapability.ArkUI.ArkUI.Full 32 33**系统接口**:此接口为系统接口,三方应用不支持调用。 34 35**参数:** 36 37| 参数名 | 类型 | 必填 | 说明 | 38| ----------- | ------------------------- | ---- | ---------- | 39| shouldHide | boolean | 是 | 指示是否隐藏不安全窗口,true表示隐藏,false表示不隐藏。 | 40 41**返回值:** 42 43| 类型 | 说明 | 44| ------------------- | ------------------------- | 45| Promise<void> | 无返回结果的Promise对象。 | 46 47**错误码:** 48 49| 错误码ID | 错误信息 | 50| -------- | --------------------------------- | 51| 202 | Permission verification failed. A non-system application calls a system API. | 52| 401 | Parameter error. Possible causes: <br> 1. Mandatory parameters are left unspecified. <br> 2. Incorrect parameters types. <br> 3. Parameter verification failed. | 53| 1300002 | Abnormal state. Possible causes: <br> 1. Permission denied. Interface caller does not have permission "ohos.permission.ALLOW_SHOW_NON_SECURE_WINDOWS". <br> 2. The UIExtension window proxy is abnormal. | 54| 1300003 | This window manager service works abnormally. | 55 56**示例** 57 58```ts 59// ExtensionProvider.ts 60 61import { UIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 62import { BusinessError } from '@kit.BasicServicesKit'; 63 64export default class EntryAbility extends UIExtensionAbility { 65 onSessionCreate(want: Want, session: UIExtensionContentSession) { 66 const extensionHostWindow = session.getUIExtensionHostWindowProxy(); 67 // 隐藏非安全窗口 68 extensionHostWindow.hideNonSecureWindows(true).then(()=> { 69 console.log(`Succeeded in hiding the non-secure windows.`); 70 }).catch((err: BusinessError)=> { 71 console.log(`Failed to hide the non-secure windows. Cause:${JSON.stringify(err)}`); 72 }) 73 } 74 onSessionDestroy(session: UIExtensionContentSession) { 75 const extensionHostWindow = session.getUIExtensionHostWindowProxy(); 76 // 取消隐藏非安全窗口 77 extensionHostWindow.hideNonSecureWindows(false).then(()=> { 78 console.log(`Succeeded in showing the non-secure windows.`); 79 }).catch((err: BusinessError)=> { 80 console.log(`Failed to show the non-secure windows. Cause:${JSON.stringify(err)}`); 81 }) 82 } 83} 84``` 85 86### setWaterMarkFlag 87 88setWaterMarkFlag(enable: boolean): Promise<void> 89 90为当前窗口添加或删除安全水印标志,使用Promise异步回调。 91> **说明:** 92> 93> 添加安全水印标志后,窗口在前台时会将当前全屏幕覆盖水印。全屏、悬浮窗、分屏等场景下只要有添加了安全水印标志的窗口在前台,就会显示全屏水印。 94 95**系统能力**:SystemCapability.ArkUI.ArkUI.Full 96 97**系统接口**:此接口为系统接口,三方应用不支持调用。 98 99**参数:** 100 101| 参数名 | 类型 | 必填 | 说明 | 102| ------ | ------- | --- | ------------------------------------------------ | 103| enable | boolean | 是 | 是否对窗口添加标志位。true表示添加,false表示删除。 | 104 105**返回值:** 106 107| 类型 | 说明 | 108| ------------------- | ------------------------- | 109| Promise<void> | 无返回结果的Promise对象。 | 110 111**错误码:** 112 113| 错误码ID | 错误信息 | 114| ------- | ---------------------------------------------- | 115| 1300002 | This window state is abnormal. | 116| 1300003 | This window manager service works abnormally. | 117| 1300008 | The operation is on invalid display. | 118 119**示例** 120 121```ts 122// ExtensionProvider.ts 123import { UIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 124import { BusinessError } from '@kit.BasicServicesKit'; 125 126export default class EntryAbility extends UIExtensionAbility { 127 onSessionCreate(want: Want, session: UIExtensionContentSession) { 128 const extensionHostWindow = session.getUIExtensionHostWindowProxy(); 129 // 添加安全水印标志 130 extensionHostWindow.setWaterMarkFlag(true).then(() => { 131 console.log(`Succeeded in setting water mark flag of window.`); 132 }).catch((err: BusinessError) => { 133 console.log(`Failed to setting water mark flag of window. Cause:${JSON.stringify(err)}`); 134 }) 135 } 136 onSessionDestroy(session: UIExtensionContentSession) { 137 const extensionHostWindow = session.getUIExtensionHostWindowProxy(); 138 // 删除安全水印标志 139 extensionHostWindow.setWaterMarkFlag(false).then(() => { 140 console.log(`Succeeded in deleting water mark flag of window.`); 141 }).catch((err: BusinessError) => { 142 console.log(`Failed to deleting water mark flag of window. Cause:${JSON.stringify(err)}`); 143 }) 144 } 145} 146``` 147