# UIAbilityContext (系统接口) UIAbilityContext是需要保存状态的[UIAbility](js-apis-app-ability-uiAbility.md)所对应的context,继承自[Context](js-apis-inner-application-context.md),提供UIAbility的相关配置信息以及操作UIAbility和ServiceExtensionAbility的方法,如启动UIAbility,停止当前UIAbilityContext所属的UIAbility,启动、停止、连接、断开连接ServiceExtensionAbility等。 > **说明:** > > - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 > - 本模块接口仅可在Stage模型下使用。 > - 本模块接口为系统接口。 ## 导入模块 ```ts import { common } from '@kit.AbilityKit'; ``` > **关于示例代码的说明:** > > 在本文档的示例中,通过`this.context`来获取`UIAbilityContext`,其中`this`代表继承自`UIAbility`的`UIAbility`实例。如需要在页面中使用`UIAbilityContext`提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 ## UIAbilityContext.startAbilityForResultWithAccount startAbilityForResultWithAccount(want: Want, accountId: number, callback: AsyncCallback\): void 启动一个Ability并在该Ability销毁时返回执行结果。使用callback异步回调。仅支持在主线程调用。 > **说明:** > > 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 > 当accountId为当前用户时,无需进行权限校验。 **需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | | accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | | callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | 是 | 启动Ability的回调函数,返回Ability结果。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000019 | No matching ability is found. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000071 | App clone is not supported. | | 16000072 | App clone or multi-instance is not supported. | | 16000073 | The app clone index is invalid. | | 16000076 | The app instance key is invalid. | | 16000077 | The number of app instances reaches the limit. | | 16000078 | The multi-instance is not supported. | | 16000079 | The APP_INSTANCE_KEY cannot be specified. | | 16000080 | Creating an instance is not supported. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, common, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; let accountId = 100; try { this.context.startAbilityForResultWithAccount(want, accountId, (err: BusinessError, result: common.AbilityResult) => { if (err.code) { // 处理业务逻辑错误 console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`); return; } // 执行正常业务 console.info('startAbilityForResultWithAccount succeed'); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startAbilityForResultWithAccount failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startAbilityForResultWithAccount startAbilityForResultWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\): void 启动一个Ability并在该Ability销毁时返回执行结果。使用callback异步回调。仅支持在主线程调用。 > **说明:** > > 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 > 当accountId为当前用户时,无需进行权限校验。 **需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | | accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | | options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 | | callback | AsyncCallback\ | 是 | 启动Ability后,Ability被销毁时的回调函数。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000019 | No matching ability is found. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000071 | App clone is not supported. | | 16000072 | App clone or multi-instance is not supported. | | 16000073 | The app clone index is invalid. | | 16000076 | The app instance key is invalid. | | 16000077 | The number of app instances reaches the limit. | | 16000078 | The multi-instance is not supported. | | 16000079 | The APP_INSTANCE_KEY cannot be specified. | | 16000080 | Creating an instance is not supported. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, StartOptions, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; let accountId = 100; let options: StartOptions = { displayId: 0 }; try { this.context.startAbilityForResultWithAccount(want, accountId, options, (err: BusinessError) => { if (err.code) { // 处理业务逻辑错误 console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`); return; } // 执行正常业务 console.info('startAbilityForResultWithAccount succeed'); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startAbilityForResultWithAccount failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startAbilityForResultWithAccount startAbilityForResultWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\ 启动一个Ability并在该Ability销毁时返回执行结果。使用Promise异步回调。仅支持在主线程调用。 > **说明:** > > 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 > 当accountId为当前用户时,无需进行权限校验。 **需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | | accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | | options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 | **返回值:** | 类型 | 说明 | | -------- | -------- | | Promise<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Ability被销毁时的回调函数,包含AbilityResult参数。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000019 | No matching ability is found. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000071 | App clone is not supported. | | 16000072 | App clone or multi-instance is not supported. | | 16000073 | The app clone index is invalid. | | 16000076 | The app instance key is invalid. | | 16000077 | The number of app instances reaches the limit. | | 16000078 | The multi-instance is not supported. | | 16000079 | The APP_INSTANCE_KEY cannot be specified. | | 16000080 | Creating an instance is not supported. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, StartOptions, Want, common } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; let accountId = 100; let options: StartOptions = { displayId: 0 }; try { this.context.startAbilityForResultWithAccount(want, accountId, options) .then((result: common.AbilityResult) => { // 执行正常业务 console.info('startAbilityForResultWithAccount succeed'); }) .catch((err: BusinessError) => { // 处理业务逻辑错误 console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startAbilityForResultWithAccount failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startServiceExtensionAbility startServiceExtensionAbility(want: Want, callback: AsyncCallback\): void 启动一个新的ServiceExtensionAbility(callback形式)。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 | | callback | AsyncCallback\ | 是 | 启动ServiceExtensionAbility的回调函数。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000019 | No matching ability is found. | | 16000050 | Internal error. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'ServiceExtensionAbility' }; try { this.context.startServiceExtensionAbility(want, (error: BusinessError) => { if (error.code) { // 处理业务逻辑错误 console.error(`startServiceExtensionAbility failed, code is ${error.code}, message is ${error.message}`); return; } // 执行正常业务 console.info('startServiceExtensionAbility succeed'); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startServiceExtensionAbility failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startServiceExtensionAbility startServiceExtensionAbility(want: Want): Promise\ 启动一个新的ServiceExtensionAbility(Promise形式)。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000019 | No matching ability is found. | | 16000050 | Internal error. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'ServiceExtensionAbility' }; try { this.context.startServiceExtensionAbility(want) .then(() => { // 执行正常业务 console.info('startServiceExtensionAbility succeed'); }) .catch((err: BusinessError) => { // 处理业务逻辑错误 console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startServiceExtensionAbility failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startServiceExtensionAbilityWithAccount startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\): void 启动一个新的ServiceExtensionAbility(callback形式)。 > **说明:** > > 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 > 当accountId为当前用户时,无需进行权限校验。 **需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 | | accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | | callback | AsyncCallback\ | 是 | 启动ServiceExtensionAbility的回调函数。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000019 | No matching ability is found. | | 16000050 | Internal error. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'ServiceExtensionAbility' }; let accountId = 100; try { this.context.startServiceExtensionAbilityWithAccount(want, accountId, (err: BusinessError) => { if (err.code) { // 处理业务逻辑错误 console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); return; } // 执行正常业务 console.info('startServiceExtensionAbilityWithAccount succeed'); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startServiceExtensionAbilityWithAccount failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startServiceExtensionAbilityWithAccount startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\ 启动一个新的ServiceExtensionAbility(Promise形式)。 > **说明:** > > 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 > 当accountId为当前用户时,无需进行权限校验。 **需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | | accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000019 | No matching ability is found. | | 16000050 | Internal error. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'ServiceExtensionAbility' }; let accountId = 100; try { this.context.startServiceExtensionAbilityWithAccount(want, accountId) .then(() => { // 执行正常业务 console.info('startServiceExtensionAbilityWithAccount succeed'); }) .catch((err: BusinessError) => { // 处理业务逻辑错误 console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startServiceExtensionAbilityWithAccount failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.stopServiceExtensionAbility stopServiceExtensionAbility(want: Want, callback: AsyncCallback\): void 停止同一应用程序内的服务(callback形式)。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 | | callback | AsyncCallback\ | 是 | 停止ServiceExtensionAbility的回调函数。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000050 | Internal error. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'ServiceExtensionAbility' }; try { this.context.stopServiceExtensionAbility(want, (err: BusinessError) => { if (err.code) { // 处理业务逻辑错误 console.error(`stopServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`); return; } // 执行正常业务 console.info('stopServiceExtensionAbility succeed'); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`stopServiceExtensionAbility failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.stopServiceExtensionAbility stopServiceExtensionAbility(want: Want): Promise\ 停止同一应用程序内的服务(Promise形式)。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'ServiceExtensionAbility' }; try { this.context.stopServiceExtensionAbility(want) .then(() => { // 执行正常业务 console.info('stopServiceExtensionAbility succeed'); }) .catch((err: BusinessError) => { // 处理业务逻辑错误 console.error(`stopServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`stopServiceExtensionAbility failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.stopServiceExtensionAbilityWithAccount stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\): void 停止同一应用程序内指定账户的服务(callback形式)。 > **说明:** > > 当accountId为当前用户时,无需进行权限校验。 **需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 | | accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | | callback | AsyncCallback\ | 是 | 停止ServiceExtensionAbility的回调函数。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'ServiceExtensionAbility' }; let accountId = 100; try { this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (err: BusinessError) => { if (err.code) { // 处理业务逻辑错误 console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); return; } // 执行正常业务 console.info('stopServiceExtensionAbilityWithAccount succeed'); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.stopServiceExtensionAbilityWithAccount stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\ 停止同一应用程序内指定账户的服务(Promise形式)。 > **说明:** > > 当accountId为当前用户时,无需进行权限校验。 **需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 | | accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'ServiceExtensionAbility' }; let accountId = 100; try { this.context.stopServiceExtensionAbilityWithAccount(want, accountId) .then(() => { // 执行正常业务 console.info('stopServiceExtensionAbilityWithAccount succeed'); }) .catch((err: BusinessError) => { // 处理业务逻辑错误 console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.connectServiceExtensionAbilityWithAccount connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number 将当前Ability连接到一个指定account的ServiceExtensionAbility。仅支持在主线程调用。 > **说明:** > > 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 > 当accountId为当前用户时,无需进行权限校验。 **需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | | accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | | options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | 与ServiceExtensionAbility建立连接后回调函数的实例。 | **返回值:** | 类型 | 说明 | | -------- | -------- | | number | 返回Ability连接的结果code。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | **示例:** ```ts import { UIAbility, Want, common } from '@kit.AbilityKit'; import { rpc } from '@kit.IPCKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'ServiceExtensionAbility' }; let accountId = 100; let commRemote: rpc.IRemoteObject; let options: common.ConnectOptions = { onConnect(elementName, remote) { commRemote = remote; console.info('onConnect...'); }, onDisconnect(elementName) { console.info('onDisconnect...'); }, onFailed(code) { console.info('onFailed...'); } }; let connection: number; try { connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`connectServiceExtensionAbility failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startAbilityWithAccount startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\): void 根据want和accountId启动Ability。使用callback异步回调。仅支持在主线程调用。 > **说明:** > > 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 > 当accountId为当前用户时,无需进行权限校验。 **需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | | accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | | callback | AsyncCallback\ | 是 | 启动Ability的回调函数。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000019 | No matching ability is found. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000071 | App clone is not supported. | | 16000072 | App clone or multi-instance is not supported. | | 16000073 | The app clone index is invalid. | | 16000076 | The app instance key is invalid. | | 16000077 | The number of app instances reaches the limit. | | 16000078 | The multi-instance is not supported. | | 16000079 | The APP_INSTANCE_KEY cannot be specified. | | 16000080 | Creating an instance is not supported. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; let accountId = 100; try { this.context.startAbilityWithAccount(want, accountId, (err: BusinessError) => { if (err.code) { // 处理业务逻辑错误 console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); return; } // 执行正常业务 console.info('startAbilityWithAccount succeed'); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startAbilityWithAccount failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startAbilityWithAccount startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\): void 根据want、accountId及startOptions启动Ability。使用callback异步回调。仅支持在主线程调用。 > **说明:** > > 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 > 当accountId为当前用户时,无需进行权限校验。 **需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | | accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。| | options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 | | callback | AsyncCallback\ | 是 | 启动Ability的回调函数。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000019 | No matching ability is found. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000071 | App clone is not supported. | | 16000072 | App clone or multi-instance is not supported. | | 16000073 | The app clone index is invalid. | | 16000076 | The app instance key is invalid. | | 16000077 | The number of app instances reaches the limit. | | 16000078 | The multi-instance is not supported. | | 16000079 | The APP_INSTANCE_KEY cannot be specified. | | 16000080 | Creating an instance is not supported. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, Want, StartOptions } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; let accountId = 100; let options: StartOptions = { displayId: 0 }; try { this.context.startAbilityWithAccount(want, accountId, options, (err: BusinessError) => { if (err.code) { // 处理业务逻辑错误 console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); return; } // 执行正常业务 console.info('startAbilityWithAccount succeed'); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startAbilityWithAccount failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startAbilityWithAccount startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\ 根据want、accountId和startOptions启动Ability。使用Promise异步回调。仅支持在主线程调用。 > **说明:** > > 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 > 当accountId为当前用户时,无需进行权限校验。 **需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | | accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | | options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000019 | No matching ability is found. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000071 | App clone is not supported. | | 16000072 | App clone or multi-instance is not supported. | | 16000073 | The app clone index is invalid. | | 16000076 | The app instance key is invalid. | | 16000077 | The number of app instances reaches the limit. | | 16000078 | The multi-instance is not supported. | | 16000079 | The APP_INSTANCE_KEY cannot be specified. | | 16000080 | Creating an instance is not supported. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, Want, StartOptions } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; let accountId = 100; let options: StartOptions = { displayId: 0 }; try { this.context.startAbilityWithAccount(want, accountId, options) .then(() => { // 执行正常业务 console.info('startAbilityWithAccount succeed'); }) .catch((err: BusinessError) => { // 处理业务逻辑错误 console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startAbilityWithAccount failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.setMissionIcon setMissionIcon(icon: image.PixelMap, callback: AsyncCallback\): void 设置当前ability在任务中显示的图标, 图标大小最大为600M(callback形式)。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | icon | image.PixelMap | 是 | 在最近的任务中显示的ability图标。 | | callback | AsyncCallback\ | 是 | 指定的回调函数的结果。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | **示例:** ```ts import { UIAbility } from '@kit.AbilityKit'; import { image } from '@kit.ImageKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let imagePixelMap: image.PixelMap; let color = new ArrayBuffer(0); image.createPixelMap(color, { size: { height: 100, width: 100 } }).then((data) => { imagePixelMap = data; this.context.setMissionIcon(imagePixelMap, (err: BusinessError) => { console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`); }) }).catch((err: BusinessError) => { console.error(`createPixelMap failed, code is ${err.code}, message is ${err.message}`); }); } } ``` ## UIAbilityContext.setMissionIcon setMissionIcon(icon: image.PixelMap): Promise\ 设置当前ability在任务中显示的图标, 图标大小最大为600M(promise形式)。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | icon | image.PixelMap | 是 | 在最近的任务中显示的ability图标。 | **返回值:** | 类型 | 说明 | | -------- | -------- | | Promise<void> | 返回一个Promise,包含接口的结果。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | **示例:** ```ts import { UIAbility } from '@kit.AbilityKit'; import { image } from '@kit.ImageKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let imagePixelMap: image.PixelMap; let color = new ArrayBuffer(0); image.createPixelMap(color, { size: { height: 100, width: 100 } }).then((data) => { imagePixelMap = data; this.context.setMissionIcon(imagePixelMap) .then(() => { console.info('setMissionIcon succeed'); }) .catch((err: BusinessError) => { console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`); }); }).catch((err: BusinessError) => { console.error(`createPixelMap failed, code is ${err.code}, message is ${err.message}`); }); } } ``` ## UIAbilityContext.startRecentAbility startRecentAbility(want: Want, callback: AsyncCallback<void>): void 启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。使用callback异步回调。仅支持在主线程调用。 > **说明:** > > 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 需要启动Ability的want信息。 | | callback | AsyncCallback\ | 是 | 指定的回调函数的结果。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | | 16000073 | The app clone index is invalid. | **示例:** ```ts import { UIAbility, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; try { this.context.startRecentAbility(want, (err: BusinessError) => { if (err.code) { // 处理业务逻辑错误 console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); return; } // 执行正常业务 console.info('startRecentAbility succeed'); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startRecentAbility startRecentAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void 启动一个指定的Ability。如果这个Ability有多个实例,将拉起最近启动的那个实例。当开发者需要携带启动参数时可以选择此API。使用callback异步回调。仅支持在主线程调用。 > **说明:** > > 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 需要启动Ability的want信息。 | | options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 | | callback | AsyncCallback\ | 是 | 指定的回调函数的结果。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | | 16000073 | The app clone index is invalid. | **示例:** ```ts import { UIAbility, Want, StartOptions } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; let options: StartOptions = { displayId: 0 }; try { this.context.startRecentAbility(want, options, (err: BusinessError) => { if (err.code) { // 处理业务逻辑错误 console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); return; } // 执行正常业务 console.info('startRecentAbility succeed'); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startRecentAbility startRecentAbility(want: Want, options?: StartOptions): Promise<void> 启动一个指定的Ability。如果这个Ability有多个实例,将拉起最近启动的那个实例。使用Promise异步回调。仅支持在主线程调用。 > **说明:** > > 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 需要启动Ability的want信息。 | | options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | | 16000073 | The app clone index is invalid. | **示例:** ```ts import { UIAbility, Want, StartOptions } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; let options: StartOptions = { displayId: 0, }; try { this.context.startRecentAbility(want, options) .then(() => { // 执行正常业务 console.info('startRecentAbility succeed'); }) .catch((err: BusinessError) => { // 处理业务逻辑错误 console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startAbilityByCallWithAccount10+ startAbilityByCallWithAccount(want: Want, accountId: number): Promise<Caller> 根据accountId对指定的Ability进行call调用,并且可以使用返回的Caller通信接口与被调用方进行通信。仅支持在主线程调用。 该接口不支持拉起启动模式为[specified模式](../../application-models/uiability-launch-type.md#specified启动模式)的UIAbility。 使用规则: - 跨用户场景下,Call调用目标Ability时,调用方应用需同时申请`ohos.permission.ABILITY_BACKGROUND_COMMUNICATION`与`ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS`权限。 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限。 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限。 - 同设备与跨设备场景下,该接口的使用规则存在差异,详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 **需要权限**:ohos.permission.ABILITY_BACKGROUND_COMMUNICATION, ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId(可选)、parameters(可选),其中deviceId缺省或为空表示启动本地Ability,parameters缺省或为空表示后台启动Ability。 | | accountId | number | 是 | 系统账号的账号ID,-1表示当前活动用户,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | **返回值:** | 类型 | 说明 | | -------- | -------- | | Promise<[Caller](js-apis-app-ability-uiAbility.md#caller)> | 获取要通讯的caller对象。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | Static permission denied. The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000050 | Internal error. | | 16000071 | App clone is not supported. | | 16000072 | App clone or multi-instance is not supported. | | 16000073 | The app clone index is invalid. | | 16000076 | The app instance key is invalid. | | 16000077 | The number of app instances reaches the limit. | | 16000078 | The multi-instance is not supported. | | 16000079 | The APP_INSTANCE_KEY cannot be specified. | | 16000080 | Creating an instance is not supported. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, Want, Caller } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let caller: Caller; // 系统账号的账号ID, -1表示当前激活用户 let accountId = -1; // 指定启动的Ability let want: Want = { bundleName: 'com.acts.actscalleeabilityrely', moduleName: 'entry', abilityName: 'EntryAbility', deviceId: '', parameters: { // 'ohos.aafwk.param.callAbilityToForeground' 值设置为true时为前台启动, 设置false或不设置为后台启动 'ohos.aafwk.param.callAbilityToForeground': true } }; try { this.context.startAbilityByCallWithAccount(want, accountId) .then((obj: Caller) => { // 执行正常业务 caller = obj; console.log('startAbilityByCallWithAccount succeed'); }).catch((error: BusinessError) => { // 处理业务逻辑错误 console.error(`startAbilityByCallWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`); }); } catch (paramError) { // 处理入参错误异常 console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`); } } } ``` ## UIAbilityContext.startAbilityAsCaller10+ startAbilityAsCaller(want: Want, callback: AsyncCallback\): void 使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用callback异步回调。仅支持在主线程调用。 > **说明:** > > 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | | callback | AsyncCallback<void> | 是 | 回调函数。当启动Ability成功,err为undefined,否则为错误对象。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000071 | App clone is not supported. | | 16000072 | App clone or multi-instance is not supported. | | 16000073 | The app clone index is invalid. | | 16000076 | The app instance key is invalid. | | 16000077 | The number of app instances reaches the limit. | | 16000078 | The multi-instance is not supported. | | 16000079 | The APP_INSTANCE_KEY cannot be specified. | | 16000080 | Creating an instance is not supported. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit'; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { // want包含启动该应用的Caller信息 let localWant: Want = want; localWant.bundleName = 'com.example.demo'; localWant.moduleName = 'entry'; localWant.abilityName = 'TestAbility'; // 使用启动方的Caller身份信息启动新Ability this.context.startAbilityAsCaller(localWant, (err) => { if (err && err.code != 0) { console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err)); } else { console.log('startAbilityAsCaller success.'); } }) } } ``` ## UIAbilityContext.startAbilityAsCaller10+ startAbilityAsCaller(want: Want, options: StartOptions, callback: AsyncCallback\): void 使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用callback异步回调。仅支持在主线程调用。 > **说明:** > > 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | | options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 | | callback | AsyncCallback<void> | 是 | 回调函数。当启动Ability成功,err为undefined,否则为错误对象。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.| | 16000001 | The specified ability does not exist. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000071 | App clone is not supported. | | 16000072 | App clone or multi-instance is not supported. | | 16000073 | The app clone index is invalid. | | 16000076 | The app instance key is invalid. | | 16000077 | The number of app instances reaches the limit. | | 16000078 | The multi-instance is not supported. | | 16000079 | The APP_INSTANCE_KEY cannot be specified. | | 16000080 | Creating an instance is not supported. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, Want, AbilityConstant, StartOptions } from '@kit.AbilityKit'; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { // want包含启动该应用的Caller信息 let localWant: Want = want; localWant.bundleName = 'com.example.demo'; localWant.moduleName = 'entry'; localWant.abilityName = 'TestAbility'; let option: StartOptions = { displayId: 0 }; // 使用启动方的Caller身份信息启动新Ability this.context.startAbilityAsCaller(localWant, option, (err) => { if (err && err.code != 0) { console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err)); } else { console.log('startAbilityAsCaller success.'); } }) } } ``` ## UIAbilityContext.startAbilityAsCaller10+ startAbilityAsCaller(want: Want, options?: StartOptions): Promise\ 使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用Promise异步回调。仅支持在主线程调用。 > **说明:** > > 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | | options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 | **返回值:** | 类型 | 说明 | | -------- | -------- | | Promise<void> | Promise对象。无返回结果的Promise对象。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000071 | App clone is not supported. | | 16000072 | App clone or multi-instance is not supported. | | 16000073 | The app clone index is invalid. | | 16000076 | The app instance key is invalid. | | 16000077 | The number of app instances reaches the limit. | | 16000078 | The multi-instance is not supported. | | 16000079 | The APP_INSTANCE_KEY cannot be specified. | | 16000080 | Creating an instance is not supported. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | **示例:** ```ts import { UIAbility, Want, AbilityConstant, StartOptions } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { // want包含启动该应用的Caller信息 let localWant: Want = want; localWant.bundleName = 'com.example.demo'; localWant.moduleName = 'entry'; localWant.abilityName = 'TestAbility'; let option: StartOptions = { displayId: 0 }; // 使用启动方的Caller身份信息启动新Ability this.context.startAbilityAsCaller(localWant, option) .then(() => { console.log('startAbilityAsCaller success.'); }) .catch((err: BusinessError) => { console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err)); }) } } ``` ## UIAbilityContext.requestModalUIExtension11+ requestModalUIExtension(pickerWant: Want): Promise\ 请求在指定的前台应用上拉起对应类型的UIExtensionAbility。使用Promise异步回调。仅支持在主线程调用。 其中,前台应用通过want.parameters中bundleName来指定,如果未指定前台应用、bundleName指定的应用未在前台或指定的前台应用的bundleName不正确,则在系统界面上直接拉起UIExtensionAbility;被拉起的UIExtensionAbility通过want中bundleName、abilityName、moduleName字段共同确定,同时需要通过want.parameters中的ability.want.params.uiExtensionType字段配置UIExtensionAbility的类型。 在前台应用上拉起UIExtensionAility之前,必须确保该应用已完成页面初始化,否则将导致拉起失败、并出现"uiContent is nullptr"的报错信息。应用可通过监听页面加载状态来判断拉起UIExtensionAbility的时机,页面初始化成功后会出现关键日志信息"UIContentImpl: focus again"。 > **说明:** > > 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | pickerWant | [Want](js-apis-app-ability-want.md) | 是 | 拉起UIExtension的want信息。 | **返回值:** | 类型 | 说明 | | -------- | -------- | | Promise<void> | Promise对象。无返回结果的Promise对象。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000050 | Internal error. | **示例:** ```ts import { UIAbility, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { bundleName: 'com.example.myapplication', abilityName: 'com.example.myapplication.UIExtAbility', moduleName: 'entry_test', parameters: { 'bundleName': 'com.example.myapplication', //与com.example.myapplication.UIExtAbility配置的type相同 'ability.want.params.uiExtensionType': 'sys/commonUI' } }; try { this.context.requestModalUIExtension(want) .then(() => { // 执行正常业务 console.info('requestModalUIExtension succeed'); }) .catch((err: BusinessError) => { // 处理业务逻辑错误 console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.requestModalUIExtension11+ requestModalUIExtension(pickerWant: Want, callback: AsyncCallback\): void 请求在指定的前台应用上拉起对应类型的UIExtensionAbility。使用callback异步回调。仅支持在主线程调用。 其中,前台应用通过want.parameters中bundleName来指定,如果未指定前台应用、bundleName指定的应用未在前台或指定的前台应用的bundleName不正确,则在系统界面上直接拉起UIExtensionAbility;被拉起的UIExtensionAbility通过want中bundleName、abilityName、moduleName字段共同确定,同时需要通过want.parameters中的ability.want.params.uiExtensionType字段配置UIExtensionAbility的类型。 在前台应用上拉起UIExtensionAility之前,必须确保该应用已完成页面初始化,否则将导致拉起失败、并出现"uiContent is nullptr"的报错信息。应用可通过监听页面加载状态来判断拉起UIExtensionAbility的时机,页面初始化成功后会出现关键日志信息"UIContentImpl: focus again"。 > **说明:** > > 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **系统接口**:此接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | pickerWant | [Want](js-apis-app-ability-want.md) | 是 | 拉起UIExtension的want信息。 | | callback | AsyncCallback<void> | 是 | 回调函数。当拉起UIExtension成功,err为undefined,否则为错误对象。 | **错误码:** 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 | 错误码ID | 错误信息 | | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000050 | Internal error. | **示例:** ```ts import { UIAbility, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { bundleName: 'com.example.myapplication', abilityName: 'UIExtAbility', moduleName: 'entry_test', parameters: { 'bundleName': 'com.example.myapplication', //与com.example.myapplication.UIExtAbility配置的type相同 'ability.want.params.uiExtensionType': 'sys/commonUI' } }; try { this.context.requestModalUIExtension(want, (err: BusinessError) => { if (err.code) { // 处理业务逻辑错误 console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`); return; } // 执行正常业务 console.info('requestModalUIExtension succeed'); }); } catch (err) { // 处理入参错误异常 let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`); } } } ```