1e41f4b71Sopenharmony_ci# @ohos.app.ability.EmbeddedUIExtensionAbility (跨进程界面嵌入扩展能力) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciEmbeddedUIExtensionAbility为开发者提供了跨进程界面嵌入的能力,继承自[UIExtensionAbility](js-apis-app-ability-uiExtensionAbility.md)。目前EmbeddedUIExtensionAbility只能被同应用的UIAbility拉起,并且仅允许在拥有多进程权限的场景下使用。各类Ability的继承关系详见[继承关系说明](./js-apis-app-ability-ability.md#ability的继承关系说明)。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 12 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> 本模块接口仅可在Stage模型下使用。 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## 导入模块 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci```ts 14e41f4b71Sopenharmony_ciimport { EmbeddedUIExtensionAbility } from '@kit.AbilityKit'; 15e41f4b71Sopenharmony_ci``` 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## 属性 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 必填 | 说明 | 22e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 23e41f4b71Sopenharmony_ci| context | [UIExtensionContext](js-apis-inner-application-uiExtensionContext.md) | 否 | 否 | 上下文。 | 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci## EmbeddedUIExtensionAbility.onCreate 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_cionCreate(): void 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ciEmbeddedUIExtensionAbility创建时回调,执行初始化业务逻辑操作。 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci**示例:** 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci ```ts 36e41f4b71Sopenharmony_ci import { EmbeddedUIExtensionAbility } from '@kit.AbilityKit'; 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci const TAG: string = '[testTag] EmbeddedUIExt'; 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci export default class EmbeddedUIExt extends EmbeddedUIExtensionAbility { 41e41f4b71Sopenharmony_ci onCreate() { 42e41f4b71Sopenharmony_ci console.info(TAG, `onCreate`); 43e41f4b71Sopenharmony_ci } 44e41f4b71Sopenharmony_ci } 45e41f4b71Sopenharmony_ci ``` 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci## EmbeddedUIExtensionAbility.onSessionCreate 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_cionSessionCreate(want: Want, session: UIExtensionContentSession): void 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci当EmbeddedUIExtensionAbility界面内容对象创建后调用。 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci**参数:** 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 58e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 59e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 当前EmbeddedUIExtensionAbility的Want类型信息,包括Ability名称、Bundle名称等。 | 60e41f4b71Sopenharmony_ci| session | [UIExtensionContentSession](js-apis-app-ability-uiExtensionContentSession.md) | 是 | EmbeddedUIExtensionAbility界面内容相关信息。 | 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci**示例:** 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci ```ts 65e41f4b71Sopenharmony_ci import { EmbeddedUIExtensionAbility, Want, UIExtensionContentSession } from '@kit.AbilityKit'; 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci const TAG: string = '[testTag] EmbeddedUIExt'; 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci export default class EmbeddedUIExt extends EmbeddedUIExtensionAbility { 70e41f4b71Sopenharmony_ci onSessionCreate(want: Want, session: UIExtensionContentSession) { 71e41f4b71Sopenharmony_ci console.info(TAG, `onSessionCreate, want: ${JSON.stringify(want)}`); 72e41f4b71Sopenharmony_ci } 73e41f4b71Sopenharmony_ci } 74e41f4b71Sopenharmony_ci ``` 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci## EmbeddedUIExtensionAbility.onSessionDestroy 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_cionSessionDestroy(session: UIExtensionContentSession): void 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci当EmbeddedUIExtensionAbility界面内容对象销毁后调用。 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci**参数:** 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 87e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 88e41f4b71Sopenharmony_ci| session | [UIExtensionContentSession](js-apis-app-ability-uiExtensionContentSession.md) | 是 | EmbeddedUIExtensionAbility界面内容相关信息。 | 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci**示例:** 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci ```ts 93e41f4b71Sopenharmony_ci import { EmbeddedUIExtensionAbility, UIExtensionContentSession } from '@kit.AbilityKit'; 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci const TAG: string = '[testTag] EmbeddedUIExt'; 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci export default class EmbeddedUIExt extends EmbeddedUIExtensionAbility { 98e41f4b71Sopenharmony_ci onSessionDestroy(session: UIExtensionContentSession) { 99e41f4b71Sopenharmony_ci console.info(TAG, `onSessionDestroy`); 100e41f4b71Sopenharmony_ci } 101e41f4b71Sopenharmony_ci } 102e41f4b71Sopenharmony_ci ``` 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci## EmbeddedUIExtensionAbility.onForeground 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_cionForeground(): void 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ciEmbeddedUIExtensionAbility生命周期回调,当EmbeddedUIExtensionAbility从后台转到前台时触发。 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci**示例:** 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci ```ts 115e41f4b71Sopenharmony_ci import { EmbeddedUIExtensionAbility } from '@kit.AbilityKit'; 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci const TAG: string = '[testTag] EmbeddedUIExt'; 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci export default class EmbeddedUIExt extends EmbeddedUIExtensionAbility { 120e41f4b71Sopenharmony_ci onForeground() { 121e41f4b71Sopenharmony_ci console.info(TAG, `onForeground`); 122e41f4b71Sopenharmony_ci } 123e41f4b71Sopenharmony_ci } 124e41f4b71Sopenharmony_ci ``` 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci## EmbeddedUIExtensionAbility.onBackground 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_cionBackground(): void 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ciEmbeddedUIExtensionAbility生命周期回调,当EmbeddedUIExtensionAbility从前台转到后台时触发。 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci**示例:** 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci ```ts 137e41f4b71Sopenharmony_ci import { EmbeddedUIExtensionAbility } from '@kit.AbilityKit'; 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci const TAG: string = '[testTag] EmbeddedUIExt'; 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci export default class EmbeddedUIExt extends EmbeddedUIExtensionAbility { 142e41f4b71Sopenharmony_ci onBackground() { 143e41f4b71Sopenharmony_ci console.info(TAG, `onBackground`); 144e41f4b71Sopenharmony_ci } 145e41f4b71Sopenharmony_ci } 146e41f4b71Sopenharmony_ci ``` 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci## EmbeddedUIExtensionAbility.onDestroy 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_cionDestroy(): void | Promise<void> 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ciEmbeddedUIExtensionAbility生命周期回调,在销毁时回调,执行资源清理等操作。 153e41f4b71Sopenharmony_ci在执行完onDestroy生命周期回调后,应用可能会退出,从而可能导致onDestroy中的异步函数未能正确执行,比如异步写入数据库。可以使用异步生命周期,以确保异步onDestroy完成后再继续后续的生命周期。 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci**返回值:** 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci| 类型 | 说明 | 160e41f4b71Sopenharmony_ci| ----------------- | ------------------------------------------------------------ | 161e41f4b71Sopenharmony_ci| void \| Promise\<void> | 无返回结果或无返回结果的Promise对象。 | 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci**示例:** 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci ```ts 166e41f4b71Sopenharmony_ci import { EmbeddedUIExtensionAbility } from '@kit.AbilityKit'; 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci const TAG: string = '[testTag] EmbeddedUIExt'; 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci export default class EmbeddedUIExt extends EmbeddedUIExtensionAbility { 171e41f4b71Sopenharmony_ci onDestroy() { 172e41f4b71Sopenharmony_ci console.info(TAG, `onDestroy`); 173e41f4b71Sopenharmony_ci } 174e41f4b71Sopenharmony_ci } 175e41f4b71Sopenharmony_ci ``` 176