# @ohos.app.ability.InsightIntentExecutor (意图调用执行基类) 本模块提供意图调用执行基类,开发者通过意图调用执行基类对接端侧意图框架,实现响应意图调用的业务逻辑。开发者接入意图框架时,在意图配置文件中声明对接的意图名称、意图接入方式等,系统根据用户交互和开发者的意图配置文件进行意图调用,触发相应的意图调用执行回调。 > **说明:** > > 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 > > 本模块接口仅可在Stage模型下使用。 ## 导入模块 ```ts import { InsightIntentExecutor } from '@kit.AbilityKit'; ``` ## 属性 **模型约束**:此接口仅可在Stage模型下使用。 **原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core | 名称 | 类型 | 只读 | 可选 | 说明 | | -------- | -------- | -------- | -------- | -------- | | context | [InsightIntentContext](js-apis-app-ability-insightIntentContext.md) | 否 | 否 | 意图调用执行上下文。 | ## InsightIntentExecutor.onExecuteInUIAbilityForegroundMode onExecuteInUIAbilityForegroundMode(name: string, param: Record, pageLoader: window.WindowStage): insightIntent.ExecuteResult | Promise 当意图调用是将UIAbility在前台显示时,触发该回调。支持同步返回和使用Promise异步返回。 **模型约束**:此接口仅可在Stage模型下使用。 **原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 **系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | name | string | 是 | 意图调用名称。 | | param | Record | 是 | 意图调用参数。 | | pageLoader | [window.WindowStage](../apis-arkui/js-apis-window.md#windowstage9) | 是 | 页面加载器。 | **返回值:** | 类型 | 说明 | | -------- | -------- | | [insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult) | 意图调用执行结果。| | Promise<[insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult)> | Promise对象,返回意图调用执行结果。 | **示例:** 直接返回意图调用的结果,示例如下: ```ts import { InsightIntentExecutor, insightIntent } from '@kit.AbilityKit'; import { window } from '@kit.ArkUI'; import { hilog } from '@kit.PerformanceAnalysisKit'; export default class IntentExecutorImpl extends InsightIntentExecutor { onExecuteInUIAbilityForegroundMode(name: string, param: Record, pageLoader: window.WindowStage): insightIntent.ExecuteResult { let result: insightIntent.ExecuteResult; if (name !== 'SupportedInsightIntentName') { hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name); result = { // decided by developer code: 404, result: { message: 'Unsupported insight intent.', } }; return result; } // if developer need load content pageLoader.loadContent('pages/Index', (err, data) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); } else { hilog.info(0x0000, 'testTag', '%{public}s', 'Succeeded in loading the content'); } }); result = { code: 0, result: { message: 'Execute insight intent succeed.', } }; return result; } } ``` 使用Promise异步返回意图调用的结果,示例如下: ```ts import { InsightIntentExecutor, insightIntent } from '@kit.AbilityKit'; import { window } from '@kit.ArkUI'; import { hilog } from '@kit.PerformanceAnalysisKit'; async function executeInsightIntent(param: Record): Promise { return new Promise((resolve, reject) => { let result: insightIntent.ExecuteResult = { code: 0, result: { message: 'Execute insight intent succeed.', } }; resolve(result); }) } export default class IntentExecutorImpl extends InsightIntentExecutor { async onExecuteInUIAbilityForegroundMode(name: string, param: Record, pageLoader: window.WindowStage): Promise { let result: insightIntent.ExecuteResult; if (name !== 'SupportedInsightIntentName') { hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name); result = { // decided by developer code: 404, result: { message: 'Unsupported insight intent.', } }; return result; } result = await executeInsightIntent(param); return result; } } ``` ## InsightIntentExecutor.onExecuteInUIAbilityBackgroundMode onExecuteInUIAbilityBackgroundMode(name: string, param: Record): insightIntent.ExecuteResult | Promise 当意图调用是将UIAbility在后台拉起时,触发该回调。支持同步返回和使用Promise异步返回。 **模型约束**:此接口仅可在Stage模型下使用。 **原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 **系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | name | string | 是 | 意图调用名称。 | | param | Record | 是 | 意图调用参数。 | **返回值:** | 类型 | 说明 | | -------- | -------- | | [insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult) | 意图调用执行结果。| | Promise<[insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult)> | Promise对象,返回意图调用执行结果。 | **示例:** 直接返回意图调用的结果,示例如下: ```ts import { InsightIntentExecutor, insightIntent } from '@kit.AbilityKit'; export default class IntentExecutorImpl extends InsightIntentExecutor { onExecuteInUIAbilityBackgroundMode(name: string, param: Record): insightIntent.ExecuteResult { let result: insightIntent.ExecuteResult = { code: 0, result: { message: 'Execute insight intent succeed.', } }; return result; } } ``` 使用Promise异步返回意图调用的结果,示例如下: ```ts import { InsightIntentExecutor, insightIntent } from '@kit.AbilityKit'; async function executeInsightIntent(param: Record): Promise { return new Promise((resolve, reject) => { let result: insightIntent.ExecuteResult = { code: 0, result: { message: 'Execute insight intent succeed.', } }; resolve(result); }) } export default class IntentExecutorImpl extends InsightIntentExecutor { async onExecuteInUIAbilityBackgroundMode(name: string, param: Record): Promise { let result: insightIntent.ExecuteResult = await executeInsightIntent(param); return result; } } ``` ## InsightIntentExecutor.onExecuteInUIExtensionAbility onExecuteInUIExtensionAbility(name: string, param: Record, pageLoader: UIExtensionContentSession): insightIntent.ExecuteResult | Promise 当意图调用是拉起UIExtensionAbility时,触发该回调。支持同步返回和使用Promise异步返回。 **模型约束**:此接口仅可在Stage模型下使用。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | name | string | 是 | 意图调用名称。 | | param | Record | 是 | 意图调用参数。 | | pageLoader | [UIExtensionContentSession](js-apis-app-ability-uiExtensionContentSession.md) | 是 | 页面加载器。 | **返回值:** | 类型 | 说明 | | -------- | -------- | | [insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult) | 意图调用执行结果。| | Promise<[insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult)> | Promise对象,返回意图调用执行结果。 | **示例:** 直接返回意图调用的结果,示例如下: ```ts import { InsightIntentExecutor, insightIntent, UIExtensionContentSession } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; export default class IntentExecutorImpl extends InsightIntentExecutor { onExecuteInUIExtensionAbility(name: string, param: Record, pageLoader: UIExtensionContentSession): insightIntent.ExecuteResult { let result: insightIntent.ExecuteResult; if (name !== 'SupportedInsightIntentName') { hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name); result = { // decided by developer code: 404, result: { message: 'Unsupported insight intent.', } }; return result; } // if developer need load content pageLoader.loadContent('pages/Index'); result = { code: 0, result: { message: 'Execute insight intent succeed.', } }; return result; } } ``` 使用Promise异步返回意图调用的结果,示例如下: ```ts import { InsightIntentExecutor, insightIntent, UIExtensionContentSession } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; async function executeInsightIntent(param: Record): Promise { return new Promise((resolve, reject) => { let result: insightIntent.ExecuteResult = { code: 0, result: { message: 'Execute insight intent succeed.', } }; resolve(result); }) } export default class IntentExecutorImpl extends InsightIntentExecutor { async onExecuteInUIExtensionAbility(name: string, param: Record, pageLoader: UIExtensionContentSession): Promise { let result: insightIntent.ExecuteResult; if (name !== 'SupportedInsightIntentName') { hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name); result = { // decided by developer code: 404, result: { message: 'Unsupported insight intent.', } }; return result; } result = await executeInsightIntent(param); return result; } } ``` ## InsightIntentExecutor.onExecuteInServiceExtensionAbility onExecuteInServiceExtensionAbility(name: string, param: Record): insightIntent.ExecuteResult | Promise 当意图调用是拉起ServiceExtensionAbility时,触发该回调。支持同步返回和使用Promise异步返回。 **模型约束**:此接口仅可在Stage模型下使用。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | name | string | 是 | 意图调用名称。 | | param | Record | 是 | 意图调用参数。 | **返回值:** | 类型 | 说明 | | -------- | -------- | | [insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult) | 意图调用执行结果。| | Promise<[insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult)> | Promise对象,返回意图调用执行结果。 | **示例:** 直接返回意图调用的结果,示例如下: ```ts import { InsightIntentExecutor, insightIntent } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; export default class IntentExecutorImpl extends InsightIntentExecutor { onExecuteInServiceExtensionAbility(name: string, param: Record): insightIntent.ExecuteResult { let result: insightIntent.ExecuteResult; if (name !== 'SupportedInsightIntentName') { hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name); result = { // decided by developer code: 404, result: { message: 'Unsupported insight intent.', } }; return result; } result = { code: 0, result: { message: 'Execute insight intent succeed.', } }; return result; } } ``` 使用Promise异步返回意图调用的结果,示例如下: ```ts import { InsightIntentExecutor, insightIntent } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; async function executeInsightIntent(param: Record): Promise { return new Promise((resolve, reject) => { let result: insightIntent.ExecuteResult = { code: 0, result: { message: 'Execute insight intent succeed.', } }; resolve(result); }); } export default class IntentExecutorImpl extends InsightIntentExecutor { async onExecuteInServiceExtensionAbility(name: string, param: Record): Promise { let result: insightIntent.ExecuteResult; if (name !== 'SupportedInsightIntentName') { hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name); result = { // decided by developer code: 404, result: { message: 'Unsupported insight intent.', } }; return result; } result = await executeInsightIntent(param); return result; } } ```