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