1# WantAgent模块 2 3>**说明:** 4> 5>本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 6> 7> API 9当前为Canary版本,仅供使用,不保证接口可稳定调用。 8 9## 导入模块 10 11```js 12import WantAgent from '@ohos.wantAgent'; 13``` 14 15## WantAgent.getWantAgent 16 17getWantAgent(info: WantAgentInfo, callback: AsyncCallback\<WantAgent\>): void 18 19创建WantAgent(callback形式)。 20 21**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 22 23**参数:** 24 25| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 26| -------- | --- | ---- | -------------------------- | ---- | ----------------------- | 27| info | 是 | 否 | WantAgentInfo | 是 | WantAgent信息。 | 28| callback | 是 | 否 | AsyncCallback\<WantAgent\> | 是 | 创建WantAgent的回调方法。 | 29 30**示例:** 31 32```js 33import WantAgent from '@ohos.wantAgent'; 34import { OperationType, WantAgentFlags } from '@ohos.wantagent'; 35 36//getWantAgent回调 37function getWantAgentCallback(err, data) { 38 console.info("==========================>getWantAgentCallback=======================>"); 39} 40//WantAgentInfo对象 41var wantAgentInfo = { 42 wants: [ 43 { 44 deviceId: "deviceId", 45 bundleName: "com.neu.setResultOnAbilityResultTest1", 46 abilityName: "com.example.test.MainAbility", 47 action: "action1", 48 entities: ["entity1"], 49 type: "MIMETYPE", 50 uri: "key={true,true,false}", 51 parameters: 52 { 53 mykey0: 2222, 54 mykey1: [1, 2, 3], 55 mykey2: "[1, 2, 3]", 56 mykey3: "ssssssssssssssssssssssssss", 57 mykey4: [false, true, false], 58 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 59 mykey6: true, 60 } 61 } 62 ], 63 operationType: OperationType.START_ABILITIES, 64 requestCode: 0, 65 wantAgentFlags:[WantAgentFlags.UPDATE_PRESENT_FLAG] 66} 67 68WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback) 69``` 70 71 72 73## WantAgent.getWantAgent 74 75getWantAgent(info: WantAgentInfo): Promise\<WantAgent\> 76 77创建WantAgent(Promise形式)。 78 79**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 80 81**参数:** 82 83| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 84| ---- | --- | ---- | ------------- | ---- | ------------- | 85| info | 是 | 否 | WantAgentInfo | 是 | WantAgent信息。 | 86 87**返回值:** 88 89| 类型 | 说明 | 90| ----------------------------------------------------------- | ------------------------------------------------------------ | 91| Promise\<WantAgent\> | 以Promise形式返回WantAgent。 | 92 93**示例:** 94 95```js 96import WantAgent from '@ohos.wantAgent'; 97import { OperationType, WantAgentFlags } from '@ohos.wantagent'; 98 99//WantAgentInfo对象 100var wantAgentInfo = { 101 wants: [ 102 { 103 deviceId: "deviceId", 104 bundleName: "com.neu.setResultOnAbilityResultTest1", 105 abilityName: "com.example.test.MainAbility", 106 action: "action1", 107 entities: ["entity1"], 108 type: "MIMETYPE", 109 uri: "key={true,true,false}", 110 parameters: 111 { 112 mykey0: 2222, 113 mykey1: [1, 2, 3], 114 mykey2: "[1, 2, 3]", 115 mykey3: "ssssssssssssssssssssssssss", 116 mykey4: [false, true, false], 117 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 118 mykey6: true, 119 } 120 } 121 ], 122 operationType: OperationType.START_ABILITIES, 123 requestCode: 0, 124 wantAgentFlags:[WantAgentFlags.UPDATE_PRESENT_FLAG] 125} 126 127WantAgent.getWantAgent(wantAgentInfo).then((data) => { 128 console.info("==========================>getWantAgentCallback=======================>"); 129}); 130``` 131 132 133 134## WantAgent.getBundleName 135 136getBundleName(agent: WantAgent, callback: AsyncCallback\<string\>): void 137 138获取WantAgent实例的包名(callback形式)。 139 140**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 141 142**参数:** 143 144| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 145| -------- | --- | ---- | ----------------------- | ---- | --------------------------------- | 146| agent | 是 | 否 | WantAgent | 是 | WantAgent对象。 | 147| callback | 是 | 否 | AsyncCallback\<string\> | 是 | 获取WantAgent实例的包名的回调方法。 | 148 149**示例:** 150 151```js 152import WantAgent from '@ohos.wantAgent'; 153import { OperationType, WantAgentFlags } from '@ohos.wantagent'; 154 155//wantAgent对象 156var wantAgent; 157 158//getWantAgent回调 159function getWantAgentCallback(err, data) { 160 console.info("==========================>getWantAgentCallback=======================>"); 161 if (err.code == 0) { 162 wantAgent = data; 163 } else { 164 console.info('----getWantAgent failed!----'); 165 } 166} 167//WantAgentInfo对象 168var wantAgentInfo = { 169 wants: [ 170 { 171 deviceId: "deviceId", 172 bundleName: "com.neu.setResultOnAbilityResultTest1", 173 abilityName: "com.example.test.MainAbility", 174 action: "action1", 175 entities: ["entity1"], 176 type: "MIMETYPE", 177 uri: "key={true,true,false}", 178 parameters: 179 { 180 mykey0: 2222, 181 mykey1: [1, 2, 3], 182 mykey2: "[1, 2, 3]", 183 mykey3: "ssssssssssssssssssssssssss", 184 mykey4: [false, true, false], 185 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 186 mykey6: true, 187 } 188 } 189 ], 190 operationType: OperationType.START_ABILITIES, 191 requestCode: 0, 192 wantAgentFlags:[WantAgentFlags.UPDATE_PRESENT_FLAG] 193} 194 195WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback) 196 197//getBundleName回调 198function getBundleNameCallback(err, data) { 199 console.info("==========================>getBundleNameCallback=======================>"); 200} 201WantAgent.getBundleName(wantAgent, getBundleNameCallback) 202``` 203 204 205 206## WantAgent.getBundleName 207 208getBundleName(agent: WantAgent): Promise\<string\> 209 210获取WantAgent实例的包名(Promise形式)。 211 212**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 213 214**参数:** 215 216| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 217| ----- | --- | ---- | --------- | ---- | ------------- | 218| agent | 是 | 否 | WantAgent | 是 | WantAgent对象。 | 219 220**返回值:** 221 222| 类型 | 说明 | 223| ----------------------------------------------------------- | ------------------------------------------------------------ | 224| Promise\<string\> | 以Promise形式返回获取WantAgent实例的包名。 | 225 226**示例:** 227 228```js 229import WantAgent from '@ohos.wantAgent'; 230import { OperationType, WantAgentFlags } from '@ohos.wantagent'; 231 232//wantAgent对象 233var wantAgent; 234 235//WantAgentInfo对象 236var wantAgentInfo = { 237 wants: [ 238 { 239 deviceId: "deviceId", 240 bundleName: "com.neu.setResultOnAbilityResultTest1", 241 abilityName: "com.example.test.MainAbility", 242 action: "action1", 243 entities: ["entity1"], 244 type: "MIMETYPE", 245 uri: "key={true,true,false}", 246 parameters: 247 { 248 mykey0: 2222, 249 mykey1: [1, 2, 3], 250 mykey2: "[1, 2, 3]", 251 mykey3: "ssssssssssssssssssssssssss", 252 mykey4: [false, true, false], 253 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 254 mykey6: true, 255 } 256 } 257 ], 258 operationType: OperationType.START_ABILITIES, 259 requestCode: 0, 260 wantAgentFlags:[WantAgentFlags.UPDATE_PRESENT_FLAG] 261} 262 263WantAgent.getWantAgent(wantAgentInfo).then((data) => { 264 console.info("==========================>getWantAgentCallback=======================>"); 265 wantAgent = data; 266}); 267 268WantAgent.getBundleName(wantAgent).then((data) => { 269 console.info("==========================>getBundleNameCallback=======================>"); 270}); 271``` 272 273 274 275## WantAgent.getUid 276 277getUid(agent: WantAgent, callback: AsyncCallback\<number\>): void 278 279获取WantAgent实例的用户ID(callback形式)。 280 281**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 282 283**参数:** 284 285| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 286| -------- | --- | ---- | ----------------------- | ---- | ----------------------------------- | 287| agent | 是 | 否 | WantAgent | 是 | WantAgent对象。 | 288| callback | 是 | 否 | AsyncCallback\<number\> | 是 | 获取WantAgent实例的用户ID的回调方法。 | 289 290**示例:** 291 292```js 293import WantAgent from '@ohos.wantAgent'; 294import { OperationType, WantAgentFlags } from '@ohos.wantagent'; 295 296//wantAgent对象 297var wantAgent; 298 299//getWantAgent回调 300function getWantAgentCallback(err, data) { 301 console.info("==========================>getWantAgentCallback=======================>"); 302 if (err.code == 0) { 303 wantAgent = data; 304 } else { 305 console.info('----getWantAgent failed!----'); 306 } 307} 308//WantAgentInfo对象 309var wantAgentInfo = { 310 wants: [ 311 { 312 deviceId: "deviceId", 313 bundleName: "com.neu.setResultOnAbilityResultTest1", 314 abilityName: "com.example.test.MainAbility", 315 action: "action1", 316 entities: ["entity1"], 317 type: "MIMETYPE", 318 uri: "key={true,true,false}", 319 parameters: 320 { 321 mykey0: 2222, 322 mykey1: [1, 2, 3], 323 mykey2: "[1, 2, 3]", 324 mykey3: "ssssssssssssssssssssssssss", 325 mykey4: [false, true, false], 326 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 327 mykey6: true, 328 } 329 } 330 ], 331 operationType: OperationType.START_ABILITIES, 332 requestCode: 0, 333 wantAgentFlags:[WantAgentFlags.UPDATE_PRESENT_FLAG] 334} 335 336WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback) 337 338//getUid回调 339function getUidCallback(err, data) { 340 console.info("==========================>getUidCallback=======================>"); 341} 342WantAgent.getUid(wantAgent, getUidCallback) 343``` 344 345 346 347## WantAgent.getUid 348 349getUid(agent: WantAgent): Promise\<number\> 350 351获取WantAgent实例的用户ID(Promise形式)。 352 353**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 354 355**参数:** 356 357| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 358| ----- | --- | ---- | --------- | ---- | ------------- | 359| agent | 是 | 否 | WantAgent | 是 | WantAgent对象。 | 360 361**返回值:** 362 363| 类型 | 说明 | 364| ----------------------------------------------------------- | ------------------------------------------------------------ | 365| Promise\<number\> | 以Promise形式返回获取WantAgent实例的用户ID。 | 366 367**示例:** 368 369```js 370import WantAgent from '@ohos.wantAgent'; 371import { OperationType, WantAgentFlags } from '@ohos.wantagent'; 372 373//wantAgent对象 374var wantAgent; 375 376//WantAgentInfo对象 377var wantAgentInfo = { 378 wants: [ 379 { 380 deviceId: "deviceId", 381 bundleName: "com.neu.setResultOnAbilityResultTest1", 382 abilityName: "com.example.test.MainAbility", 383 action: "action1", 384 entities: ["entity1"], 385 type: "MIMETYPE", 386 uri: "key={true,true,false}", 387 parameters: 388 { 389 mykey0: 2222, 390 mykey1: [1, 2, 3], 391 mykey2: "[1, 2, 3]", 392 mykey3: "ssssssssssssssssssssssssss", 393 mykey4: [false, true, false], 394 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 395 mykey6: true, 396 } 397 } 398 ], 399 operationType: OperationType.START_ABILITIES, 400 requestCode: 0, 401 wantAgentFlags:[WantAgentFlags.UPDATE_PRESENT_FLAG] 402} 403 404WantAgent.getWantAgent(wantAgentInfo).then((data) => { 405 console.info("==========================>getWantAgentCallback=======================>"); 406 wantAgent = data; 407}); 408 409WantAgent.getUid(wantAgent).then((data) => { 410 console.info("==========================>getUidCallback=======================>"); 411}); 412``` 413 414 415 416## WantAgent.getWant 417 418getWant(agent: WantAgent, callback: AsyncCallback\<Want\>): void 419 420获取WantAgent对象的want(callback形式)。 421 422**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 423 424**参数:** 425 426| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 427| -------- | --- | ---- | --------------------- | ---- | ------------------------------- | 428| agent | 是 | 否 | WantAgent | 是 | WantAgent对象。 | 429| callback | 是 | 否 | AsyncCallback\<Want\> | 是 | 获取WantAgent对象want的回调方法。 | 430 431**示例:** 432 433```js 434import WantAgent from '@ohos.wantAgent'; 435import { OperationType, WantAgentFlags } from '@ohos.wantagent'; 436 437//wantAgent对象 438var wantAgent; 439 440//getWantAgent回调 441function getWantAgentCallback(err, data) { 442 console.info("==========================>getWantAgentCallback=======================>"); 443 if (err.code == 0) { 444 wantAgent = data; 445 } else { 446 console.info('----getWantAgent failed!----'); 447 } 448} 449//WantAgentInfo对象 450var wantAgentInfo = { 451 wants: [ 452 { 453 deviceId: "deviceId", 454 bundleName: "com.neu.setResultOnAbilityResultTest1", 455 abilityName: "com.example.test.MainAbility", 456 action: "action1", 457 entities: ["entity1"], 458 type: "MIMETYPE", 459 uri: "key={true,true,false}", 460 parameters: 461 { 462 mykey0: 2222, 463 mykey1: [1, 2, 3], 464 mykey2: "[1, 2, 3]", 465 mykey3: "ssssssssssssssssssssssssss", 466 mykey4: [false, true, false], 467 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 468 mykey6: true, 469 } 470 } 471 ], 472 operationType: OperationType.START_ABILITIES, 473 requestCode: 0, 474 wantAgentFlags:[WantAgentFlags.UPDATE_PRESENT_FLAG] 475} 476 477WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback) 478 479//getWant回调 480function getWantCallback(err, data) { 481 console.info("==========================>getWantCallback=======================>"); 482} 483WantAgent.getWant(wantAgent, getWantCallback) 484``` 485 486 487 488## WantAgent.getWant 489 490getWant(agent: WantAgent): Promise\<Want\> 491 492获取WantAgent对象的want(Promise形式)。 493 494**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 495 496**参数:** 497 498| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 499| ----- | --- | ---- | --------- | ---- | ------------- | 500| agent | 是 | 否 | WantAgent | 是 | WantAgent对象。 | 501 502**返回值:** 503 504| 类型 | 说明 | 505| ----------------------------------------------------------- | ------------------------------------------------------------ | 506| Promise\<Want\> | 以Promise形式返回获取WantAgent对象的want。 | 507 508**示例:** 509 510```js 511import WantAgent from '@ohos.wantAgent'; 512import { OperationType, WantAgentFlags } from '@ohos.wantagent'; 513 514//wantAgent对象 515var wantAgent; 516 517//WantAgentInfo对象 518var wantAgentInfo = { 519 wants: [ 520 { 521 deviceId: "deviceId", 522 bundleName: "com.neu.setResultOnAbilityResultTest1", 523 abilityName: "com.example.test.MainAbility", 524 action: "action1", 525 entities: ["entity1"], 526 type: "MIMETYPE", 527 uri: "key={true,true,false}", 528 parameters: 529 { 530 mykey0: 2222, 531 mykey1: [1, 2, 3], 532 mykey2: "[1, 2, 3]", 533 mykey3: "ssssssssssssssssssssssssss", 534 mykey4: [false, true, false], 535 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 536 mykey6: true, 537 } 538 } 539 ], 540 operationType: OperationType.START_ABILITIES, 541 requestCode: 0, 542 wantAgentFlags:[WantAgentFlags.UPDATE_PRESENT_FLAG] 543} 544 545WantAgent.getWantAgent(wantAgentInfo).then((data) => { 546 console.info("==========================>getWantAgentCallback=======================>"); 547 wantAgent = data; 548}); 549 550WantAgent.getWant(wantAgent).then((data) => { 551 console.info("==========================>getWantCallback=======================>"); 552}); 553``` 554 555 556 557## WantAgent.cancel 558 559cancel(agent: WantAgent, callback: AsyncCallback\<void\>): void 560 561取消WantAgent实例(callback形式)。 562 563**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 564 565**参数:** 566 567| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 568| -------- | --- | ---- | --------------------- | ---- | --------------------------- | 569| agent | 是 | 否 | WantAgent | 是 | WantAgent对象。 | 570| callback | 是 | 否 | AsyncCallback\<void\> | 是 | 取消WantAgent实例的回调方法。 | 571 572**示例:** 573 574```js 575import WantAgent from '@ohos.wantAgent'; 576import { OperationType, WantAgentFlags } from '@ohos.wantagent'; 577 578//wantAgent对象 579var wantAgent; 580 581//getWantAgent回调 582function getWantAgentCallback(err, data) { 583 console.info("==========================>getWantAgentCallback=======================>"); 584 if (err.code == 0) { 585 wantAgent = data; 586 } else { 587 console.info('----getWantAgent failed!----'); 588 } 589} 590//WantAgentInfo对象 591var wantAgentInfo = { 592 wants: [ 593 { 594 deviceId: "deviceId", 595 bundleName: "com.neu.setResultOnAbilityResultTest1", 596 abilityName: "com.example.test.MainAbility", 597 action: "action1", 598 entities: ["entity1"], 599 type: "MIMETYPE", 600 uri: "key={true,true,false}", 601 parameters: 602 { 603 mykey0: 2222, 604 mykey1: [1, 2, 3], 605 mykey2: "[1, 2, 3]", 606 mykey3: "ssssssssssssssssssssssssss", 607 mykey4: [false, true, false], 608 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 609 mykey6: true, 610 } 611 } 612 ], 613 operationType: OperationType.START_ABILITIES, 614 requestCode: 0, 615 wantAgentFlags:[WantAgentFlags.UPDATE_PRESENT_FLAG] 616} 617 618WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback) 619 620//cancel回调 621function cancelCallback(err, data) { 622 console.info("==========================>cancelCallback=======================>"); 623} 624WantAgent.cancel(wantAgent, cancelCallback) 625``` 626 627 628 629## WantAgent.cancel 630 631cancel(agent: WantAgent): Promise\<void\> 632 633取消WantAgent实例(Promise形式)。 634 635**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 636 637**参数:** 638 639| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 640| ----- | --- | ---- | --------- | ---- | ------------- | 641| agent | 是 | 否 | WantAgent | 是 | WantAgent对象。 | 642 643**返回值:** 644 645| 类型 | 说明 | 646| --------------- | ------------------------------- | 647| Promise\<void\> | 以Promise形式获取异步返回结果。 | 648 649**示例:** 650 651```js 652import WantAgent from '@ohos.wantAgent'; 653import { OperationType, WantAgentFlags } from '@ohos.wantagent'; 654 655//wantAgent对象 656var wantAgent; 657 658//WantAgentInfo对象 659var wantAgentInfo = { 660 wants: [ 661 { 662 deviceId: "deviceId", 663 bundleName: "com.neu.setResultOnAbilityResultTest1", 664 abilityName: "com.example.test.MainAbility", 665 action: "action1", 666 entities: ["entity1"], 667 type: "MIMETYPE", 668 uri: "key={true,true,false}", 669 parameters: 670 { 671 mykey0: 2222, 672 mykey1: [1, 2, 3], 673 mykey2: "[1, 2, 3]", 674 mykey3: "ssssssssssssssssssssssssss", 675 mykey4: [false, true, false], 676 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 677 mykey6: true, 678 } 679 } 680 ], 681 operationType: OperationType.START_ABILITIES, 682 requestCode: 0, 683 wantAgentFlags:[WantAgentFlags.UPDATE_PRESENT_FLAG] 684} 685 686WantAgent.getWantAgent(wantAgentInfo).then((data) => { 687 console.info("==========================>getWantAgentCallback=======================>"); 688 wantAgent = data; 689}); 690 691WantAgent.cancel(wantAgent).then((data) => { 692 console.info("==========================>cancelCallback=======================>"); 693}); 694``` 695 696 697 698## WantAgent.trigger 699 700trigger(agent: WantAgent, triggerInfo: TriggerInfo, callback?: Callback\<CompleteData\>): void 701 702主动激发WantAgent实例(callback形式)。 703 704**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 705 706**参数:** 707 708| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 709| ----------- | --- | ---- | ----------------------------- | ---- | ------------------------------- | 710| agent | 是 | 否 | WantAgent | 是 | WantAgent对象。 | 711| triggerInfo | 是 | 否 | TriggerInfo | 是 | TriggerInfo对象。 | 712| callback | 是 | 否 | AsyncCallback\<CompleteData\> | 是 | 主动激发WantAgent实例的回调方法。 | 713 714**示例:** 715 716```js 717import WantAgent from '@ohos.wantAgent'; 718import { OperationType, WantAgentFlags } from '@ohos.wantagent'; 719 720//wantAgent对象 721var wantAgent; 722 723//getWantAgent回调 724function getWantAgentCallback(err, data) { 725 console.info("==========================>getWantAgentCallback=======================>"); 726 if (err.code == 0) { 727 wantAgent = data; 728 } else { 729 console.info('----getWantAgent failed!----'); 730 } 731} 732//WantAgentInfo对象 733var wantAgentInfo = { 734 wants: [ 735 { 736 deviceId: "deviceId", 737 bundleName: "com.neu.setResultOnAbilityResultTest1", 738 abilityName: "com.example.test.MainAbility", 739 action: "action1", 740 entities: ["entity1"], 741 type: "MIMETYPE", 742 uri: "key={true,true,false}", 743 parameters: 744 { 745 mykey0: 2222, 746 mykey1: [1, 2, 3], 747 mykey2: "[1, 2, 3]", 748 mykey3: "ssssssssssssssssssssssssss", 749 mykey4: [false, true, false], 750 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 751 mykey6: true, 752 } 753 } 754 ], 755 operationType: OperationType.START_ABILITIES, 756 requestCode: 0, 757 wantAgentFlags:[WantAgentFlags.UPDATE_PRESENT_FLAG] 758} 759 760WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback) 761 762//trigger回调 763function triggerCallback(err, data) { 764 console.info("==========================>triggerCallback=======================>"); 765} 766 767var triggerInfo = { 768 code:0 769} 770WantAgent.trigger(wantAgent, triggerInfo, triggerCallback) 771``` 772 773 774 775## WantAgent.equal 776 777equal(agent: WantAgent, otherAgent: WantAgent, callback: AsyncCallback\<boolean\>): void 778 779判断两个WantAgent实例是否相等(callback形式)。 780 781**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 782 783**参数:** 784 785| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 786| ---------- | --- | ---- | ------------------------ | ---- | --------------------------------------- | 787| agent | 是 | 否 | WantAgent | 是 | WantAgent对象。 | 788| otherAgent | 是 | 否 | WantAgent | 是 | WantAgent对象。 | 789| callback | 是 | 否 | AsyncCallback\<boolean\> | 是 | 判断两个WantAgent实例是否相等的回调方法。 | 790 791**示例:** 792 793```js 794import WantAgent from '@ohos.wantAgent'; 795import { OperationType, WantAgentFlags } from '@ohos.wantagent'; 796 797//wantAgent对象 798var wantAgent1; 799var wantAgent2; 800 801//getWantAgent回调 802function getWantAgentCallback(err, data) { 803 console.info("==========================>getWantAgentCallback=======================>"); 804 if (err.code == 0) { 805 wantAgent1 = data; 806 wantAgent2 = data; 807 } else { 808 console.info('----getWantAgent failed!----'); 809 } 810} 811//WantAgentInfo对象 812var wantAgentInfo = { 813 wants: [ 814 { 815 deviceId: "deviceId", 816 bundleName: "com.neu.setResultOnAbilityResultTest1", 817 abilityName: "com.example.test.MainAbility", 818 action: "action1", 819 entities: ["entity1"], 820 type: "MIMETYPE", 821 uri: "key={true,true,false}", 822 parameters: 823 { 824 mykey0: 2222, 825 mykey1: [1, 2, 3], 826 mykey2: "[1, 2, 3]", 827 mykey3: "ssssssssssssssssssssssssss", 828 mykey4: [false, true, false], 829 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 830 mykey6: true, 831 } 832 } 833 ], 834 operationType: OperationType.START_ABILITIES, 835 requestCode: 0, 836 wantAgentFlags:[WantAgentFlags.UPDATE_PRESENT_FLAG] 837} 838 839WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback) 840 841//equal回调 842function equalCallback(err, data) { 843 console.info("==========================>equalCallback=======================>"); 844} 845WantAgent.equal(wantAgent1, wantAgent2, equalCallback) 846``` 847 848 849 850## WantAgent.equal 851 852equal(agent: WantAgent, otherAgent: WantAgent): Promise\<boolean\> 853 854判断两个WantAgent实例是否相等(Promise形式)。 855 856**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 857 858**参数:** 859 860| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 861| ---------- | --- | ---- | --------- | ---- | ------------- | 862| agent | 是 | 否 | WantAgent | 是 | WantAgent对象。 | 863| otherAgent | 是 | 否 | WantAgent | 是 | WantAgent对象。 | 864 865**返回值:** 866 867| 类型 | 说明 | 868| ----------------------------------------------------------- | ------------------------------------------------------------ | 869| Promise\<boolean\> | 以Promise形式返回获取判断两个WantAgent实例是否相等的结果。 | 870 871**示例:** 872 873```js 874import WantAgent from '@ohos.wantAgent'; 875import { OperationType, WantAgentFlags } from '@ohos.wantagent'; 876 877//wantAgent对象 878var wantAgent1; 879var wantAgent2; 880 881//WantAgentInfo对象 882var wantAgentInfo = { 883 wants: [ 884 { 885 deviceId: "deviceId", 886 bundleName: "com.neu.setResultOnAbilityResultTest1", 887 abilityName: "com.example.test.MainAbility", 888 action: "action1", 889 entities: ["entity1"], 890 type: "MIMETYPE", 891 uri: "key={true,true,false}", 892 parameters: 893 { 894 mykey0: 2222, 895 mykey1: [1, 2, 3], 896 mykey2: "[1, 2, 3]", 897 mykey3: "ssssssssssssssssssssssssss", 898 mykey4: [false, true, false], 899 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 900 mykey6: true, 901 } 902 } 903 ], 904 operationType: OperationType.START_ABILITIES, 905 requestCode: 0, 906 wantAgentFlags:[WantAgentFlags.UPDATE_PRESENT_FLAG] 907} 908 909WantAgent.getWantAgent(wantAgentInfo).then((data) => { 910 console.info("==========================>getWantAgentCallback=======================>"); 911 wantAgent1 = data; 912 wantAgent2 = data; 913}); 914 915WantAgent.equal(wantAgent1, wantAgent2).then((data) => { 916 console.info("==========================>equalCallback=======================>"); 917}); 918``` 919 920 921## WantAgent.getOperationType<sup>9+</sup> 922 923getOperationType(agent: WantAgent, callback: AsyncCallback\<number>): void 924 925获取一个WantAgent的OperationType信息(callback形式)。 926 927**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 928 929**参数:** 930 931| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 932| ---------- | --- | ---- | --------- | ---- | ------------- | 933| agent | 是 | 否 | WantAgent | 是 | WantAgent对象。 | 934| callback | 是 | 否 | AsyncCallback\<number> | 是 | 获取一个WantAgent的OperationType信息的回调方法。 | 935 936**示例**: 937 938```js 939import WantAgent from '@ohos.wantAgent'; 940 941//wantAgent对象 942var wantAgent; 943 944//WantAgentInfo对象 945var wantAgentInfo = { 946 wants: [ 947 { 948 deviceId: "deviceId", 949 bundleName: "com.neu.setResultOnAbilityResultTest1", 950 abilityName: "com.example.test.MainAbility", 951 action: "action1", 952 entities: ["entity1"], 953 type: "MIMETYPE", 954 uri: "key={true,true,false}", 955 parameters: 956 { 957 mykey0: 2222, 958 mykey1: [1, 2, 3], 959 mykey2: "[1, 2, 3]", 960 mykey3: "ssssssssssssssssssssssssss", 961 mykey4: [false, true, false], 962 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 963 mykey6: true, 964 } 965 } 966 ], 967 requestCode: 0, 968 wantAgentFlags:[WantAgentFlags.UPDATE_PRESENT_FLAG] 969} 970 971WantAgent.getWantAgent(wantAgentInfo).then((data) => { 972 console.info("==========================>getWantAgentCallback=======================>"); 973 wantAgent = data; 974}); 975 976WantAgent.getOperationType(wantAgent, (OperationType) => { 977 console.log('----------- getOperationType ----------, OperationType: ' + OperationType); 978}) 979``` 980 981 982## WantAgent.getOperationType<sup>9+</sup> 983 984getOperationType(agent: WantAgent): Promise\<number> 985 986获取一个WantAgent的OperationType信息(Promise形式)。 987 988**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 989 990**参数:** 991 992| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 993| ---------- | --- | ---- | --------- | ---- | ------------- | 994| agent | 是 | 否 | WantAgent | 是 | WantAgent对象。 | 995 996**返回值:** 997 998| 类型 | 说明 | 999| ----------------------------------------------------------- | ------------------------------------------------------------ | 1000| Promise\<number> | 以Promise形式返回获取operationType的结果。 | 1001 1002 1003**示例**: 1004 1005```js 1006import WantAgent from '@ohos.wantAgent'; 1007 1008//wantAgent对象 1009var wantAgent; 1010 1011//WantAgentInfo对象 1012var wantAgentInfo = { 1013 wants: [ 1014 { 1015 deviceId: "deviceId", 1016 bundleName: "com.neu.setResultOnAbilityResultTest1", 1017 abilityName: "com.example.test.MainAbility", 1018 action: "action1", 1019 entities: ["entity1"], 1020 type: "MIMETYPE", 1021 uri: "key={true,true,false}", 1022 parameters: 1023 { 1024 mykey0: 2222, 1025 mykey1: [1, 2, 3], 1026 mykey2: "[1, 2, 3]", 1027 mykey3: "ssssssssssssssssssssssssss", 1028 mykey4: [false, true, false], 1029 mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"], 1030 mykey6: true, 1031 } 1032 } 1033 ], 1034 requestCode: 0, 1035 wantAgentFlags:[WantAgentFlags.UPDATE_PRESENT_FLAG] 1036} 1037 1038WantAgent.getWantAgent(wantAgentInfo).then((data) => { 1039 console.info("==========================>getWantAgentCallback=======================>"); 1040 wantAgent = data; 1041}); 1042 1043WantAgent.getOperationType(wantAgent).then((OperationType) => { 1044 console.log('getOperationType success, OperationType: ' + OperationType); 1045}).catch((err) => { 1046 console.log('getOperationType fail, err: ' + err); 1047}) 1048``` 1049 1050 1051## WantAgentInfo 1052 1053**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core 1054 1055| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 1056| -------------- | --- | ---- | ------------------------------- | ---- | ---------------------- | 1057| wants | 是 | 是 | Array\<Want\> | 是 | 将被执行的动作列表。 | 1058| operationType | 是 | 是 | wantAgent.OperationType | 是 | 动作类型。 | 1059| requestCode | 是 | 是 | number | 是 | 使用者定义的一个私有值。 | 1060| wantAgentFlags | 是 | 是 | Array<wantAgent.WantAgentFlags> | 否 | 动作执行属性。 | 1061| extraInfo | 是 | 是 | {[key: string]: any} | 否 | 额外数据。 | 1062 1063 1064 1065## WantAgentFlags 1066 1067**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core 1068 1069| 名称 | 值 | 说明 | 1070| ------------------- | -------------- | ------------------------------------------------------------ | 1071| ONE_TIME_FLAG | WantAgentFlags | WantAgent仅能使用一次。 | 1072| NO_BUILD_FLAG | WantAgentFlags | 如果描述WantAgent对象不存在,则不创建它,直接返回null。 | 1073| CANCEL_PRESENT_FLAG | WantAgentFlags | 在生成一个新的WantAgent对象前取消已存在的一个WantAgent对象。 | 1074| UPDATE_PRESENT_FLAG | WantAgentFlags | 使用新的WantAgent的额外数据替换已存在的WantAgent中的额外数据。 | 1075| CONSTANT_FLAG | WantAgentFlags | WantAgent是不可变的。 | 1076| REPLACE_ELEMENT | WantAgentFlags | 当前Want中的element属性可被WantAgent.trigger()中Want的element属性取代 | 1077| REPLACE_ACTION | WantAgentFlags | 当前Want中的action属性可被WantAgent.trigger()中Want的action属性取代 | 1078| REPLACE_URI | WantAgentFlags | 当前Want中的uri属性可被WantAgent.trigger()中Want的uri属性取代 | 1079| REPLACE_ENTITIES | WantAgentFlags | 当前Want中的entities属性可被WantAgent.trigger()中Want的entities属性取代 | 1080| REPLACE_BUNDLE | WantAgentFlags | 当前Want中的bundleName属性可被WantAgent.trigger()中Want的bundleName属性取代 | 1081 1082 1083 1084## OperationType 1085 1086**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core 1087 1088| 名称 | 值 | 说明 | 1089| ----------------- | ------------- | ------------------------- | 1090| UNKNOWN_TYPE | OperationType | 不识别的类型。 | 1091| START_ABILITY | OperationType | 开启一个有页面的Ability。 | 1092| START_ABILITIES | OperationType | 开启多个有页面的Ability。 | 1093| START_SERVICE | OperationType | 开启一个无页面的ability。 | 1094| SEND_COMMON_EVENT | OperationType | 发送一个公共事件。 | 1095 1096 1097 1098## CompleteData 1099 1100**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core 1101 1102| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 1103| -------------- | --- | ---- | ------------------------------ | ---- | ---------------------- | 1104| info | 是 | 是 | WantAgent | 是 | 触发的wantAgent。 | 1105| want | 是 | 是 | Want | 是 | 存在的被触发的want。 | 1106| finalCode | 是 | 是 | number | 是 | 触发wantAgent的请求代码。| 1107| finalData | 是 | 是 | string | 否 | 公共事件收集的最终数据。 | 1108| extraInfo | 是 | 是 | {[key: string]: any} | 否 | 额外数据。 | 1109 1110 1111 1112## TriggerInfo 1113 1114**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core 1115 1116| 名称 | 可读 | 可写 | 类型 | 必填 | 描述 | 1117| ---------- | --- | ---- | -------------------- | ---- | ----------- | 1118| code | 是 | 是 | number | 是 | result code。 | 1119| want | 是 | 是 | Want | 否 | Want。 | 1120| permission | 是 | 是 | string | 否 | 权限定义。 | 1121| extraInfo | 是 | 是 | {[key: string]: any} | 否 | 额外数据。 |