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