1# UIExtensionContext (System API) 2 3<!--Kit: Ability Kit--> 4<!--Subsystem: Ability--> 5<!--Owner: @zhangyafei-echo--> 6<!--Designer: @li-weifeng2--> 7<!--Tester: @lixueqing513--> 8<!--Adviser: @huipeizi--> 9 10UIExtensionContext provides the context environment for [UIExtensionAbility](js-apis-app-ability-uiExtensionAbility.md). It inherits from [ExtensionContext](js-apis-inner-application-extensionContext.md) and provides UIExtensionAbility-related configuration and APIs for operating the UIExtensionAbility. For example, you can use the APIs to start a UIExtensionAbility. 11 12> **NOTE** 13> 14> - The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version. 15> - The APIs of this module can be used only in the stage model. 16> - The APIs provided by this module are system APIs. 17 18## Modules to Import 19 20```ts 21import { common } from '@kit.AbilityKit'; 22``` 23 24## UIExtensionContext 25 26### startAbilityForResultAsCaller 27 28startAbilityForResultAsCaller(want: Want, options?: StartOptions): Promise<AbilityResult> 29 30Starts an ability with the caller information specified. The caller information is carried in **want** and identified at the system service layer. The ability can obtain the caller information from the **want** parameter in the **onCreate** lifecycle callback. When this API is used to start an ability, the caller information carried in **want** is not overwritten by the current application information. The system service layer can obtain the initial caller information. This API uses a promise to return the result. 31 32 - Normally, you can call [terminateSelfWithResult](js-apis-inner-application-uiAbilityContext.md#terminateselfwithresult) to terminate the ability. The result is returned to the caller. 33 - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. 34 - If different applications call this API to start an ability that uses the singleton mode and then call [terminateSelfWithResult](js-apis-inner-application-uiAbilityContext.md#terminateselfwithresult) to terminate the ability, the normal result is returned to the last caller, and an exception message, in which **resultCode** is **-1**, is returned to others. 35 36> **NOTE** 37> 38> For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 39 40**Model restriction**: This API can be used only in the stage model. 41 42**System API**: This is a system API. 43 44**System capability**: SystemCapability.Ability.AbilityRuntime.Core 45 46**Parameters** 47 48| Name | Type | Mandatory| Description | 49| ------- | --------------------------------------------------- | ---- | ------------------------- | 50| want | [Want](js-apis-app-ability-want.md) | Yes | Want information about the target ability. | 51| options | [StartOptions](js-apis-app-ability-startOptions.md) | No | Parameters used for starting the ability.| 52 53**Return value** 54 55| Type | Description | 56| ------------------------------------------------------------ | ------------------------- | 57| Promise<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Promise used to return the result.| 58 59**Error codes** 60 61For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 62 63| ID| Error Message | 64| -------- | ------------------------------------------------------- | 65| 401| Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 66| 16000001 | The specified ability does not exist. | 67| 16000004 | Cannot start an invisible component. | 68| 16000050 | Internal error. | 69| 16000073 | The app clone index is invalid. | 70 71**Example** 72 73```ts 74import { UIExtensionAbility } from '@kit.AbilityKit'; 75import { BusinessError } from '@kit.BasicServicesKit'; 76 77export default class UIExtension extends UIExtensionAbility { 78 onForeground() { 79 this.context.startAbilityForResultAsCaller({ 80 bundleName: 'com.example.startabilityforresultascaller', 81 abilityName: 'EntryAbility', 82 moduleName: 'entry' 83 }).then((data) => { 84 console.log(`StartAbilityForResultAsCaller success, data: ${JSON.stringify(data)}.`); 85 }).catch((error: BusinessError) => { 86 console.error(`StartAbilityForResultAsCaller failed, err code: ${error.code}, err msg: ${error.message}.`); 87 }); 88 } 89} 90``` 91 92### startServiceExtensionAbility<sup>18+</sup> 93 94startServiceExtensionAbility(want: Want): Promise\<void> 95 96Starts a ServiceExtensionAbility. This API uses a promise to return the result. 97 98**Model restriction**: This API can be used only in the stage model. 99 100**System API**: This is a system API. 101 102**System capability**: SystemCapability.Ability.AbilityRuntime.Core 103 104**Parameters** 105 106| Name| Type| Mandatory| Description| 107| ------ | ------ | ------ | ------ | 108| want | [Want](js-apis-app-ability-want.md) | Yes| Want information for starting the ServiceExtensionAbility.| 109 110**Return value** 111 112| Type| Description| 113| ------ | ------ | 114| Promise<void> | Promise that returns no value.| 115 116**Error codes** 117 118For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 119 120| ID| Error Message| 121| ------ | ------ | 122| 201 | The application does not have permission to call the interface. | 123| 202 | The application is not system-app, can not use system-api. | 124| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 125| 16000001 | The specified ability does not exist. | 126| 16000002 | Incorrect ability type. | 127| 16000004 | Cannot start an invisible component. | 128| 16000005 | The specified process does not have the permission. | 129| 16000006 | Cross-user operations are not allowed. | 130| 16000008 | The crowdtesting application expires. | 131| 16000011 | The context does not exist. | 132| 16000012 | The application is controlled. | 133| 16000013 | The application is controlled by EDM. | 134| 16000019 | No matching ability is found. | 135| 16000050 | Internal error. | 136| 16200001 | The caller has been released. | 137 138**Example** 139 140```ts 141import { UIExtensionAbility, Want } from '@kit.AbilityKit'; 142import { BusinessError } from '@kit.BasicServicesKit'; 143 144export default class UIExtAbility extends UIExtensionAbility { 145 onForeground() { 146 let want: Want = { 147 bundleName: 'com.example.myapplication', 148 moduleName: 'entry', 149 abilityName: 'ServiceExtensionAbility' 150 }; 151 152 try { 153 this.context.startServiceExtensionAbility(want) 154 .then(() => { 155 // Carry out normal service processing. 156 console.info('startServiceExtensionAbility succeed'); 157 }) 158 .catch((err: BusinessError) => { 159 // Process service logic errors. 160 console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`); 161 }); 162 } catch (err) { 163 // Process input parameter errors. 164 let code = (err as BusinessError).code; 165 let message = (err as BusinessError).message; 166 console.error(`startServiceExtensionAbility failed, code is ${code}, message is ${message}`); 167 } 168 } 169} 170``` 171 172### startServiceExtensionAbilityWithAccount<sup>18+</sup> 173 174startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void> 175 176Starts a ServiceExtensionAbility under a specified system account. This API uses a promise to return the result. 177 178> **NOTE** 179> 180> For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 181> 182> Permission verification is not required when **accountId** specifies the current user. 183 184**Model restriction**: This API can be used only in the stage model. 185 186**System API**: This is a system API. 187 188**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 189 190**System capability**: SystemCapability.Ability.AbilityRuntime.Core 191 192**Parameters** 193 194| Name| Type| Mandatory| Description| 195| ------ | ------ | ------ | ------ | 196| want | [Want](js-apis-app-ability-want.md) | Yes| Want information for starting the ServiceExtensionAbility.| 197| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9).| 198 199**Return value** 200 201| Type| Description| 202| ------ | ------ | 203| Promise<void> | Promise that returns no value.| 204 205**Error codes** 206 207For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 208 209| ID| Error Message| 210| ------ | ------ | 211| 201 | The application does not have permission to call the interface. | 212| 202 | The application is not system-app, can not use system-api. | 213| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 214| 16000001 | The specified ability does not exist. | 215| 16000002 | Incorrect ability type. | 216| 16000004 | Cannot start an invisible component. | 217| 16000005 | The specified process does not have the permission. | 218| 16000006 | Cross-user operations are not allowed. | 219| 16000008 | The crowdtesting application expires. | 220| 16000011 | The context does not exist. | 221| 16000012 | The application is controlled. | 222| 16000013 | The application is controlled by EDM. | 223| 16000019 | No matching ability is found. | 224| 16000050 | Internal error. | 225| 16200001 | The caller has been released. | 226 227**Example** 228 229```ts 230import { UIExtensionAbility, Want } from '@kit.AbilityKit'; 231import { BusinessError } from '@kit.BasicServicesKit'; 232 233export default class UIExtAbility extends UIExtensionAbility { 234 onForeground() { 235 let want: Want = { 236 bundleName: 'com.example.myapplication', 237 moduleName: 'entry', 238 abilityName: 'ServiceExtensionAbility' 239 }; 240 let accountId = 100; 241 242 try { 243 this.context.startServiceExtensionAbilityWithAccount(want, accountId) 244 .then(() => { 245 // Carry out normal service processing. 246 console.info('startServiceExtensionAbilityWithAccount succeed'); 247 }) 248 .catch((err: BusinessError) => { 249 // Process service logic errors. 250 console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); 251 }); 252 } catch (err) { 253 // Process input parameter errors. 254 let code = (err as BusinessError).code; 255 let message = (err as BusinessError).message; 256 console.error(`startServiceExtensionAbilityWithAccount failed, code is ${code}, message is ${message}`); 257 } 258 } 259} 260``` 261 262### setHostPageOverlayForbidden<sup>15+</sup> 263 264setHostPageOverlayForbidden(isForbidden: boolean) : void 265 266Sets whether the page started by the [UIExtensionAbility](../apis-ability-kit/js-apis-app-ability-uiExtensionAbility.md) can be overlaid by the page of the user. 267 268> **NOTE** 269> 270> For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 271> 272> This API must be called before a window is created. You are advised to call it within the [onCreate](../apis-ability-kit/js-apis-app-ability-uiExtensionAbility.md#oncreate) lifecycle of the [UIExtensionAbility](../apis-ability-kit/js-apis-app-ability-uiExtensionAbility.md). 273 274**Model restriction**: This API can be used only in the stage model. 275 276**System API**: This is a system API. 277 278**System capability**: SystemCapability.Ability.AbilityRuntime.Core 279 280**Parameters** 281 282| Name| Type| Mandatory| Description| 283| ------ | ------ | ------ | ------ | 284| isForbidden | boolean | Yes| Whether the page started by the [UIExtensionAbility](../apis-ability-kit/js-apis-app-ability-uiExtensionAbility.md) can be overlaid by the page of the user. **true** if the page can be overlaid, **false** otherwise.| 285 286 287**Error codes** 288 289For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 290 291| ID| Error Message| 292| ------ | ------ | 293| 202 | The application is not system-app, can not use system-api. | 294| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 295 296**Example** 297 298```ts 299import { UIExtensionAbility } from '@kit.AbilityKit'; 300import { BusinessError } from '@kit.BasicServicesKit'; 301 302export default class UIExtAbility extends UIExtensionAbility { 303 OnCreate() { 304 try { 305 this.context.setHostPageOverlayForbidden(true) 306 } catch (err) { 307 // Process input parameter errors. 308 let code = (err as BusinessError).code; 309 let message = (err as BusinessError).message; 310 console.error(`setHostPageOverlayForbidden failed, code is ${code}, message is ${message}`); 311 } 312 } 313} 314``` 315 316### startUIAbilities<sup>20+</sup> 317 318startUIAbilities(wantList: Array\<Want>): Promise\<void> 319 320Starts multiple UIAbilities simultaneously. This API uses a promise to return the result. 321 322You can pass the Want information of multiple UIAbility instances, which can point to one or more applications. If all the UIAbility instances can be started successfully, the system displays these UIAbility instances in multiple windows simultaneously. Depending on the window handling, different devices may have varying display effects (including window shape, quantity, and layout). 323 324> **NOTE** 325> 326> This API takes effect only on phones and tablets. 327> 328> For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 329 330**System API**: This is a system API. 331 332**System capability**: SystemCapability.Ability.AbilityRuntime.Core 333 334**Parameters** 335 336| Name| Type| Mandatory| Description| 337| ------ | ------ | ------ | ------ | 338| wantList | Array\<[Want](js-apis-app-ability-want.md)> | Yes| List of launch parameters for multiple UIAbilities to be started simultaneously. A maximum of four Want objects can be passed. The **Want** parameter does not support implicit launch, cross-user launch, distributed launch, instant installation, or on-demand loading. By default, the main application is launched unless specified otherwise.| 339 340**Return value** 341 342| Type| Description| 343| -------- | -------- | 344| Promise<void> | Promise that returns no value.| 345 346**Error codes** 347 348For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 349 350| ID| Error Message| 351| ------ | ------ | 352| 201 | The application does not have permission to call the interface. | 353| 202 | Not system application. | 354| 801 | Capability not supported. | 355| 16000001 | The specified ability does not exist. | 356| 16000004 | Failed to start the invisible ability. | 357| 16000005 | The specified process does not have the permission. | 358| 16000006 | Cross-user operations are not allowed. | 359| 16000008 | The crowdtesting application expires. | 360| 16000009 | An ability cannot be started or stopped in Wukong mode. | 361| 16000011 | The context does not exist. | 362| 16000050 | Internal error. | 363| 16200001 | The caller has been released. | 364| 16000073 | The app clone index is invalid. | 365| 16000076 | The app instance key is invalid. | 366| 16000080 | Creating a new instance is not supported. | 367| 16000120 | A maximum of four UIAbility instances can be started simultaneously. The current parameter exceeds the maximum number or is less than 1.| 368| 16000121 | The target component type is not a UIAbility. | 369| 16000122 | The target component is blocked by the system module and does not support startup. | 370| 16000123 | Implicit startup is not supported. | 371| 16000124 | Starting a remote UIAbility is not supported. | 372| 16000125 | Starting a plugin UIAbility is not supported. | 373| 16000126 | Starting DLP files is not supported. | 374 375**Example** 376 377```ts 378import { UIExtensionAbility, Want } from '@kit.AbilityKit'; 379import { BusinessError } from '@kit.BasicServicesKit'; 380 381export default class EntryUIExtAbility extends UIExtensionAbility { 382 onForeground() { 383 let want1: Want = { 384 bundleName: 'com.example.myapplication1', 385 abilityName: 'EntryAbility' 386 }; 387 let want2: Want = { 388 bundleName: 'com.example.myapplication2', 389 abilityName: 'EntryAbility' 390 }; 391 let wantList: Array<Want> = [want1, want2]; 392 try { 393 this.context.startUIAbilities(wantList).then(() => { 394 console.info(`TestTag:: start succeeded.`); 395 }).catch((error: BusinessError) => { 396 console.info(`TestTag:: startUIAbilities failed: ${JSON.stringify(error)}`); 397 }); 398 } catch (paramError) { 399 // Process input parameter errors. 400 console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`); 401 } 402 } 403} 404``` 405