1e41f4b71Sopenharmony_ci# UIExtensionContext (系统接口)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciUIExtensionContext是[UIExtensionAbility](js-apis-app-ability-uiExtensionAbility.md)的上下文环境,继承自[ExtensionContext](js-apis-inner-application-extensionContext.md),提供UIExtensionAbility的相关配置信息以及操作UIAbility的方法,如启动UIAbility等。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明:**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci>  - 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8e41f4b71Sopenharmony_ci>  - 本模块接口仅可在Stage模型下使用。
9e41f4b71Sopenharmony_ci>  - 本模块接口为系统接口。
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## 导入模块
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci```ts
14e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
15e41f4b71Sopenharmony_ci```
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## UIExtensionContext.startAbilityForResultAsCaller
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_cistartAbilityForResultAsCaller(want: Want, options?: StartOptions): Promise<AbilityResult>
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用Promise异步回调。
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci - 正常情况下可通过调用[terminateSelfWithResult](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateselfwithresult)接口使之终止并且返回结果给调用方。
24e41f4b71Sopenharmony_ci - 异常情况下比如杀死Ability会返回异常信息给调用方, 异常信息中resultCode为-1。
25e41f4b71Sopenharmony_ci - 如果被启动的Ability模式是单实例模式, 不同应用多次调用该接口启动这个Ability,当这个Ability调用[terminateSelfWithResult](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateselfwithresult)接口使之终止时,只将正常结果返回给最后一个调用方, 其它调用方返回异常信息, 异常信息中resultCode为-1。
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci> **说明:**
28e41f4b71Sopenharmony_ci>
29e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci**模型约束**:此接口仅可在Stage模型下使用。
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci**系统接口**:此接口为系统接口。
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci**参数:**
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci| 参数名  | 类型                                                | 必填 | 说明                      |
40e41f4b71Sopenharmony_ci| ------- | --------------------------------------------------- | ---- | ------------------------- |
41e41f4b71Sopenharmony_ci| want    | [Want](js-apis-app-ability-want.md)                 | 是   | 启动Ability的want信息。   |
42e41f4b71Sopenharmony_ci| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否   | 启动Ability所携带的参数。 |
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci**返回值:**
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci| 类型                                                         | 说明                      |
47e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | ------------------------- |
48e41f4b71Sopenharmony_ci| Promise<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Promise对象,返回Ability结果对象。 |
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci**错误码:**
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                                |
55e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------- |
56e41f4b71Sopenharmony_ci| 401| Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
57e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist.                   |
58e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability.                      |
59e41f4b71Sopenharmony_ci| 16000050 | Internal error.                                         |
60e41f4b71Sopenharmony_ci| 16000073 | The app clone index is invalid. |
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci**示例:**
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci```ts
65e41f4b71Sopenharmony_ciimport { UIExtensionAbility } from '@kit.AbilityKit';
66e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ciexport default class UIExtension extends UIExtensionAbility {
69e41f4b71Sopenharmony_ci  onForeground() {
70e41f4b71Sopenharmony_ci    this.context.startAbilityForResultAsCaller({
71e41f4b71Sopenharmony_ci      bundleName: 'com.example.startabilityforresultascaller',
72e41f4b71Sopenharmony_ci      abilityName: 'EntryAbility',
73e41f4b71Sopenharmony_ci      moduleName: 'entry'
74e41f4b71Sopenharmony_ci    }).then((data) => {
75e41f4b71Sopenharmony_ci      console.log('=======>startAbilityForResultAsCaller data Promise ======>' + JSON.stringify(data));
76e41f4b71Sopenharmony_ci    }).catch((error: BusinessError) => {
77e41f4b71Sopenharmony_ci      console.log('=======>startAbilityForResultAsCaller error.code Promise ======>' + error.code);
78e41f4b71Sopenharmony_ci    });
79e41f4b71Sopenharmony_ci  }
80e41f4b71Sopenharmony_ci}
81e41f4b71Sopenharmony_ci```
82