1e41f4b71Sopenharmony_ci# @ohos.arkui.uiExtension (uiExtension)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci用于EmbeddedUIExtensionAbility(或UIExtensionAbility)中获取宿主应用的窗口信息或对应的EmbeddedComponent<!--Del-->(或UIExtensionComponent)<!--DelEnd-->组件的信息。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> 从API Version 12开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## 导入模块
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci```
13e41f4b71Sopenharmony_ciimport { uiExtension } from '@kit.ArkUI'
14e41f4b71Sopenharmony_ci```
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci## WindowProxy
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci### getWindowAvoidArea
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_cigetWindowAvoidArea(type: window.AvoidAreaType): window.AvoidArea
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci获取宿主应用窗口内容规避的区域;如系统栏区域、刘海屏区域、手势区域、软键盘区域等与宿主窗口内容重叠时,需要宿主窗口内容避让的区域。
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.ArkUI.ArkUI.Full
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 |
29e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
30e41f4b71Sopenharmony_ci| type |[window.AvoidAreaType](./js-apis-window.md#avoidareatype7) | 是 | 表示规避区类型。 |
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci**返回值:**
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci| 类型 | 说明 |
35e41f4b71Sopenharmony_ci| -------- | -------- |
36e41f4b71Sopenharmony_ci|[window.AvoidArea](./js-apis-window.md#avoidarea7) | 宿主窗口内容规避区域。 |
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci**错误码**:
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
43e41f4b71Sopenharmony_ci| ------- | -------- |
44e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci**示例**
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci```ts
49e41f4b71Sopenharmony_ci// ExtensionProvider.ts
50e41f4b71Sopenharmony_ciimport { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit';
51e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI';
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ciexport default class EntryAbility extends EmbeddedUIExtensionAbility {
54e41f4b71Sopenharmony_ci  onSessionCreate(want: Want, session: UIExtensionContentSession) {
55e41f4b71Sopenharmony_ci    const extensionWindow = session.getUIExtensionWindowProxy();
56e41f4b71Sopenharmony_ci    // 获取宿主应用窗口的避让信息
57e41f4b71Sopenharmony_ci    let avoidArea: window.AvoidArea | undefined = extensionWindow?.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
58e41f4b71Sopenharmony_ci    console.info(`avoidArea: ${JSON.stringify(avoidArea)}`);
59e41f4b71Sopenharmony_ci  }
60e41f4b71Sopenharmony_ci}
61e41f4b71Sopenharmony_ci```
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci### on('avoidAreaChange')
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_cion(type: 'avoidAreaChange', callback: Callback&lt;AvoidAreaInfo&gt;): void
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci注册系统规避区变化的监听。
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.ArkUI.ArkUI.Full
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 |
74e41f4b71Sopenharmony_ci| ------ | ---- | ---- | ---- |
75e41f4b71Sopenharmony_ci| type   | string | 是 | 监听的事件类型,固定为'avoidAreaChange',即系统规避区变化事件。 |
76e41f4b71Sopenharmony_ci| callback | [Callback](../apis-basic-services-kit/js-apis-base.md#callback)<[AvoidAreaInfo](#avoidareainfo)> | 是 | 回调函数:入参用于接收当前规避区的信息。 |
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci**错误码**:
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
83e41f4b71Sopenharmony_ci| ------- | -------- |
84e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci**示例**
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci```ts
89e41f4b71Sopenharmony_ci// ExtensionProvider.ts
90e41f4b71Sopenharmony_ciimport { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit';
91e41f4b71Sopenharmony_ciimport { uiExtension } from '@kit.ArkUI';
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ciexport default class EntryAbility extends EmbeddedUIExtensionAbility {
94e41f4b71Sopenharmony_ci  onSessionCreate(want: Want, session: UIExtensionContentSession) {
95e41f4b71Sopenharmony_ci    const extensionWindow = session.getUIExtensionWindowProxy();
96e41f4b71Sopenharmony_ci    // 注册避让区变化的监听
97e41f4b71Sopenharmony_ci    extensionWindow.on('avoidAreaChange', (info: uiExtension.AvoidAreaInfo) => {
98e41f4b71Sopenharmony_ci      console.info(`The avoid area of the host window is: ${JSON.stringify(info.area)}.`);
99e41f4b71Sopenharmony_ci    });
100e41f4b71Sopenharmony_ci  }
101e41f4b71Sopenharmony_ci}
102e41f4b71Sopenharmony_ci```
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci### off('avoidAreaChange')
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_cioff(type: 'avoidAreaChange', callback?: Callback&lt;AvoidAreaInfo&gt;): void
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci注销系统规避区变化的监听。
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.ArkUI.ArkUI.Full
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ci| 参数名   | 类型 | 必填 | 说明 |
115e41f4b71Sopenharmony_ci| -------- | ---- | ---- | ---  |
116e41f4b71Sopenharmony_ci| type     | string | 是 | 注销的事件类型,固定为'avoidAreaChange',即系统规避区变化事件。 |
117e41f4b71Sopenharmony_ci| callback | [Callback](../apis-basic-services-kit/js-apis-base.md#callback)<[AvoidAreaInfo](#avoidareainfo)> | 否 | 回调函数:如果传入该参数,则关闭该监听。如果未传入参数,则关闭所有系统规避区变化的监听。 |
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci**错误码**:
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                                     |
124e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
125e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. |
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci**示例**
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci```ts
130e41f4b71Sopenharmony_ci// ExtensionProvider.ts
131e41f4b71Sopenharmony_ciimport { EmbeddedUIExtensionAbility, UIExtensionContentSession } from '@kit.AbilityKit';
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ciexport default class EntryAbility extends EmbeddedUIExtensionAbility {
134e41f4b71Sopenharmony_ci  onSessionDestroy(session: UIExtensionContentSession) {
135e41f4b71Sopenharmony_ci    const extensionWindow = session.getUIExtensionWindowProxy();
136e41f4b71Sopenharmony_ci    // 注销所有避让区变化的监听
137e41f4b71Sopenharmony_ci    extensionWindow.off('avoidAreaChange');
138e41f4b71Sopenharmony_ci  }
139e41f4b71Sopenharmony_ci}
140e41f4b71Sopenharmony_ci```
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci### on('windowSizeChange')
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_cion(type: 'windowSizeChange', callback: Callback<window.Size>): void
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci注册宿主应用窗口尺寸变化的监听。
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.ArkUI.ArkUI.Full
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci| 参数名   | 类型                  | 必填 | 说明                   |
153e41f4b71Sopenharmony_ci| -------- | --------------------- | ---- | ---------------------- |
154e41f4b71Sopenharmony_ci| type     | string                | 是   | 监听的事件类型,固定为'windowSizeChange',即窗口尺寸变化事件。 |
155e41f4b71Sopenharmony_ci| callback | [Callback](../apis-basic-services-kit/js-apis-base.md#callback)<[window.Size](js-apis-window.md#size7)> | 是   | 回调函数:入参用于接收当前窗口的尺寸。 |
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ci**错误码**:
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
162e41f4b71Sopenharmony_ci| ------- | -------- |
163e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_ci**示例**
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ci```ts
168e41f4b71Sopenharmony_ci// ExtensionProvider.ts
169e41f4b71Sopenharmony_ciimport { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit';
170e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI';
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_ciexport default class EntryAbility extends EmbeddedUIExtensionAbility {
173e41f4b71Sopenharmony_ci  onSessionCreate(want: Want, session: UIExtensionContentSession) {
174e41f4b71Sopenharmony_ci    const extensionWindow = session.getUIExtensionWindowProxy();
175e41f4b71Sopenharmony_ci    // 注册宿主应用窗口大小变化的监听
176e41f4b71Sopenharmony_ci    extensionWindow.on('windowSizeChange', (size: window.Size) => {
177e41f4b71Sopenharmony_ci      console.info(`The avoid area of the host window is: ${JSON.stringify(size)}.`);
178e41f4b71Sopenharmony_ci    });
179e41f4b71Sopenharmony_ci  }
180e41f4b71Sopenharmony_ci}
181e41f4b71Sopenharmony_ci```
182e41f4b71Sopenharmony_ci
183e41f4b71Sopenharmony_ci### off('windowSizeChange')
184e41f4b71Sopenharmony_ci
185e41f4b71Sopenharmony_cioff(type: 'windowSizeChange', callback?: Callback<window.Size>): void
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ci注销宿主应用窗口尺寸变化的监听。
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
190e41f4b71Sopenharmony_ci
191e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.ArkUI.ArkUI.Full
192e41f4b71Sopenharmony_ci
193e41f4b71Sopenharmony_ci| 参数名   | 类型                  | 必填 | 说明                   |
194e41f4b71Sopenharmony_ci| -------- | --------------------- | ---- | ---------------------- |
195e41f4b71Sopenharmony_ci| type     | string                | 是   | 注销的事件类型,固定值:'windowSizeChange',即窗口尺寸变化事件。 |
196e41f4b71Sopenharmony_ci| callback | [Callback](../apis-basic-services-kit/js-apis-base.md#callback)<[window.Size](js-apis-window.md#size7)> | 否   | 回调函数:如果传入该参数,则关闭该监听。如果未传入参数,则关闭所有系统规避区变化的监听。 |
197e41f4b71Sopenharmony_ci
198e41f4b71Sopenharmony_ci**错误码**:
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。
201e41f4b71Sopenharmony_ci
202e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
203e41f4b71Sopenharmony_ci| ------- | -------- |
204e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
205e41f4b71Sopenharmony_ci
206e41f4b71Sopenharmony_ci**示例**
207e41f4b71Sopenharmony_ci
208e41f4b71Sopenharmony_ci```ts
209e41f4b71Sopenharmony_ci// ExtensionProvider.ts
210e41f4b71Sopenharmony_ciimport { EmbeddedUIExtensionAbility, UIExtensionContentSession } from '@kit.AbilityKit';
211e41f4b71Sopenharmony_ci
212e41f4b71Sopenharmony_ciexport default class EntryAbility extends EmbeddedUIExtensionAbility {
213e41f4b71Sopenharmony_ci  onSessionDestroy(session: UIExtensionContentSession) {
214e41f4b71Sopenharmony_ci    const extensionWindow = session.getUIExtensionWindowProxy();
215e41f4b71Sopenharmony_ci    // 注销宿主应用窗口大小变化的监听
216e41f4b71Sopenharmony_ci    extensionWindow.off('windowSizeChange');
217e41f4b71Sopenharmony_ci  }
218e41f4b71Sopenharmony_ci}
219e41f4b71Sopenharmony_ci```
220e41f4b71Sopenharmony_ci
221e41f4b71Sopenharmony_ci### createSubWindowWithOptions
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_cicreateSubWindowWithOptions(name: string, subWindowOptions: window.SubWindowOptions): Promise&lt;window.Window&gt;
224e41f4b71Sopenharmony_ci
225e41f4b71Sopenharmony_ci创建该WindowProxy实例下的子窗口,使用Promise异步回调。
226e41f4b71Sopenharmony_ci
227e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
228e41f4b71Sopenharmony_ci
229e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
230e41f4b71Sopenharmony_ci
231e41f4b71Sopenharmony_ci**模型约束:** 此接口仅可在Stage模型下使用。
232e41f4b71Sopenharmony_ci
233e41f4b71Sopenharmony_ci**参数:**
234e41f4b71Sopenharmony_ci
235e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明           |
236e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------- |
237e41f4b71Sopenharmony_ci| name   | string | 是   | 子窗口的名字。 |
238e41f4b71Sopenharmony_ci| subWindowOptions | [window.SubWindowOptions](js-apis-window.md#subwindowoptions12) | 是   | 子窗口参数。  |
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ci**返回值:**
241e41f4b71Sopenharmony_ci
242e41f4b71Sopenharmony_ci| 类型                             | 说明                                             |
243e41f4b71Sopenharmony_ci| -------------------------------- | ------------------------------------------------ |
244e41f4b71Sopenharmony_ci| Promise&lt;[window.Window](js-apis-window.md#window)&gt; | Promise对象。返回当前WindowProxy下创建的子窗口对象。 |
245e41f4b71Sopenharmony_ci
246e41f4b71Sopenharmony_ci**错误码:**
247e41f4b71Sopenharmony_ci
248e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。
249e41f4b71Sopenharmony_ci
250e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
251e41f4b71Sopenharmony_ci| ------- | ------------------------------ |
252e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
253e41f4b71Sopenharmony_ci| 801 | Capability not supported. Failed to call the API due to limited device capabilities. |
254e41f4b71Sopenharmony_ci| 1300002 | This window state is abnormal. |
255e41f4b71Sopenharmony_ci| 1300005 | This window proxy is abnormal. |
256e41f4b71Sopenharmony_ci
257e41f4b71Sopenharmony_ci**示例:**
258e41f4b71Sopenharmony_ci
259e41f4b71Sopenharmony_ci```ts
260e41f4b71Sopenharmony_ci// ExtensionProvider.ts
261e41f4b71Sopenharmony_ciimport { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit';
262e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI';
263e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
264e41f4b71Sopenharmony_ci
265e41f4b71Sopenharmony_ciexport default class EntryAbility extends EmbeddedUIExtensionAbility {
266e41f4b71Sopenharmony_ci  onSessionCreate(want: Want, session: UIExtensionContentSession) {
267e41f4b71Sopenharmony_ci    const extensionWindow = session.getUIExtensionWindowProxy();
268e41f4b71Sopenharmony_ci    const subWindowOpts: window.SubWindowOptions = {
269e41f4b71Sopenharmony_ci      title: 'This is a subwindow',
270e41f4b71Sopenharmony_ci      decorEnabled: true
271e41f4b71Sopenharmony_ci    };
272e41f4b71Sopenharmony_ci    // 创建子窗口
273e41f4b71Sopenharmony_ci    extensionWindow.createSubWindowWithOptions('subWindowForHost', subWindowOpts)
274e41f4b71Sopenharmony_ci      .then((subWindow: window.Window) => {
275e41f4b71Sopenharmony_ci        subWindow.setUIContent('pages/Index', (err, data) =>{
276e41f4b71Sopenharmony_ci          if (err && err.code != 0) {
277e41f4b71Sopenharmony_ci            return;
278e41f4b71Sopenharmony_ci          }
279e41f4b71Sopenharmony_ci          subWindow?.resize(300, 300, (err, data)=>{
280e41f4b71Sopenharmony_ci            if (err && err.code != 0) {
281e41f4b71Sopenharmony_ci              return;
282e41f4b71Sopenharmony_ci            }
283e41f4b71Sopenharmony_ci            subWindow?.moveWindowTo(100, 100, (err, data)=>{
284e41f4b71Sopenharmony_ci              if (err && err.code != 0) {
285e41f4b71Sopenharmony_ci                return;
286e41f4b71Sopenharmony_ci              }
287e41f4b71Sopenharmony_ci              subWindow?.showWindow((err, data) => {
288e41f4b71Sopenharmony_ci                if (err && err.code == 0) {
289e41f4b71Sopenharmony_ci                  console.info(`The subwindow has been shown!`);
290e41f4b71Sopenharmony_ci                } else {
291e41f4b71Sopenharmony_ci                  console.error(`Failed to show the subwindow!`);
292e41f4b71Sopenharmony_ci                }
293e41f4b71Sopenharmony_ci              });
294e41f4b71Sopenharmony_ci            });
295e41f4b71Sopenharmony_ci          });
296e41f4b71Sopenharmony_ci        });
297e41f4b71Sopenharmony_ci      }).catch((error: BusinessError) => {
298e41f4b71Sopenharmony_ci        console.error(`Create subwindow failed: ${JSON.stringify(error)}`);
299e41f4b71Sopenharmony_ci      })
300e41f4b71Sopenharmony_ci  }
301e41f4b71Sopenharmony_ci}
302e41f4b71Sopenharmony_ci```
303e41f4b71Sopenharmony_ci
304e41f4b71Sopenharmony_ci## AvoidAreaInfo
305e41f4b71Sopenharmony_ci
306e41f4b71Sopenharmony_ci用于表示窗口规避区的信息。
307e41f4b71Sopenharmony_ci
308e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
309e41f4b71Sopenharmony_ci
310e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.ArkUI.ArkUI.Full
311e41f4b71Sopenharmony_ci
312e41f4b71Sopenharmony_ci| 名称 | 类型                 | 必填 | 说明        |
313e41f4b71Sopenharmony_ci| ------ | -------------------- | ------------------ | ------------------ |
314e41f4b71Sopenharmony_ci| type   | [window.AvoidAreaType](js-apis-window.md#avoidareatype7) | 是 | 窗口规避区类型。   |
315e41f4b71Sopenharmony_ci| area   | [window.AvoidArea](js-apis-window.md#avoidarea7)     | 是| 窗口内容规避区域。 |
316e41f4b71Sopenharmony_ci
317e41f4b71Sopenharmony_ci
318e41f4b71Sopenharmony_ci## 完整示例
319e41f4b71Sopenharmony_ci
320e41f4b71Sopenharmony_ci本示例展示文档中所有API在EmbeddedUIExtensionAbility中的基础使用方式,示例应用的`bundleName`为"com.example.embeddeddemo", 被拉起的`EmbeddedUIExtensionAbility`为"ExampleEmbeddedAbility".
321e41f4b71Sopenharmony_ci
322e41f4b71Sopenharmony_ci- 示例应用中的EntryAbility(UIAbility)加载首页文件:`pages/Index.ets`,其中内容如下:
323e41f4b71Sopenharmony_ci
324e41f4b71Sopenharmony_ci  ```ts
325e41f4b71Sopenharmony_ci  // pages/Index.ets -- UIAbility启动时加载此页面
326e41f4b71Sopenharmony_ci  import { Want } from '@kit.AbilityKit';
327e41f4b71Sopenharmony_ci
328e41f4b71Sopenharmony_ci  @Entry
329e41f4b71Sopenharmony_ci  @Component
330e41f4b71Sopenharmony_ci  struct Index {
331e41f4b71Sopenharmony_ci    @State message: string = 'Message: '
332e41f4b71Sopenharmony_ci    private want: Want = {
333e41f4b71Sopenharmony_ci      bundleName: "com.example.embeddeddemo",
334e41f4b71Sopenharmony_ci      abilityName: "ExampleEmbeddedAbility",
335e41f4b71Sopenharmony_ci    }
336e41f4b71Sopenharmony_ci
337e41f4b71Sopenharmony_ci    build() {
338e41f4b71Sopenharmony_ci      Row() {
339e41f4b71Sopenharmony_ci        Column() {
340e41f4b71Sopenharmony_ci          Text(this.message).fontSize(30)
341e41f4b71Sopenharmony_ci          EmbeddedComponent(this.want, EmbeddedType.EMBEDDED_UI_EXTENSION)
342e41f4b71Sopenharmony_ci            .width('100%')
343e41f4b71Sopenharmony_ci            .height('90%')
344e41f4b71Sopenharmony_ci            .onTerminated((info)=>{
345e41f4b71Sopenharmony_ci              this.message = 'Termination: code = ' + info.code + ', want = ' + JSON.stringify(info.want);
346e41f4b71Sopenharmony_ci            })
347e41f4b71Sopenharmony_ci            .onError((error)=>{
348e41f4b71Sopenharmony_ci              this.message = 'Error: code = ' + error.code;
349e41f4b71Sopenharmony_ci            })
350e41f4b71Sopenharmony_ci        }
351e41f4b71Sopenharmony_ci        .width('100%')
352e41f4b71Sopenharmony_ci      }
353e41f4b71Sopenharmony_ci      .height('100%')
354e41f4b71Sopenharmony_ci    }
355e41f4b71Sopenharmony_ci  }
356e41f4b71Sopenharmony_ci  ```
357e41f4b71Sopenharmony_ci
358e41f4b71Sopenharmony_ci- EmbeddedComponent拉起的EmbeddedUIExtensionAbility在`ets/extensionAbility/ExampleEmbeddedAbility`文件中实现,内容如下:
359e41f4b71Sopenharmony_ci
360e41f4b71Sopenharmony_ci  ```ts
361e41f4b71Sopenharmony_ci  import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit';
362e41f4b71Sopenharmony_ci
363e41f4b71Sopenharmony_ci  const TAG: string = '[ExampleEmbeddedAbility]'
364e41f4b71Sopenharmony_ci  export default class ExampleEmbeddedAbility extends EmbeddedUIExtensionAbility {
365e41f4b71Sopenharmony_ci    
366e41f4b71Sopenharmony_ci    onCreate() {
367e41f4b71Sopenharmony_ci      console.info(TAG, `onCreate`);
368e41f4b71Sopenharmony_ci    }
369e41f4b71Sopenharmony_ci
370e41f4b71Sopenharmony_ci    onForeground() {
371e41f4b71Sopenharmony_ci      console.info(TAG, `onForeground`);
372e41f4b71Sopenharmony_ci    }
373e41f4b71Sopenharmony_ci
374e41f4b71Sopenharmony_ci    onBackground() {
375e41f4b71Sopenharmony_ci      console.info(TAG, `onBackground`);
376e41f4b71Sopenharmony_ci    }
377e41f4b71Sopenharmony_ci
378e41f4b71Sopenharmony_ci    onDestroy() {
379e41f4b71Sopenharmony_ci      console.info(TAG, `onDestroy`);
380e41f4b71Sopenharmony_ci    }
381e41f4b71Sopenharmony_ci
382e41f4b71Sopenharmony_ci    onSessionCreate(want: Want, session: UIExtensionContentSession) {
383e41f4b71Sopenharmony_ci      console.info(TAG, `onSessionCreate, want: ${JSON.stringify(want)}`);
384e41f4b71Sopenharmony_ci      let param: Record<string, UIExtensionContentSession> = {
385e41f4b71Sopenharmony_ci        'session': session
386e41f4b71Sopenharmony_ci      };
387e41f4b71Sopenharmony_ci      let storage: LocalStorage = new LocalStorage(param);
388e41f4b71Sopenharmony_ci      session.loadContent('pages/extension', storage);
389e41f4b71Sopenharmony_ci    }
390e41f4b71Sopenharmony_ci  }
391e41f4b71Sopenharmony_ci  ```
392e41f4b71Sopenharmony_ci
393e41f4b71Sopenharmony_ci- EmbeddedUIExtensionAbility的入口页面文件`pages/extension.ets`内容如下:
394e41f4b71Sopenharmony_ci
395e41f4b71Sopenharmony_ci  ```ts
396e41f4b71Sopenharmony_ci  import { UIExtensionContentSession } from '@kit.AbilityKit';
397e41f4b71Sopenharmony_ci  import { uiExtension, window } from '@kit.ArkUI';
398e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
399e41f4b71Sopenharmony_ci  let storage = LocalStorage.getShared()
400e41f4b71Sopenharmony_ci
401e41f4b71Sopenharmony_ci  @Entry(storage)
402e41f4b71Sopenharmony_ci  @Component
403e41f4b71Sopenharmony_ci  struct Extension {
404e41f4b71Sopenharmony_ci    @State message: string = 'EmbeddedUIExtensionAbility Index';
405e41f4b71Sopenharmony_ci    private session: UIExtensionContentSession | undefined = storage.get<UIExtensionContentSession>('session');
406e41f4b71Sopenharmony_ci    private extensionWindow: uiExtension.WindowProxy | undefined = this.session?.getUIExtensionWindowProxy();
407e41f4b71Sopenharmony_ci    private subWindow: window.Window | undefined = undefined;
408e41f4b71Sopenharmony_ci
409e41f4b71Sopenharmony_ci    aboutToAppear(): void {
410e41f4b71Sopenharmony_ci      this.extensionWindow?.on('windowSizeChange', (size: window.Size) => {
411e41f4b71Sopenharmony_ci          console.info(`size = ${JSON.stringify(size)}`);
412e41f4b71Sopenharmony_ci      });
413e41f4b71Sopenharmony_ci      this.extensionWindow?.on('avoidAreaChange', (info: uiExtension.AvoidAreaInfo) => {
414e41f4b71Sopenharmony_ci          console.info(`type = ${JSON.stringify(info.type)}, area = ${JSON.stringify(info.area)}`);
415e41f4b71Sopenharmony_ci      });
416e41f4b71Sopenharmony_ci    }
417e41f4b71Sopenharmony_ci
418e41f4b71Sopenharmony_ci    aboutToDisappear(): void {
419e41f4b71Sopenharmony_ci      this.extensionWindow?.off('windowSizeChange');
420e41f4b71Sopenharmony_ci      this.extensionWindow?.off('avoidAreaChange');
421e41f4b71Sopenharmony_ci    }
422e41f4b71Sopenharmony_ci
423e41f4b71Sopenharmony_ci    build() {
424e41f4b71Sopenharmony_ci      Column() {
425e41f4b71Sopenharmony_ci        Text(this.message)
426e41f4b71Sopenharmony_ci          .fontSize(20)
427e41f4b71Sopenharmony_ci          .fontWeight(FontWeight.Bold)
428e41f4b71Sopenharmony_ci        Button("获取系统规避区信息").width('90%').margin({top: 5, bottom: 5}).fontSize(16).onClick(() => {
429e41f4b71Sopenharmony_ci          let avoidArea: window.AvoidArea | undefined = this.extensionWindow?.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
430e41f4b71Sopenharmony_ci          console.info(`系统规避区: ${JSON.stringify(avoidArea)}`);
431e41f4b71Sopenharmony_ci        })
432e41f4b71Sopenharmony_ci        Button("创建子窗口").width('90%').margin({top: 5, bottom: 5}).fontSize(16).onClick(() => {
433e41f4b71Sopenharmony_ci          let subWindowOpts: window.SubWindowOptions = {
434e41f4b71Sopenharmony_ci              'title': 'This is a subwindow',
435e41f4b71Sopenharmony_ci              decorEnabled: true
436e41f4b71Sopenharmony_ci          };
437e41f4b71Sopenharmony_ci          this.extensionWindow?.createSubWindowWithOptions('subWindowForHost', subWindowOpts)
438e41f4b71Sopenharmony_ci              .then((subWindow: window.Window) => {
439e41f4b71Sopenharmony_ci                  this.subWindow = subWindow;
440e41f4b71Sopenharmony_ci                  this.subWindow.loadContent('pages/Index', storage, (err, data) =>{
441e41f4b71Sopenharmony_ci                      if (err && err.code != 0) {
442e41f4b71Sopenharmony_ci                          return;
443e41f4b71Sopenharmony_ci                      }
444e41f4b71Sopenharmony_ci                      this.subWindow?.resize(300, 300, (err, data)=>{
445e41f4b71Sopenharmony_ci                          if (err && err.code != 0) {
446e41f4b71Sopenharmony_ci                              return;
447e41f4b71Sopenharmony_ci                          }
448e41f4b71Sopenharmony_ci                          this.subWindow?.moveWindowTo(100, 100, (err, data)=>{
449e41f4b71Sopenharmony_ci                              if (err && err.code != 0) {
450e41f4b71Sopenharmony_ci                                  return;
451e41f4b71Sopenharmony_ci                              }
452e41f4b71Sopenharmony_ci                              this.subWindow?.showWindow((err, data) => {
453e41f4b71Sopenharmony_ci                                  if (err && err.code == 0) {
454e41f4b71Sopenharmony_ci                                      console.info(`The subwindow has been shown!`);
455e41f4b71Sopenharmony_ci                                  } else {
456e41f4b71Sopenharmony_ci                                      console.error(`Failed to show the subwindow!`);
457e41f4b71Sopenharmony_ci                                  }
458e41f4b71Sopenharmony_ci                              });
459e41f4b71Sopenharmony_ci                          });
460e41f4b71Sopenharmony_ci                      });
461e41f4b71Sopenharmony_ci                  });
462e41f4b71Sopenharmony_ci              }).catch((error: BusinessError) => {
463e41f4b71Sopenharmony_ci                  console.error(`Create subwindow failed: ${JSON.stringify(error)}`);
464e41f4b71Sopenharmony_ci              })
465e41f4b71Sopenharmony_ci        })
466e41f4b71Sopenharmony_ci      }.width('100%').height('100%')
467e41f4b71Sopenharmony_ci    }
468e41f4b71Sopenharmony_ci  }
469e41f4b71Sopenharmony_ci  ```
470e41f4b71Sopenharmony_ci
471e41f4b71Sopenharmony_ci- 最后,示例应用的`module.json5`中的"extensionAbilities"中需要增加一项,具体内容如下:
472e41f4b71Sopenharmony_ci  ```json
473e41f4b71Sopenharmony_ci  {
474e41f4b71Sopenharmony_ci    "name": "ExampleEmbeddedAbility",
475e41f4b71Sopenharmony_ci    "srcEntry": "./ets/extensionAbility/ExampleEmbeddedAbility.ets",
476e41f4b71Sopenharmony_ci    "type": "embeddedUI"
477e41f4b71Sopenharmony_ci  }
478e41f4b71Sopenharmony_ci  ```
479