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