1# @ohos.app.ability.InsightIntentContext (意图执行上下文) 2 3<!--Kit: Ability Kit--> 4<!--Subsystem: Ability--> 5<!--Owner: @zhangyafei-echo; @linjunjie6--> 6<!--Designer: @li-weifeng2--> 7<!--Tester: @lixueqing513--> 8<!--Adviser: @huipeizi--> 9 10本模块提供意图执行上下文,是[意图执行基类](./js-apis-app-ability-insightIntentExecutor.md)和[@InsightIntentEntry的意图执行基类](./js-apis-app-ability-InsightIntentEntryExecutor.md)的属性,为意图执行提供基础能力,例如启动本应用内的[UIAbility组件](./js-apis-app-ability-uiAbility.md)。 11 12> **说明:** 13> 14> 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 15> 16> 本模块接口仅可在Stage模型下使用。 17 18## 导入模块 19 20```ts 21import { InsightIntentContext } from '@kit.AbilityKit'; 22``` 23 24## InsightIntentContext.startAbility 25 26startAbility(want: Want, callback: AsyncCallback\<void\>): void 27 28启动UIAbility组件,仅支持启动本应用内的UIAbility组件。使用callback异步回调。 29 30**模型约束**:此接口仅可在Stage模型下使用。 31 32**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 33 34**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 35 36**参数:** 37 38| 参数名 | 类型 | 必填 | 说明 | 39| -------- | -------- | -------- | -------- | 40| want | [Want](js-apis-app-ability-want.md) | 是 | 启动UIAbility组件的want信息。 | 41| callback | AsyncCallback<void> | 是 | 回调函数。当接口调用成功,err为undefined,否则为错误对象。 | 42 43**错误码:** 44 45以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 46 47| 错误码ID | 错误信息 | 48| -------- | -------- | 49| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 50| 16000001 | The specified ability does not exist. | 51| 16000004 | Cannot start an invisible component. | 52| 16000005 | The specified process does not have the permission. | 53| 16000006 | Cross-user operations are not allowed. | 54| 16000008 | The crowdtesting application expires. | 55| 16000009 | An ability cannot be started or stopped in Wukong mode. | 56| 16000011 | The context does not exist. | 57| 16000012 | The application is controlled. | 58| 16000013 | The application is controlled by EDM. | 59| 16000050 | Internal error. | 60| 16000053 | The ability is not on the top of the UI. | 61| 16000055 | Installation-free timed out. | 62| 16000061 | Operation not supported. | 63| 16200001 | The caller has been released. | 64 65**示例:** 66 67 ```ts 68 import { InsightIntentExecutor, insightIntent, Want } from '@kit.AbilityKit'; 69 import { window } from '@kit.ArkUI'; 70 import { hilog } from '@kit.PerformanceAnalysisKit'; 71 72 export default class IntentExecutorImpl extends InsightIntentExecutor { 73 onExecuteInUIAbilityForegroundMode(name: string, param: Record<string, Object>, pageLoader: window.WindowStage): insightIntent.ExecuteResult { 74 let want: Want = { 75 bundleName: 'com.ohos.intentexecutedemo', 76 moduleName: 'entry', 77 abilityName: 'AnotherAbility', 78 }; 79 80 try { 81 this.context.startAbility(want, (error) => { 82 if (error) { 83 hilog.error(0x0000, 'testTag', 'Start ability failed with %{public}s', JSON.stringify(error)); 84 } else { 85 hilog.info(0x0000, 'testTag', '%{public}s', 'Start ability succeed'); 86 } 87 }) 88 } catch (error) { 89 hilog.error(0x0000, 'testTag', 'Start ability error caught %{public}s', JSON.stringify(error)); 90 } 91 92 let result: insightIntent.ExecuteResult = { 93 code: 0, 94 result: { 95 message: 'Execute insight intent succeed.', 96 } 97 }; 98 return result; 99 } 100 } 101 ``` 102 103## InsightIntentContext.startAbility 104 105startAbility(want: Want): Promise\<void\> 106 107启动UIAbility组件,仅支持启动本应用内的UIAbility组件。使用Promise异步回调。 108 109**模型约束**:此接口仅可在Stage模型下使用。 110 111**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 112 113**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 114 115**参数:** 116 117| 参数名 | 类型 | 必填 | 说明 | 118| -------- | -------- | -------- | -------- | 119| want | [Want](js-apis-app-ability-want.md) | 是 | 启动UIAbility组件的want信息。 | 120 121**返回值:** 122 123| 类型 | 说明 | 124| -------- | -------- | 125| Promise<void> | Promise对象,无返回结果。 | 126 127**错误码:** 128 129以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 130 131| 错误码ID | 错误信息 | 132| -------- | -------- | 133| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 134| 16000001 | The specified ability does not exist. | 135| 16000004 | Cannot start an invisible component. | 136| 16000005 | The specified process does not have the permission. | 137| 16000006 | Cross-user operations are not allowed. | 138| 16000008 | The crowdtesting application expires. | 139| 16000009 | An ability cannot be started or stopped in Wukong mode. | 140| 16000011 | The context does not exist. | 141| 16000012 | The application is controlled. | 142| 16000013 | The application is controlled by EDM. | 143| 16000050 | Internal error. | 144| 16000053 | The ability is not on the top of the UI. | 145| 16000055 | Installation-free timed out. | 146| 16000061 | Operation not supported. | 147| 16200001 | The caller has been released. | 148 149**示例:** 150 151 ```ts 152 import { InsightIntentExecutor, insightIntent, Want } from '@kit.AbilityKit'; 153 import { window } from '@kit.ArkUI'; 154 import { hilog } from '@kit.PerformanceAnalysisKit'; 155 156 export default class IntentExecutorImpl extends InsightIntentExecutor { 157 async onExecuteInUIAbilityForegroundMode(name: string, param: Record<string, Object>, pageLoader: window.WindowStage): Promise<insightIntent.ExecuteResult> { 158 let want: Want = { 159 bundleName: 'com.ohos.intentexecutedemo', 160 moduleName: 'entry', 161 abilityName: 'AnotherAbility', 162 }; 163 164 try { 165 await this.context.startAbility(want); 166 hilog.info(0x0000, 'testTag', '%{public}s', 'Start ability finished'); 167 } catch (error) { 168 hilog.error(0x0000, 'testTag', 'Start ability error caught %{public}s', JSON.stringify(error)); 169 } 170 171 let result: insightIntent.ExecuteResult = { 172 code: 0, 173 result: { 174 message: 'Execute insight intent succeed.', 175 } 176 }; 177 return result; 178 } 179 } 180 ``` 181