未验证 提交 c5536f9a 编写于 作者: O openharmony_ci 提交者: Gitee

!4287 WantAgent开发指导资料问题修改

Merge pull request !4287 from zero-cyc/0524
# WantAgent开发指导
### 简介
wantAgent封装了一个指定行为的意图,可以通过wantAgent.trigger接口主动触发,也可以通过与通知绑定被动触发。
### 场景简介
WantAgent封装了一个行为意图信息,可以通过WantAgent.trigger接口主动触发,也可以通过与通知绑定被动触发。
具体的行为包括:启动ability和发布公共事件。
### 场景介绍
通过wantAgent,拉起另外一个ability
具体的行为包括:启动Ability和发布公共事件。
### 接口说明
| 接口名 | 接口描述 |
| ---------------------------------------------------------------------------------------------- | ----------- |
| wantAgent.getWantAgentInfo(info: WantAgentInfo, callback: AsyncCallback\<WantAgent\>) | 创建wantAgent对象(callback) |
| wantAgent.getWantAgent(info: WantAgentInfo): Promise\<WantAgent\>; | 创建wantAgent对象(promise) |
| commonEvent.trigger(agent: WantAgent, triggerInfo: TriggerInfo, callback?: Callback\<CompleteData\>) | 触发wantAgent |
| getWantAgentInfo(info: WantAgentInfo, callback: AsyncCallback\<WantAgent\>) | 以AsyncCallback形式创建WantAgent对象 |
| getWantAgent(info: WantAgentInfo): Promise\<WantAgent\> | 以Promise形式创建WantAgent对象 |
| trigger(agent: WantAgent, triggerInfo: TriggerInfo, callback?: Callback\<CompleteData\>) | 触发WantAgent |
### 开发步骤
1. 导入WantAgent模块。
```javascript
import wantAgent from '@ohos.wantAgent';
```
```
import wantAgent from '@ohos.wantAgent';
```
2. 创建拉起Ability的WantAgentInfo信息,详细的WantAgentInfo信息数据类型及包含的参数请见[WantAgentInfo文档](../reference/apis/js-apis-wantAgent.md#wantagentinfo)介绍。
2. 创建WantAgentInfo信息,详细的WantAgentInfo信息数据类型及包含的参数请见[WantAgentInfo文档](../reference/apis/js-apis-wantAgent.md#WantAgentInfo)介绍。
```
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_ABILITY,
requestCode: 0,
wantAgentFlags:[WantAgentFlags.CONSTANT_FLAG]
}
```
```javascript
private wantAgentObj = null //用于保存创建成功的wantAgent对象,后续使用其完成触发的动作
3. 创建发布公共事件的WantAgentInfo信息
//wantAgentInfo
var wantAgentInfo = {
wants: [
{
deviceId: "",
bundleName: "com.example.test",
abilityName: "com.example.test.MainAbility",
action: "",
entities: [],
uri: "",
parameters: {}
}
],
operationType: OperationType.START_ABILITY,
requestCode: 0,
wantAgentFlags:[WantAgentFlags.CONSTANT_FLAG]
}
```
```
private wantAgentObj = null //用于保存创建成功的WantAgent对象,后续使用其完成触发的动作。
//wantAgentInfo
var wantAgentInfo = {
wants: [
{
action: "event_name", // 设置事件名
parameters: {}
}
],
operationType: OperationType.SEND_COMMON_EVENT,
requestCode: 0,
wantAgentFlags:[WantAgentFlags.CONSTANT_FLAG]
}
```
3. 创建wantAgent,保存返回的wantAgent对象wantAgentObj,用于执行后续触发操作。
4. 创建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
}
})
```
```
//创建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
5. 触发WantAgent
```
//触发wantAgent
var triggerInfo = {
code:0
}
wantAgent.trigger(wantAgentObj, triggerInfo, (completeData) => {
console.log("[WantAgent]getWantAgent success, completeData: ", + JSON.stringify(completeData))
})
```
\ No newline at end of file
```
//触发WantAgent
var triggerInfo = {
code:0
}
wantAgent.trigger(wantAgentObj, triggerInfo, (completeData) => {
console.log("[WantAgent]getWantAgent success, completeData: ", + JSON.stringify(completeData))
})
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册