From 589aebedf7917c9c26fadb1f3825d928c38335e6 Mon Sep 17 00:00:00 2001 From: zero-cyc Date: Wed, 23 Mar 2022 17:37:22 +0800 Subject: [PATCH] chenlien@huawei.com Signed-off-by: zero-cyc Change-Id: I1808646f80f615d9d010ca8e52c14672faf31e99 --- zh-cn/application-dev/ability/wantagent.md | 77 +++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/zh-cn/application-dev/ability/wantagent.md b/zh-cn/application-dev/ability/wantagent.md index 2e8511fd53..df8e79d046 100644 --- a/zh-cn/application-dev/ability/wantagent.md +++ b/zh-cn/application-dev/ability/wantagent.md @@ -1 +1,76 @@ -# WantAgent开发指导 \ No newline at end of file +# WantAgent开发指导 +### 简介 +wantAgent封装了一个指定行为的意图,可以通过wantAgent.trigger接口主动触发,也可以通过与通知绑定被动触发。 + +具体的行为包括:启动ability和发布公共事件。 + +### 场景介绍 +通过wantAgent,拉起另外一个ability + +### 接口说明 +| 接口名 | 接口描述 | +| ---------------------------------------------------------------------------------------------- | ----------- | +| wantAgent.getWantAgentInfo(info: WantAgentInfo, callback: AsyncCallback\) | 创建wantAgent对象(callback) | +| wantAgent.getWantAgent(info: WantAgentInfo): Promise\; | 创建wantAgent对象(promise) | +| commonEvent.trigger(agent: WantAgent, triggerInfo: TriggerInfo, callback?: Callback\) | 触发wantAgent | + +### 开发步骤 +1. 导入WantAgent模块。 + +```javascript +import wantAgent from '@ohos.wantAgent'; +``` + +2. 创建WantAgentInfo信息,详细的WantAgentInfo信息数据类型及包含的参数请见[WantAgentInfo文档](../reference/apis/js-apis-wantAgent.md#WantAgentInfo)介绍。 + +```javascript +private wantAgentObj = null //用于保存创建成功的wantAgent对象,后续使用其完成触发的动作 + +//wantAgentInfo +var wantAgentInfo = { + wants: [ + { + deviceId: "", + bundleName: "com.example.test", + abilityName: "com.example.test.MainAbility", + action: "", + entities: [], + uri: "", + parameters: {} + } + ], + operationType: OperationType.START_ABILITIE, + requestCode: 0, + wantAgentFlags:[WantAgentFlags.CONSTANT_FLAG] +} +``` + +3. 创建wantAgent,保存返回的wantAgent对象wantAgentObj,用于执行后续触发操作。 + +```javascript +//创建wantAgent +wantAgent.getWantAgent(wantAgentInfo, (err, wantAgentObj) => { + if (err.code) { + console.error("[WantAgent]getWantAgent err=" + JSON.stringify(err)) + } else { + console.log("[WantAgent]getWantAgent success") + this.wantAgentObj = wantAgentObj + } +}) +``` + +4. 触发wantAgent + +``` +//触发wantAgent +var triggerInfo = { + code:0 +} +wantAgent.trigger(wantAgentObj, triggerInfo, (err, completeData) => { + if (err.code) { + console.error("[WantAgent]trigger err=" + JSON.stringify(err)) + } else { + console.log("[WantAgent]getWantAgent success, completeData: ", + JSON.stringify(completeData)) + } +}) +``` \ No newline at end of file -- GitLab