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