1# @ohos.app.ability.insightIntentDriver (执行意图调用)(系统接口) 2 3本模块提供执行意图调用的能力,系统根据用户交互等信息执行意图调用。 4 5> **说明:** 6> 7> 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> 本模块接口仅可在Stage模型下使用。 10> 11> 本模块为系统接口。 12 13## 导入模块 14 15```ts 16import { insightIntentDriver } from '@kit.AbilityKit'; 17``` 18 19## ExecuteParam 20 21执行意图调用的参数。 22 23**模型约束**:此接口仅可在Stage模型下使用。 24 25**系统接口**:此接口为系统接口。 26 27**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 28 29| 名称 | 类型 | 必填 | 说明 | 30| -------- | -------- | -------- | -------- | 31| bundleName | string | 是 | 意图调用Ability所属的应用名称。 | 32| moduleName | string | 是 | 意图调用Ability所属的模块名称。 | 33| abilityName | string | 是 | 意图调用Ability名称。 | 34| insightIntentName | string | 是 | 意图调用名称。 | 35| insightIntentParam | string | 是 | 意图调用参数。 | 36| executeMode | [insightIntent.ExecuteMode](js-apis-app-ability-insightIntent.md#executemode) | 是 | 意图调用执行模式。 | 37| displayId<sup>12+</sup> | number | 否 | 意图调用时指定的物理屏幕id,该参数应为整数,仅在executeMode为UI_ABILITY_FOREGROUND时生效。 | 38| uris<sup>18+</sup> | Array<string> | 否 | 意图调用时,意图调用方给意图执行方授权的URI列表。 | 39| flags<sup>18+</sup> | number | 否 | 意图调用时,意图调用方给意图执行方授权的uris的[flags](js-apis-app-ability-wantConstant.md#flags)。 <br/>**说明:**<br/>该参数仅支持FLAG_AUTH_READ_URI_PERMISSION、FLAG_AUTH_WRITE_URI_PERMISSION、FLAG_AUTH_READ_URI_PERMISSION\|FLAG_AUTH_WRITE_URI_PERMISSION。| 40 41## insightIntentDriver.execute 42 43execute(param: ExecuteParam, callback: AsyncCallback<insightIntent.ExecuteResult>): void 44 45执行意图调用的接口。使用callback异步回调。 46当调用方在后台时,需要申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限。 47当意图调用执行模式[ExecuteMode](js-apis-app-ability-insightIntent.md#executemode)取值为UI_ABILITY_BACKGROUND时,需要申请`ohos.permission.ABILITY_BACKGROUND_COMMUNICATION`权限。 48 49**模型约束**:此接口仅可在Stage模型下使用。 50 51**系统接口**:此接口为系统接口。 52 53**需要权限**:ohos.permission.EXECUTE_INSIGHT_INTENT 54 55**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 56 57**参数:** 58 59 | 参数名 | 类型 | 必填 | 说明 | 60 | -------- | -------- | -------- | -------- | 61 | param | [ExecuteParam](#executeparam) | 是 | 执行意图调用的参数。 | 62 | callback | AsyncCallback<[insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult)> | 是 | 回调函数,返回意图调用执行结果。 | 63 64**错误码**: 65 66以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 67 68| 错误码ID | 错误信息 | 69| -------- | -------- | 70| 201 | Permission denied. | 71| 202 | Not system application. | 72| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 73| 16000001 | The specified ability does not exist. | 74| 16000002 | Incorrect ability type. | 75| 16000004 | Failed to start the invisible ability. | 76| 16000005 | The specified process does not have the permission. | 77| 16000006 | Cross-user operations are not allowed. | 78| 16000008 | The crowdtesting application expires. | 79| 16000009 | An ability cannot be started or stopped in Wukong mode. | 80| 16000010 | The call with the continuation flag is forbidden. | 81| 16000011 | The context does not exist. | 82| 16000012 | The application is controlled. | 83| 16000013 | The application is controlled by EDM. | 84| 16000050 | Internal error. | 85| 16000053 | The ability is not on the top of the UI. | 86| 16000055 | Installation-free timed out. | 87 88**示例:** 89 90```ts 91 import { insightIntentDriver, insightIntent } from '@kit.AbilityKit'; 92 import { hilog } from '@kit.PerformanceAnalysisKit'; 93 94 function executeInsightIntentAsync() { 95 let param: insightIntentDriver.ExecuteParam = { 96 bundleName: 'com.ohos.intentexecutedemo', 97 moduleName: 'entry', 98 abilityName: 'EntryAbility', 99 insightIntentName: 'PlayMusic', 100 insightIntentParam: { 101 songName: 'City Of Stars', 102 }, 103 executeMode: insightIntent.ExecuteMode.UI_ABILITY_FOREGROUND, 104 }; 105 106 try { 107 insightIntentDriver.execute(param, (error, data: insightIntent.ExecuteResult) => { 108 if (error) { 109 hilog.error(0x0000, 'testTag', 'execute insight intent failed with %{public}s', JSON.stringify(error)); 110 } else { 111 hilog.info(0x0000, 'testTag', '%{public}s', 'execute insight intent succeed'); 112 } 113 hilog.info(0x0000, 'testTag', 'execute insight intent return %{public}d', data.code); 114 hilog.info(0x0000, 'testTag', 'execute insight intent result %{public}s', JSON.stringify(data.result)); 115 }) 116 } catch (error) { 117 hilog.error(0x0000, 'testTag', 'execute insight intent error caught %{public}s', JSON.stringify(error)); 118 } 119 } 120``` 121 122## insightIntentDriver.execute 123 124execute(param: ExecuteParam): Promise<insightIntent.ExecuteResult> 125 126执行意图调用的接口。使用Promise异步回调。 127当调用方在后台时,需要申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限。 128当意图调用执行模式[ExecuteMode](js-apis-app-ability-insightIntent.md#executemode)取值为UI_ABILITY_BACKGROUND时,需要申请`ohos.permission.ABILITY_BACKGROUND_COMMUNICATION`权限。 129 130**模型约束**:此接口仅可在Stage模型下使用。 131 132**系统接口**:此接口为系统接口。 133 134**需要权限**:ohos.permission.EXECUTE_INSIGHT_INTENT 135 136**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 137 138**参数:** 139 140 | 参数名 | 类型 | 必填 | 说明 | 141 | -------- | -------- | -------- | -------- | 142 | param | [ExecuteParam](#executeparam) | 是 | 执行意图调用的参数。 | 143 144**返回值:** 145 146| 类型 | 说明 | 147| -------- | -------- | 148| Promise<[insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult)> | Promise对象,返回意图调用执行结果。 | 149 150**错误码**: 151 152以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 153 154| 错误码ID | 错误信息 | 155| -------- | -------- | 156| 201 | Permission denied. | 157| 202 | Not system application. | 158| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 159| 16000001 | The specified ability does not exist. | 160| 16000002 | Incorrect ability type. | 161| 16000004 | Failed to start the invisible ability. | 162| 16000005 | The specified process does not have the permission. | 163| 16000006 | Cross-user operations are not allowed. | 164| 16000008 | The crowdtesting application expires. | 165| 16000009 | An ability cannot be started or stopped in Wukong mode. | 166| 16000010 | The call with the continuation flag is forbidden. | 167| 16000011 | The context does not exist. | 168| 16000012 | The application is controlled. | 169| 16000013 | The application is controlled by EDM. | 170| 16000050 | Internal error. | 171| 16000053 | The ability is not on the top of the UI. | 172| 16000055 | Installation-free timed out. | 173 174**示例:** 175 176```ts 177 import { insightIntentDriver, insightIntent } from '@kit.AbilityKit'; 178 import { hilog } from '@kit.PerformanceAnalysisKit'; 179 180 async function executeSearchMusicIntentPromise() { 181 let param: insightIntentDriver.ExecuteParam = { 182 bundleName: 'com.ohos.intentexecutedemo', 183 moduleName: 'entry', 184 abilityName: 'EntryAbility', 185 insightIntentName: 'PlayMusic', 186 insightIntentParam: { 187 songName: 'City Of Stars', 188 }, 189 executeMode: insightIntent.ExecuteMode.UI_ABILITY_FOREGROUND, 190 }; 191 192 try { 193 let resultData: insightIntent.ExecuteResult = await insightIntentDriver.execute(param); 194 hilog.info(0x0000, 'testTag', 'execute insight intent return %{public}d', resultData.code); 195 hilog.info(0x0000, 'testTag', 'execute insight intent result %{public}s', JSON.stringify(resultData.result)); 196 } catch (error) { 197 hilog.error(0x0000, 'testTag', 'execute insight intent error caught %{public}s', JSON.stringify(error)); 198 } 199 } 200``` 201