1e41f4b71Sopenharmony_ci# @ohos.app.ability.UIExtensionContentSession (带界面扩展能力界面操作类) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciUIExtensionContentSession是[UIExtensionAbility](js-apis-app-ability-uiExtensionAbility.md)加载界面内容时创建的实例对象,当UIExtensionComponent控件拉起指定的UIExtensionAbility时,UIExtensionAbility会创建UIExtensionContentSession对象,并通过[onSessionCreate](js-apis-app-ability-uiExtensionAbility.md#uiextensionabilityonsessioncreate)回调传递给开发者。一个UIExtensionComponent控件对应一个UIExtensionContentSession对象,提供界面加载,结果通知等方法。每个UIExtensionAbility的UIExtensionContentSession之间互不影响,可以各自进行操作。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 10 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> 本模块接口仅可在Stage模型下使用。 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## 导入模块 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci```ts 14e41f4b71Sopenharmony_ciimport { UIExtensionContentSession } from '@kit.AbilityKit'; 15e41f4b71Sopenharmony_ci``` 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## UIExtensionContentSession.loadContent 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciloadContent(path: string, storage?: LocalStorage): void 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci为当前UIExtensionComponent控件对应的窗口加载与LocalStorage相关联的具体页面内容。 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Ability.AbilityRuntime.Core 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**参数:** 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 28e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ | 29e41f4b71Sopenharmony_ci| path | string | 是 | 设置加载页面的路径。 | 30e41f4b71Sopenharmony_ci| storage | [LocalStorage](../../quick-start/arkts-localstorage.md) | 否 | 存储单元,为应用程序范围内的可变状态属性和非可变状态属性提供存储。默认为空。 | 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci**错误码:** 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 37e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 38e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 39e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci**示例:** 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci```ts 44e41f4b71Sopenharmony_ciimport { UIExtensionContentSession, UIExtensionAbility, Want } from '@kit.AbilityKit'; 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ciexport default class UIExtAbility extends UIExtensionAbility { 47e41f4b71Sopenharmony_ci // ... 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci onSessionCreate(want: Want, session: UIExtensionContentSession): void { 50e41f4b71Sopenharmony_ci let storage: LocalStorage = new LocalStorage(); 51e41f4b71Sopenharmony_ci storage.setOrCreate('session', session); 52e41f4b71Sopenharmony_ci session.loadContent('pages/Extension', storage); 53e41f4b71Sopenharmony_ci } 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci // ... 56e41f4b71Sopenharmony_ci} 57e41f4b71Sopenharmony_ci``` 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci## UIExtensionContentSession.terminateSelf 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_citerminateSelf(callback: AsyncCallback<void>): void 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci停止UIExtensionContentSession对应的窗口界面对象。使用callback异步回调。 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci**参数:** 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 70e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 71e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当停止UIExtensionContentSession对应的窗口界面对象成功,err为undefined,否则为错误对象。 | 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci**错误码:** 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 78e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 79e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci**示例:** 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci```ts 84e41f4b71Sopenharmony_ciimport { UIExtensionContentSession } from '@kit.AbilityKit'; 85e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_cilet storage = LocalStorage.getShared(); 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci@Entry(storage) 90e41f4b71Sopenharmony_ci@Component 91e41f4b71Sopenharmony_cistruct Index { 92e41f4b71Sopenharmony_ci private session: UIExtensionContentSession | undefined = 93e41f4b71Sopenharmony_ci storage.get<UIExtensionContentSession>('session'); 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci build() { 96e41f4b71Sopenharmony_ci RelativeContainer() { 97e41f4b71Sopenharmony_ci Button('TerminateSelf') 98e41f4b71Sopenharmony_ci .onClick(() => { 99e41f4b71Sopenharmony_ci this.session?.terminateSelf((err: BusinessError) => { 100e41f4b71Sopenharmony_ci if (err) { 101e41f4b71Sopenharmony_ci console.error(`Failed to terminate self, code: ${err.code}, msg: ${err.message}`); 102e41f4b71Sopenharmony_ci return; 103e41f4b71Sopenharmony_ci } 104e41f4b71Sopenharmony_ci console.info(`Successed in terminating self.`); 105e41f4b71Sopenharmony_ci }); 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci storage.clear(); 108e41f4b71Sopenharmony_ci }) 109e41f4b71Sopenharmony_ci } 110e41f4b71Sopenharmony_ci .height('100%') 111e41f4b71Sopenharmony_ci .width('100%') 112e41f4b71Sopenharmony_ci } 113e41f4b71Sopenharmony_ci} 114e41f4b71Sopenharmony_ci``` 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci## UIExtensionContentSession.terminateSelf 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_citerminateSelf(): Promise<void> 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci停止UIExtensionContentSession对应的窗口界面对象。使用Promise异步回调。 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci**返回值:** 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci| 类型 | 说明 | 127e41f4b71Sopenharmony_ci| -------- | -------- | 128e41f4b71Sopenharmony_ci| Promise<void> | Promise对象。无返回结果的Promise对象。 | 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci**示例:** 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci```ts 133e41f4b71Sopenharmony_ciimport { UIExtensionContentSession } from '@kit.AbilityKit'; 134e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_cilet storage = LocalStorage.getShared(); 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci@Entry(storage) 139e41f4b71Sopenharmony_ci@Component 140e41f4b71Sopenharmony_cistruct Index { 141e41f4b71Sopenharmony_ci private session: UIExtensionContentSession | undefined = 142e41f4b71Sopenharmony_ci storage.get<UIExtensionContentSession>('session'); 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci build() { 145e41f4b71Sopenharmony_ci RelativeContainer() { 146e41f4b71Sopenharmony_ci Button('TerminateSelf') 147e41f4b71Sopenharmony_ci .onClick(() => { 148e41f4b71Sopenharmony_ci this.session?.terminateSelf() 149e41f4b71Sopenharmony_ci .then(() => { 150e41f4b71Sopenharmony_ci console.info(`Successed in terminating self.`); 151e41f4b71Sopenharmony_ci }) 152e41f4b71Sopenharmony_ci .catch((err: BusinessError) => { 153e41f4b71Sopenharmony_ci console.error(`Failed to terminate self, code: ${err.code}, msg: ${err.message}`); 154e41f4b71Sopenharmony_ci }); 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci storage.clear(); 157e41f4b71Sopenharmony_ci }) 158e41f4b71Sopenharmony_ci } 159e41f4b71Sopenharmony_ci .height('100%') 160e41f4b71Sopenharmony_ci .width('100%') 161e41f4b71Sopenharmony_ci } 162e41f4b71Sopenharmony_ci} 163e41f4b71Sopenharmony_ci``` 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci## UIExtensionContentSession.terminateSelfWithResult 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_citerminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback<void>): void 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci停止UIExtensionContentSession对应的窗口界面对象,并将结果返回给UIExtensionComponent控件。使用callback异步回调。 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci**参数:** 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 176e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 177e41f4b71Sopenharmony_ci| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | 是 | 返回给UIExtensionComponent控件的信息。 | 178e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当停止成功,err为undefined,否则为错误对象。 | 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci**错误码:** 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 185e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 186e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci**示例:** 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci```ts 191e41f4b71Sopenharmony_ciimport { UIExtensionContentSession, common } from '@kit.AbilityKit'; 192e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_cilet storage = LocalStorage.getShared(); 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci@Entry(storage) 197e41f4b71Sopenharmony_ci@Component 198e41f4b71Sopenharmony_cistruct Index { 199e41f4b71Sopenharmony_ci private session: UIExtensionContentSession | undefined = 200e41f4b71Sopenharmony_ci storage.get<UIExtensionContentSession>('session'); 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci build() { 203e41f4b71Sopenharmony_ci RelativeContainer() { 204e41f4b71Sopenharmony_ci Button('TerminateSelfWithResult') 205e41f4b71Sopenharmony_ci .onClick(() => { 206e41f4b71Sopenharmony_ci let abilityResult: common.AbilityResult = { 207e41f4b71Sopenharmony_ci resultCode: 0, 208e41f4b71Sopenharmony_ci want: { 209e41f4b71Sopenharmony_ci bundleName: 'com.ohos.uiextensioncontentsession', 210e41f4b71Sopenharmony_ci parameters: { 211e41f4b71Sopenharmony_ci 'result': 123456 212e41f4b71Sopenharmony_ci } 213e41f4b71Sopenharmony_ci } 214e41f4b71Sopenharmony_ci }; 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci this.session?.terminateSelfWithResult(abilityResult, (err: BusinessError) => { 217e41f4b71Sopenharmony_ci if (err) { 218e41f4b71Sopenharmony_ci console.error(`Failed to terminate self with result, code: ${err.code}, msg: ${err.message}`); 219e41f4b71Sopenharmony_ci return; 220e41f4b71Sopenharmony_ci } 221e41f4b71Sopenharmony_ci console.info(`Successed in terminating self with result.`); 222e41f4b71Sopenharmony_ci }); 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci storage.clear(); 225e41f4b71Sopenharmony_ci }) 226e41f4b71Sopenharmony_ci } 227e41f4b71Sopenharmony_ci .height('100%') 228e41f4b71Sopenharmony_ci .width('100%') 229e41f4b71Sopenharmony_ci } 230e41f4b71Sopenharmony_ci} 231e41f4b71Sopenharmony_ci``` 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci## UIExtensionContentSession.terminateSelfWithResult 234e41f4b71Sopenharmony_ci 235e41f4b71Sopenharmony_citerminateSelfWithResult(parameter: AbilityResult): Promise<void> 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci停止UIExtensionContentSession对应的窗口界面对象,并将结果返回给UIExtensionComponent控件。使用Promise异步回调。 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci**参数:** 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 244e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 245e41f4b71Sopenharmony_ci| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | 是 | 返回给UIExtensionComponent控件的信息。 | 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci**返回值:** 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ci| 类型 | 说明 | 250e41f4b71Sopenharmony_ci| -------- | -------- | 251e41f4b71Sopenharmony_ci| Promise<void> | Promise对象。无返回结果的Promise对象。 | 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci**错误码:** 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 258e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 259e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ci**示例:** 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ci```ts 264e41f4b71Sopenharmony_ciimport { UIExtensionContentSession, common } from '@kit.AbilityKit'; 265e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_cilet storage = LocalStorage.getShared(); 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci@Entry(storage) 270e41f4b71Sopenharmony_ci@Component 271e41f4b71Sopenharmony_cistruct Index { 272e41f4b71Sopenharmony_ci private session: UIExtensionContentSession | undefined = 273e41f4b71Sopenharmony_ci storage.get<UIExtensionContentSession>('session'); 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ci build() { 276e41f4b71Sopenharmony_ci RelativeContainer() { 277e41f4b71Sopenharmony_ci Button('TerminateSelfWithResult') 278e41f4b71Sopenharmony_ci .onClick(() => { 279e41f4b71Sopenharmony_ci let abilityResult: common.AbilityResult = { 280e41f4b71Sopenharmony_ci resultCode: 0, 281e41f4b71Sopenharmony_ci want: { 282e41f4b71Sopenharmony_ci bundleName: 'com.ohos.uiextensioncontentsession', 283e41f4b71Sopenharmony_ci parameters: { 284e41f4b71Sopenharmony_ci 'result': 123456 285e41f4b71Sopenharmony_ci } 286e41f4b71Sopenharmony_ci } 287e41f4b71Sopenharmony_ci }; 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci this.session?.terminateSelfWithResult(abilityResult) 290e41f4b71Sopenharmony_ci .then(() => { 291e41f4b71Sopenharmony_ci console.info(`Successed in terminating self with result.`); 292e41f4b71Sopenharmony_ci }) 293e41f4b71Sopenharmony_ci .catch((err: BusinessError) => { 294e41f4b71Sopenharmony_ci console.error(`Failed to terminate self with result, code: ${err.code}, msg: ${err.message}`); 295e41f4b71Sopenharmony_ci }); 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ci storage.clear(); 298e41f4b71Sopenharmony_ci }) 299e41f4b71Sopenharmony_ci } 300e41f4b71Sopenharmony_ci .height('100%') 301e41f4b71Sopenharmony_ci .width('100%') 302e41f4b71Sopenharmony_ci } 303e41f4b71Sopenharmony_ci} 304e41f4b71Sopenharmony_ci``` 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ci## UIExtensionContentSession.setWindowPrivacyMode 307e41f4b71Sopenharmony_ci 308e41f4b71Sopenharmony_cisetWindowPrivacyMode(isPrivacyMode: boolean): Promise<void> 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_ci设置窗口是否为隐私模式。设置为隐私模式的窗口,窗口内容将无法被截屏或录屏。使用Promise异步回调。 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Ability.AbilityRuntime.Core 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.PRIVACY_WINDOW 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ci**参数:** 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 319e41f4b71Sopenharmony_ci| ------------- | ------- | -- | ----------------------------------------------------- | 320e41f4b71Sopenharmony_ci| isPrivacyMode | boolean | 是 | 窗口是否为隐私模式。true表示模式开启;false表示模式关闭。 | 321e41f4b71Sopenharmony_ci 322e41f4b71Sopenharmony_ci**返回值:** 323e41f4b71Sopenharmony_ci 324e41f4b71Sopenharmony_ci| 类型 | 说明 | 325e41f4b71Sopenharmony_ci| ------------------- | ------------------------ | 326e41f4b71Sopenharmony_ci| Promise<void> | Promise对象。无返回结果的Promise对象。 | 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci**错误码:** 329e41f4b71Sopenharmony_ci 330e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 331e41f4b71Sopenharmony_ci 332e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 333e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 334e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 335e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 336e41f4b71Sopenharmony_ci 337e41f4b71Sopenharmony_ci**示例:** 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_ci```ts 340e41f4b71Sopenharmony_ciimport { UIExtensionContentSession, UIExtensionAbility, Want } from '@kit.AbilityKit'; 341e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 342e41f4b71Sopenharmony_ci 343e41f4b71Sopenharmony_ciexport default class UIExtAbility extends UIExtensionAbility { 344e41f4b71Sopenharmony_ci // ... 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci onSessionCreate(want: Want, session: UIExtensionContentSession): void { 347e41f4b71Sopenharmony_ci let isPrivacyMode: boolean = true; 348e41f4b71Sopenharmony_ci try { 349e41f4b71Sopenharmony_ci session.setWindowPrivacyMode(isPrivacyMode) 350e41f4b71Sopenharmony_ci .then(() => { 351e41f4b71Sopenharmony_ci console.info(`Successed in setting window to privacy mode.`); 352e41f4b71Sopenharmony_ci }) 353e41f4b71Sopenharmony_ci .catch((err: BusinessError) => { 354e41f4b71Sopenharmony_ci console.error(`Failed to set window to privacy mode, code: ${err.code}, msg: ${err.message}`); 355e41f4b71Sopenharmony_ci }); 356e41f4b71Sopenharmony_ci } catch (e) { 357e41f4b71Sopenharmony_ci let code = (e as BusinessError).code; 358e41f4b71Sopenharmony_ci let msg = (e as BusinessError).message; 359e41f4b71Sopenharmony_ci console.error(`Failed to set window to privacy mode, code: ${code}, msg: ${msg}`); 360e41f4b71Sopenharmony_ci } 361e41f4b71Sopenharmony_ci } 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ci // ... 364e41f4b71Sopenharmony_ci} 365e41f4b71Sopenharmony_ci``` 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ci## UIExtensionContentSession.setWindowPrivacyMode 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_cisetWindowPrivacyMode(isPrivacyMode: boolean, callback: AsyncCallback<void>): void 370e41f4b71Sopenharmony_ci 371e41f4b71Sopenharmony_ci设置窗口是否为隐私模式。设置为隐私模式的窗口,窗口内容将无法被截屏或录屏。使用callback异步回调。 372e41f4b71Sopenharmony_ci 373e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Ability.AbilityRuntime.Core 374e41f4b71Sopenharmony_ci 375e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.PRIVACY_WINDOW 376e41f4b71Sopenharmony_ci 377e41f4b71Sopenharmony_ci**参数:** 378e41f4b71Sopenharmony_ci 379e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 380e41f4b71Sopenharmony_ci| ------------- | ------------------------- | -- | ------------------------------------------------------ | 381e41f4b71Sopenharmony_ci| isPrivacyMode | boolean | 是 | 窗口是否为隐私模式。true表示模式开启;false表示模式关闭。 | 382e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当设置成功,err为undefined,否则为错误对象。 | 383e41f4b71Sopenharmony_ci 384e41f4b71Sopenharmony_ci**错误码:** 385e41f4b71Sopenharmony_ci 386e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 387e41f4b71Sopenharmony_ci 388e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 389e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 390e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 391e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci**示例:** 394e41f4b71Sopenharmony_ci 395e41f4b71Sopenharmony_ci```ts 396e41f4b71Sopenharmony_ciimport { UIExtensionContentSession, UIExtensionAbility, Want } from '@kit.AbilityKit'; 397e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ciexport default class UIExtAbility extends UIExtensionAbility { 400e41f4b71Sopenharmony_ci // ... 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ci onSessionCreate(want: Want, session: UIExtensionContentSession): void { 403e41f4b71Sopenharmony_ci let isPrivacyMode: boolean = true; 404e41f4b71Sopenharmony_ci try { 405e41f4b71Sopenharmony_ci session.setWindowPrivacyMode(isPrivacyMode, (err: BusinessError) => { 406e41f4b71Sopenharmony_ci if (err) { 407e41f4b71Sopenharmony_ci console.error(`Failed to set window to privacy mode, code: ${err.code}, msg: ${err.message}`); 408e41f4b71Sopenharmony_ci return; 409e41f4b71Sopenharmony_ci } 410e41f4b71Sopenharmony_ci console.info(`Successed in setting window to privacy mode.`); 411e41f4b71Sopenharmony_ci }); 412e41f4b71Sopenharmony_ci } catch (e) { 413e41f4b71Sopenharmony_ci let code = (e as BusinessError).code; 414e41f4b71Sopenharmony_ci let msg = (e as BusinessError).message; 415e41f4b71Sopenharmony_ci console.error(`Failed to set window to privacy mode, code: ${code}, msg: ${msg}`); 416e41f4b71Sopenharmony_ci } 417e41f4b71Sopenharmony_ci } 418e41f4b71Sopenharmony_ci 419e41f4b71Sopenharmony_ci // ... 420e41f4b71Sopenharmony_ci} 421e41f4b71Sopenharmony_ci``` 422e41f4b71Sopenharmony_ci 423e41f4b71Sopenharmony_ci## UIExtensionContentSession.startAbilityByType<sup>11+</sup> 424e41f4b71Sopenharmony_ci 425e41f4b71Sopenharmony_cistartAbilityByType(type: string, wantParam: Record<string, Object>, 426e41f4b71Sopenharmony_ci abilityStartCallback: AbilityStartCallback, callback: AsyncCallback\<void>): void 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ci通过type隐式启动UIExtensionAbility。使用callback异步回调。仅支持处于前台的应用调用。 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_ci**参数:** 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 435e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 436e41f4b71Sopenharmony_ci| type | string | 是 | 显示拉起的UIExtensionAbility类型<!--Del-->,取值详见[通过startAbilityByType接口拉起垂类面板](../../application-models/start-intent-panel.md#匹配规则)<!--DelEnd-->。 | 437e41f4b71Sopenharmony_ci| wantParam | Record<string, Object> | 是 | 表示扩展参数。 | 438e41f4b71Sopenharmony_ci| abilityStartCallback | [AbilityStartCallback](js-apis-inner-application-abilityStartCallback.md) | 是 | 回调函数,返回启动失败后的详细错误信息。 | 439e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是 |回调函数。当启动Ability成功,err为undefined,否则为错误对象。 | 440e41f4b71Sopenharmony_ci 441e41f4b71Sopenharmony_ci**错误码:** 442e41f4b71Sopenharmony_ci 443e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 444e41f4b71Sopenharmony_ci 445e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 446e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 447e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 448e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 449e41f4b71Sopenharmony_ci 450e41f4b71Sopenharmony_ci**示例:** 451e41f4b71Sopenharmony_ci 452e41f4b71Sopenharmony_ci```ts 453e41f4b71Sopenharmony_ciimport { UIExtensionContentSession, UIExtensionAbility, Want, common } from '@kit.AbilityKit'; 454e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 455e41f4b71Sopenharmony_ci 456e41f4b71Sopenharmony_ciexport default class UIExtAbility extends UIExtensionAbility { 457e41f4b71Sopenharmony_ci // ... 458e41f4b71Sopenharmony_ci 459e41f4b71Sopenharmony_ci onSessionCreate(want: Want, session: UIExtensionContentSession): void { 460e41f4b71Sopenharmony_ci let wantParams: Record<string, Object> = { 461e41f4b71Sopenharmony_ci 'sceneType': 1 462e41f4b71Sopenharmony_ci }; 463e41f4b71Sopenharmony_ci let abilityStartCallback: common.AbilityStartCallback = { 464e41f4b71Sopenharmony_ci onError: (code: number, name: string, message: string) => { 465e41f4b71Sopenharmony_ci console.error(`onError, code: ${code}, name: ${name}, msg: ${message}`); 466e41f4b71Sopenharmony_ci }, 467e41f4b71Sopenharmony_ci onResult: (result: common.AbilityResult) => { 468e41f4b71Sopenharmony_ci console.info(`onResult, result: ${JSON.stringify(result)}`); 469e41f4b71Sopenharmony_ci } 470e41f4b71Sopenharmony_ci }; 471e41f4b71Sopenharmony_ci 472e41f4b71Sopenharmony_ci session.startAbilityByType('test', wantParams, abilityStartCallback, (err: BusinessError) => { 473e41f4b71Sopenharmony_ci if (err) { 474e41f4b71Sopenharmony_ci console.error(`Failed to startAbilityByType, code: ${err.code}, msg: ${err.message}`); 475e41f4b71Sopenharmony_ci return; 476e41f4b71Sopenharmony_ci } 477e41f4b71Sopenharmony_ci console.info(`Successed in startAbilityByType`); 478e41f4b71Sopenharmony_ci }); 479e41f4b71Sopenharmony_ci } 480e41f4b71Sopenharmony_ci 481e41f4b71Sopenharmony_ci // ... 482e41f4b71Sopenharmony_ci} 483e41f4b71Sopenharmony_ci``` 484e41f4b71Sopenharmony_ci 485e41f4b71Sopenharmony_ci## UIExtensionContentSession.startAbilityByType<sup>11+</sup> 486e41f4b71Sopenharmony_ci 487e41f4b71Sopenharmony_cistartAbilityByType(type: string, wantParam: Record<string, Object>, 488e41f4b71Sopenharmony_ci abilityStartCallback: AbilityStartCallback): Promise\<void> 489e41f4b71Sopenharmony_ci 490e41f4b71Sopenharmony_ci通过type隐式启动UIExtensionAbility。使用Promise异步回调。仅支持处于前台的应用调用。 491e41f4b71Sopenharmony_ci 492e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 493e41f4b71Sopenharmony_ci 494e41f4b71Sopenharmony_ci**参数:** 495e41f4b71Sopenharmony_ci 496e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 497e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 498e41f4b71Sopenharmony_ci| type | string | 是 | 显示拉起的UIExtensionAbility类型<!--Del-->,取值详见[通过startAbilityByType接口拉起垂类面板](../../application-models/start-intent-panel.md#匹配规则)<!--DelEnd-->。 | 499e41f4b71Sopenharmony_ci| wantParam | Record<string, Object> | 是 | 表示扩展参数。 | 500e41f4b71Sopenharmony_ci| abilityStartCallback | [AbilityStartCallback](js-apis-inner-application-abilityStartCallback.md) | 是 | 回调函数,返回启动失败后的详细错误信息。 | 501e41f4b71Sopenharmony_ci 502e41f4b71Sopenharmony_ci**返回值:** 503e41f4b71Sopenharmony_ci 504e41f4b71Sopenharmony_ci| 类型 | 说明 | 505e41f4b71Sopenharmony_ci| -------- | -------- | 506e41f4b71Sopenharmony_ci| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 507e41f4b71Sopenharmony_ci 508e41f4b71Sopenharmony_ci**错误码:** 509e41f4b71Sopenharmony_ci 510e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 511e41f4b71Sopenharmony_ci 512e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 513e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 514e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 515e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 516e41f4b71Sopenharmony_ci 517e41f4b71Sopenharmony_ci**示例:** 518e41f4b71Sopenharmony_ci 519e41f4b71Sopenharmony_ci```ts 520e41f4b71Sopenharmony_ciimport { UIExtensionContentSession, UIExtensionAbility, Want, common } from '@kit.AbilityKit'; 521e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 522e41f4b71Sopenharmony_ci 523e41f4b71Sopenharmony_ciexport default class UIExtAbility extends UIExtensionAbility { 524e41f4b71Sopenharmony_ci // ... 525e41f4b71Sopenharmony_ci 526e41f4b71Sopenharmony_ci onSessionCreate(want: Want, session: UIExtensionContentSession): void { 527e41f4b71Sopenharmony_ci let wantParams: Record<string, Object> = { 528e41f4b71Sopenharmony_ci 'sceneType': 1 529e41f4b71Sopenharmony_ci }; 530e41f4b71Sopenharmony_ci let abilityStartCallback: common.AbilityStartCallback = { 531e41f4b71Sopenharmony_ci onError: (code: number, name: string, message: string) => { 532e41f4b71Sopenharmony_ci console.error(`onError, code: ${code}, name: ${name}, msg: ${message}`); 533e41f4b71Sopenharmony_ci }, 534e41f4b71Sopenharmony_ci onResult: (result: common.AbilityResult) => { 535e41f4b71Sopenharmony_ci console.info(`onResult, result: ${JSON.stringify(result)}`); 536e41f4b71Sopenharmony_ci } 537e41f4b71Sopenharmony_ci }; 538e41f4b71Sopenharmony_ci 539e41f4b71Sopenharmony_ci session.startAbilityByType('test', wantParams, abilityStartCallback) 540e41f4b71Sopenharmony_ci .then(() => { 541e41f4b71Sopenharmony_ci console.info(`Successed in startAbilityByType`); 542e41f4b71Sopenharmony_ci }) 543e41f4b71Sopenharmony_ci .catch((err: BusinessError) => { 544e41f4b71Sopenharmony_ci console.error(`Failed to startAbilityByType, code: ${err.code}, msg: ${err.message}`); 545e41f4b71Sopenharmony_ci }); 546e41f4b71Sopenharmony_ci } 547e41f4b71Sopenharmony_ci 548e41f4b71Sopenharmony_ci // ... 549e41f4b71Sopenharmony_ci} 550e41f4b71Sopenharmony_ci``` 551e41f4b71Sopenharmony_ci 552e41f4b71Sopenharmony_ci## UIExtensionContentSession.getUIExtensionWindowProxy<sup>12+</sup> 553e41f4b71Sopenharmony_ci 554e41f4b71Sopenharmony_cigetUIExtensionWindowProxy(): uiExtension.WindowProxy 555e41f4b71Sopenharmony_ci 556e41f4b71Sopenharmony_ci获取UIExtension窗口代理。 557e41f4b71Sopenharmony_ci 558e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 559e41f4b71Sopenharmony_ci 560e41f4b71Sopenharmony_ci**返回值:** 561e41f4b71Sopenharmony_ci 562e41f4b71Sopenharmony_ci| 类型 | 说明 | 563e41f4b71Sopenharmony_ci| -------- | -------- | 564e41f4b71Sopenharmony_ci| uiExtension.WindowProxy | 返回UIExtension的窗口代理。 | 565e41f4b71Sopenharmony_ci 566e41f4b71Sopenharmony_ci**错误码:** 567e41f4b71Sopenharmony_ci 568e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 569e41f4b71Sopenharmony_ci 570e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 571e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 572e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 573e41f4b71Sopenharmony_ci 574e41f4b71Sopenharmony_ci**示例:** 575e41f4b71Sopenharmony_ci 576e41f4b71Sopenharmony_ci```ts 577e41f4b71Sopenharmony_ci// Index.ets 578e41f4b71Sopenharmony_ciimport { UIExtensionContentSession } from '@kit.AbilityKit'; 579e41f4b71Sopenharmony_ciimport uiExtension from '@ohos.arkui.uiExtension'; 580e41f4b71Sopenharmony_ci 581e41f4b71Sopenharmony_cilet storage = LocalStorage.getShared(); 582e41f4b71Sopenharmony_ci 583e41f4b71Sopenharmony_ci@Entry(storage) 584e41f4b71Sopenharmony_ci@Component 585e41f4b71Sopenharmony_cistruct Extension { 586e41f4b71Sopenharmony_ci @State message: string = 'EmbeddedUIExtensionAbility Index'; 587e41f4b71Sopenharmony_ci private session: UIExtensionContentSession | undefined = storage.get<UIExtensionContentSession>('session'); 588e41f4b71Sopenharmony_ci private extensionWindow: uiExtension.WindowProxy | undefined = this.session?.getUIExtensionWindowProxy(); 589e41f4b71Sopenharmony_ci 590e41f4b71Sopenharmony_ci aboutToAppear(): void { 591e41f4b71Sopenharmony_ci this.extensionWindow?.on('windowSizeChange', (size) => { 592e41f4b71Sopenharmony_ci console.info(`size = ${JSON.stringify(size)}`); 593e41f4b71Sopenharmony_ci }); 594e41f4b71Sopenharmony_ci this.extensionWindow?.on('avoidAreaChange', (info) => { 595e41f4b71Sopenharmony_ci console.info(`type = ${JSON.stringify(info.type)}, area = ${JSON.stringify(info.area)}`); 596e41f4b71Sopenharmony_ci }); 597e41f4b71Sopenharmony_ci } 598e41f4b71Sopenharmony_ci 599e41f4b71Sopenharmony_ci aboutToDisappear(): void { 600e41f4b71Sopenharmony_ci this.extensionWindow?.off('windowSizeChange'); 601e41f4b71Sopenharmony_ci this.extensionWindow?.off('avoidAreaChange'); 602e41f4b71Sopenharmony_ci } 603e41f4b71Sopenharmony_ci 604e41f4b71Sopenharmony_ci build() { 605e41f4b71Sopenharmony_ci Column() { 606e41f4b71Sopenharmony_ci Text(this.message) 607e41f4b71Sopenharmony_ci .fontSize(20) 608e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 609e41f4b71Sopenharmony_ci } 610e41f4b71Sopenharmony_ci .width('100%') 611e41f4b71Sopenharmony_ci } 612e41f4b71Sopenharmony_ci} 613e41f4b71Sopenharmony_ci```