1# ServiceExtensionContext (System API) 2 3The ServiceExtensionContext module, inherited from **ExtensionContext**, provides context for the ServiceExtensionAbility. 4 5You can use the APIs of this module to start, terminate, connect, and disconnect an ability. 6 7> **NOTE** 8> 9> - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 10> - The APIs of this module can be used only in the stage model. 11> - The APIs of this module must be used in the main thread, but not in sub-threads such as Worker and TaskPool. 12> - The APIs provided by this module are system APIs. 13 14## Modules to Import 15 16```ts 17import common from '@ohos.app.ability.common'; 18``` 19 20## Usage 21 22Before using the **ServiceExtensionContext** module, you must define a child class that inherits from **ServiceExtensionAbility**. 23 24**Example** 25 26```ts 27import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 28import rpc from '@ohos.rpc'; 29 30let commRemote: rpc.IRemoteObject; // Release the instance when the connection is disconnected. 31class EntryAbility extends ServiceExtensionAbility { 32 onCreate() { 33 let context = this.context; // Obtain a ServiceExtensionContext instance. 34 } 35} 36``` 37 38## ServiceExtensionContext.startAbility 39 40startAbility(want: Want, callback: AsyncCallback<void>): void; 41 42Starts an ability. This API uses an asynchronous callback to return the result. 43 44**System capability**: SystemCapability.Ability.AbilityRuntime.Core 45 46**System API**: This is a system API and cannot be called by third-party applications. 47 48**Parameters** 49 50| Name| Type| Mandatory| Description| 51| -------- | -------- | -------- | -------- | 52| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability, such as the ability name and bundle name.| 53| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 54 55**Error codes** 56 57| ID| Error Message| 58| ------- | -------------------------------- | 59| 16000001 | The specified ability does not exist. | 60| 16000002 | Incorrect ability type. | 61| 16000004 | Can not start invisible component. | 62| 16000005 | The specified process does not have the permission. | 63| 16000006 | Cross-user operations are not allowed. | 64| 16000008 | The crowdtesting application expires. | 65| 16000009 | An ability cannot be started or stopped in Wukong mode. | 66| 16000010 | The call with the continuation flag is forbidden. | 67| 16000011 | The context does not exist. | 68| 16000012 | The application is controlled. | 69| 16000013 | The application is controlled by EDM. | 70| 16000050 | Internal error. | 71| 16000053 | The ability is not on the top of the UI. | 72| 16000055 | Installation-free timed out. | 73| 16200001 | The caller has been released. | 74 75For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 76 77**Example** 78 79```ts 80import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 81import Want from '@ohos.app.ability.Want'; 82import { BusinessError } from '@ohos.base'; 83 84class EntryAbility extends ServiceExtensionAbility { 85 onCreate() { 86 let want: Want = { 87 bundleName: 'com.example.myapp', 88 abilityName: 'MyAbility' 89 }; 90 91 try { 92 this.context.startAbility(want, (error: BusinessError) => { 93 if (error.code) { 94 // Process service logic errors. 95 console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 96 return; 97 } 98 // Carry out normal service processing. 99 console.log('startAbility succeed'); 100 }); 101 } catch (paramError) { 102 // Process input parameter errors. 103 console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`); 104 } 105 } 106} 107``` 108 109## ServiceExtensionContext.startAbility 110 111startAbility(want: Want, options?: StartOptions): Promise\<void>; 112 113Starts an ability. This API uses a promise to return the result. 114 115**System capability**: SystemCapability.Ability.AbilityRuntime.Core 116 117**System API**: This is a system API and cannot be called by third-party applications. 118 119**Parameters** 120 121| Name| Type| Mandatory| Description| 122| -------- | -------- | -------- | -------- | 123| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability, such as the ability name and bundle name.| 124| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 125 126**Return value** 127 128| Type| Description| 129| -------- | -------- | 130| Promise<void> | Promise used to return the result.| 131 132**Error codes** 133 134| ID| Error Message| 135| ------- | -------------------------------- | 136| 16000001 | The specified ability does not exist. | 137| 16000002 | Incorrect ability type. | 138| 16000004 | Can not start invisible component. | 139| 16000005 | The specified process does not have the permission. | 140| 16000006 | Cross-user operations are not allowed. | 141| 16000008 | The crowdtesting application expires. | 142| 16000009 | An ability cannot be started or stopped in Wukong mode. | 143| 16000010 | The call with the continuation flag is forbidden. | 144| 16000011 | The context does not exist. | 145| 16000012 | The application is controlled. | 146| 16000013 | The application is controlled by EDM. | 147| 16000050 | Internal error. | 148| 16000053 | The ability is not on the top of the UI. | 149| 16000055 | Installation-free timed out. | 150| 16200001 | The caller has been released. | 151 152For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 153 154**Example** 155 156```ts 157import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 158import Want from '@ohos.app.ability.Want'; 159import StartOptions from '@ohos.app.ability.StartOptions'; 160import { BusinessError } from '@ohos.base'; 161 162class EntryAbility extends ServiceExtensionAbility { 163 onCreate() { 164 let want: Want = { 165 bundleName: 'com.example.myapp', 166 abilityName: 'MyAbility' 167 }; 168 let options: StartOptions = { 169 windowMode: 0, 170 }; 171 172 try { 173 this.context.startAbility(want, options) 174 .then((data: void) => { 175 // Carry out normal service processing. 176 console.log('startAbility succeed'); 177 }) 178 .catch((error: BusinessError) => { 179 // Process service logic errors. 180 console.error('startAbility failed, error.code: ${error.code}, error.message: ${error.message}'); 181 }); 182 } catch (paramError) { 183 // Process input parameter errors. 184 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 185 } 186 } 187} 188``` 189 190## ServiceExtensionContext.startAbility 191 192startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void 193 194Starts an ability with the start options specified. This API uses an asynchronous callback to return the result. 195 196**System capability**: SystemCapability.Ability.AbilityRuntime.Core 197 198**System API**: This is a system API and cannot be called by third-party applications. 199 200**Parameters** 201 202| Name| Type| Mandatory| Description| 203| -------- | -------- | -------- | -------- | 204| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 205| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| 206| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 207 208**Error codes** 209 210| ID| Error Message| 211| ------- | -------------------------------- | 212| 16000001 | The specified ability does not exist. | 213| 16000002 | Incorrect ability type. | 214| 16000004 | Can not start invisible component. | 215| 16000005 | The specified process does not have the permission. | 216| 16000006 | Cross-user operations are not allowed. | 217| 16000008 | The crowdtesting application expires. | 218| 16000009 | An ability cannot be started or stopped in Wukong mode. | 219| 16000010 | The call with the continuation flag is forbidden. | 220| 16000011 | The context does not exist. | 221| 16000012 | The application is controlled. | 222| 16000013 | The application is controlled by EDM. | 223| 16000050 | Internal error. | 224| 16000053 | The ability is not on the top of the UI. | 225| 16000055 | Installation-free timed out. | 226| 16200001 | The caller has been released. | 227 228For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 229 230**Example** 231 232```ts 233import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 234import Want from '@ohos.app.ability.Want'; 235import StartOptions from '@ohos.app.ability.StartOptions'; 236import { BusinessError } from '@ohos.base'; 237 238class EntryAbility extends ServiceExtensionAbility { 239 onCreate() { 240 let want: Want = { 241 deviceId: '', 242 bundleName: 'com.example.myapplication', 243 abilityName: 'EntryAbility' 244 }; 245 let options: StartOptions = { 246 windowMode: 0 247 }; 248 249 try { 250 this.context.startAbility(want, options, (error: BusinessError) => { 251 if (error.code) { 252 // Process service logic errors. 253 console.error('startAbility failed, error.code: ${error.code}, error.message: ${error.message}'); 254 return; 255 } 256 // Carry out normal service processing. 257 console.log('startAbility succeed'); 258 }); 259 } catch (paramError) { 260 // Process input parameter errors. 261 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 262 } 263 } 264} 265``` 266 267## ServiceExtensionContext.startAbilityWithAccount 268 269startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void; 270 271Starts an ability with the account ID specified. This API uses an asynchronous callback to return the result. 272 273Observe the following when using this API: 274 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 275 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 276 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 277 278**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 279 280**System capability**: SystemCapability.Ability.AbilityRuntime.Core 281 282**System API**: This is a system API and cannot be called by third-party applications. 283 284**Parameters** 285 286| Name| Type| Mandatory| Description| 287| -------- | -------- | -------- | -------- | 288| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 289| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated).| 290| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.| 291 292**Error codes** 293 294| ID| Error Message| 295| ------- | -------------------------------- | 296| 16000001 | The specified ability does not exist. | 297| 16000002 | Incorrect ability type. | 298| 16000004 | Can not start invisible component. | 299| 16000005 | The specified process does not have the permission. | 300| 16000006 | Cross-user operations are not allowed. | 301| 16000008 | The crowdtesting application expires. | 302| 16000009 | An ability cannot be started or stopped in Wukong mode. | 303| 16000010 | The call with the continuation flag is forbidden. | 304| 16000011 | The context does not exist. | 305| 16000012 | The application is controlled. | 306| 16000013 | The application is controlled by EDM. | 307| 16000050 | Internal error. | 308| 16000053 | The ability is not on the top of the UI. | 309| 16000055 | Installation-free timed out. | 310| 16200001 | The caller has been released. | 311 312For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 313 314**Example** 315 316```ts 317import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 318import Want from '@ohos.app.ability.Want'; 319import { BusinessError } from '@ohos.base'; 320 321class EntryAbility extends ServiceExtensionAbility { 322 onCreate() { 323 let want: Want = { 324 deviceId: '', 325 bundleName: 'com.example.myapplication', 326 abilityName: 'EntryAbility' 327 }; 328 let accountId = 100; 329 330 try { 331 this.context.startAbilityWithAccount(want, accountId, (error: BusinessError) => { 332 if (error.code) { 333 // Process service logic errors. 334 console.error('startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}'); 335 return; 336 } 337 // Carry out normal service processing. 338 console.log('startAbilityWithAccount succeed'); 339 }); 340 } catch (paramError) { 341 // Process input parameter errors. 342 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 343 } 344 } 345} 346``` 347 348## ServiceExtensionContext.startAbilityWithAccount 349 350startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void; 351 352Starts an ability with the account ID and start options specified. This API uses an asynchronous callback to return the result. 353 354Observe the following when using this API: 355 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 356 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 357 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 358 359**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 360 361**System capability**: SystemCapability.Ability.AbilityRuntime.Core 362 363**System API**: This is a system API and cannot be called by third-party applications. 364 365**Parameters** 366 367| Name| Type| Mandatory| Description| 368| -------- | -------- | -------- | -------- | 369| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 370| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated).| 371| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| 372| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.| 373 374**Error codes** 375 376| ID| Error Message| 377| ------- | -------------------------------- | 378| 16000001 | The specified ability does not exist. | 379| 16000002 | Incorrect ability type. | 380| 16000004 | Can not start invisible component. | 381| 16000005 | The specified process does not have the permission. | 382| 16000006 | Cross-user operations are not allowed. | 383| 16000008 | The crowdtesting application expires. | 384| 16000009 | An ability cannot be started or stopped in Wukong mode. | 385| 16000010 | The call with the continuation flag is forbidden. | 386| 16000011 | The context does not exist. | 387| 16000012 | The application is controlled. | 388| 16000013 | The application is controlled by EDM. | 389| 16000050 | Internal error. | 390| 16000053 | The ability is not on the top of the UI. | 391| 16000055 | Installation-free timed out. | 392| 16200001 | The caller has been released. | 393 394For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 395 396**Example** 397 398```ts 399import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 400import Want from '@ohos.app.ability.Want'; 401import StartOptions from '@ohos.app.ability.StartOptions'; 402import { BusinessError } from '@ohos.base'; 403 404class EntryAbility extends ServiceExtensionAbility { 405 onCreate() { 406 let want: Want = { 407 deviceId: '', 408 bundleName: 'com.example.myapplication', 409 abilityName: 'EntryAbility' 410 }; 411 let accountId = 100; 412 let options: StartOptions = { 413 windowMode: 0 414 }; 415 416 try { 417 this.context.startAbilityWithAccount(want, accountId, options, (error: BusinessError) => { 418 if (error.code) { 419 // Process service logic errors. 420 console.error('startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}'); 421 return; 422 } 423 // Carry out normal service processing. 424 console.log('startAbilityWithAccount succeed'); 425 }); 426 } catch (paramError) { 427 // Process input parameter errors. 428 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 429 } 430 } 431} 432``` 433 434 435## ServiceExtensionContext.startAbilityWithAccount 436 437startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void>; 438 439Starts an ability with the account ID specified. This API uses a promise to return the result. 440 441Observe the following when using this API: 442 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 443 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 444 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 445 446**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 447 448**System capability**: SystemCapability.Ability.AbilityRuntime.Core 449 450**System API**: This is a system API and cannot be called by third-party applications. 451 452**Parameters** 453 454| Name| Type| Mandatory| Description| 455| -------- | -------- | -------- | -------- | 456| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 457| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1).| 458| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 459 460**Return value** 461 462| Type| Description| 463| -------- | -------- | 464| Promise<void> | Promise used to return the result.| 465 466**Error codes** 467 468| ID| Error Message| 469| ------- | -------------------------------- | 470| 16000001 | The specified ability does not exist. | 471| 16000002 | Incorrect ability type. | 472| 16000004 | Can not start invisible component. | 473| 16000005 | The specified process does not have the permission. | 474| 16000006 | Cross-user operations are not allowed. | 475| 16000008 | The crowdtesting application expires. | 476| 16000009 | An ability cannot be started or stopped in Wukong mode. | 477| 16000010 | The call with the continuation flag is forbidden. | 478| 16000011 | The context does not exist. | 479| 16000012 | The application is controlled. | 480| 16000013 | The application is controlled by EDM. | 481| 16000050 | Internal error. | 482| 16000053 | The ability is not on the top of the UI. | 483| 16000055 | Installation-free timed out. | 484| 16200001 | The caller has been released. | 485 486For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 487 488**Example** 489 490```ts 491import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 492import Want from '@ohos.app.ability.Want'; 493import StartOptions from '@ohos.app.ability.StartOptions'; 494import { BusinessError } from '@ohos.base'; 495 496class EntryAbility extends ServiceExtensionAbility { 497 onCreate() { 498 let want: Want = { 499 deviceId: '', 500 bundleName: 'com.example.myapplication', 501 abilityName: 'EntryAbility' 502 }; 503 let accountId = 100; 504 let options: StartOptions = { 505 windowMode: 0 506 }; 507 508 try { 509 this.context.startAbilityWithAccount(want, accountId, options) 510 .then((data: void) => { 511 // Carry out normal service processing. 512 console.log('startAbilityWithAccount succeed'); 513 }) 514 .catch((error: BusinessError) => { 515 // Process service logic errors. 516 console.error('startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}'); 517 }); 518 } catch (paramError) { 519 // Process input parameter errors. 520 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 521 } 522 } 523} 524``` 525 526## ServiceExtensionContext.startServiceExtensionAbility 527 528startServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void; 529 530Starts a new ServiceExtensionAbility. This API uses an asynchronous callback to return the result. 531 532**System capability**: SystemCapability.Ability.AbilityRuntime.Core 533 534**System API**: This is a system API and cannot be called by third-party applications. 535 536**Parameters** 537 538| Name| Type| Mandatory| Description| 539| -------- | -------- | -------- | -------- | 540| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 541| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.| 542 543**Error codes** 544 545| ID| Error Message| 546| ------- | -------------------------------- | 547| 16000001 | The specified ability does not exist. | 548| 16000002 | Incorrect ability type. | 549| 16000004 | Can not start invisible component. | 550| 16000005 | The specified process does not have the permission. | 551| 16000006 | Cross-user operations are not allowed. | 552| 16000008 | The crowdtesting application expires. | 553| 16000011 | The context does not exist. | 554| 16000012 | The application is controlled. | 555| 16000013 | The application is controlled by EDM. | 556| 16000050 | Internal error. | 557| 16200001 | The caller has been released. | 558 559For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 560 561**Example** 562 563```ts 564import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 565import Want from '@ohos.app.ability.Want'; 566import { BusinessError } from '@ohos.base'; 567 568class EntryAbility extends ServiceExtensionAbility { 569 onCreate() { 570 let want: Want = { 571 deviceId: '', 572 bundleName: 'com.example.myapplication', 573 abilityName: 'EntryAbility' 574 }; 575 576 try { 577 this.context.startServiceExtensionAbility(want, (error: BusinessError) => { 578 if (error.code) { 579 // Process service logic errors. 580 console.error('startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}'); 581 return; 582 } 583 // Carry out normal service processing. 584 console.log('startServiceExtensionAbility succeed'); 585 }); 586 } catch (paramError) { 587 // Process input parameter errors. 588 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 589 } 590 } 591} 592``` 593 594## ServiceExtensionContext.startServiceExtensionAbility 595 596startServiceExtensionAbility(want: Want): Promise\<void>; 597 598Starts a new ServiceExtensionAbility. This API uses a promise to return the result. 599 600**System capability**: SystemCapability.Ability.AbilityRuntime.Core 601 602**System API**: This is a system API and cannot be called by third-party applications. 603 604**Parameters** 605 606| Name| Type| Mandatory| Description| 607| -------- | -------- | -------- | -------- | 608| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 609 610**Return value** 611 612| Type| Description| 613| -------- | -------- | 614| Promise<void> | Promise used to return the result.| 615 616**Error codes** 617 618| ID| Error Message| 619| ------- | -------------------------------- | 620| 16000001 | The specified ability does not exist. | 621| 16000002 | Incorrect ability type. | 622| 16000004 | Can not start invisible component. | 623| 16000005 | The specified process does not have the permission. | 624| 16000006 | Cross-user operations are not allowed. | 625| 16000008 | The crowdtesting application expires. | 626| 16000011 | The context does not exist. | 627| 16000012 | The application is controlled. | 628| 16000013 | The application is controlled by EDM. | 629| 16000050 | Internal error. | 630| 16200001 | The caller has been released. | 631 632For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 633 634**Example** 635 636```ts 637import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 638import Want from '@ohos.app.ability.Want'; 639import { BusinessError } from '@ohos.base'; 640 641class EntryAbility extends ServiceExtensionAbility { 642 onCreate() { 643 let want: Want = { 644 deviceId: '', 645 bundleName: 'com.example.myapplication', 646 abilityName: 'EntryAbility' 647 }; 648 649 try { 650 this.context.startServiceExtensionAbility(want) 651 .then((data: void) => { 652 // Carry out normal service processing. 653 console.log('startServiceExtensionAbility succeed'); 654 }) 655 .catch((error: BusinessError) => { 656 // Process service logic errors. 657 console.error('startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}'); 658 }); 659 } catch (paramError) { 660 // Process input parameter errors. 661 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 662 } 663 } 664} 665``` 666 667## ServiceExtensionContext.startServiceExtensionAbilityWithAccount 668 669startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void; 670 671Starts a new ServiceExtensionAbility with the account ID specified. This API uses an asynchronous callback to return the result. 672 673> **NOTE** 674> 675> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user. 676 677**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 678 679**System capability**: SystemCapability.Ability.AbilityRuntime.Core 680 681**System API**: This is a system API and cannot be called by third-party applications. 682 683**Parameters** 684 685| Name| Type| Mandatory| Description| 686| -------- | -------- | -------- | -------- | 687| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 688| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated).| 689| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.| 690 691**Error codes** 692 693| ID| Error Message| 694| ------- | -------------------------------- | 695| 16000001 | The specified ability does not exist. | 696| 16000002 | Incorrect ability type. | 697| 16000004 | Can not start invisible component. | 698| 16000005 | The specified process does not have the permission. | 699| 16000006 | Cross-user operations are not allowed. | 700| 16000008 | The crowdtesting application expires. | 701| 16000011 | The context does not exist. | 702| 16000012 | The application is controlled. | 703| 16000013 | The application is controlled by EDM. | 704| 16000050 | Internal error. | 705| 16200001 | The caller has been released. | 706 707For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 708 709 710**Example** 711 712```ts 713import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 714import Want from '@ohos.app.ability.Want'; 715import { BusinessError } from '@ohos.base'; 716 717class EntryAbility extends ServiceExtensionAbility { 718 onCreate() { 719 let want: Want = { 720 deviceId: '', 721 bundleName: 'com.example.myapplication', 722 abilityName: 'EntryAbility' 723 }; 724 let accountId = 100; 725 726 try { 727 this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error: BusinessError) => { 728 if (error.code) { 729 // Process service logic errors. 730 console.error('startServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}'); 731 return; 732 } 733 // Carry out normal service processing. 734 console.log('startServiceExtensionAbilityWithAccount succeed'); 735 }); 736 } catch (paramError) { 737 // Process input parameter errors. 738 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 739 } 740 } 741} 742``` 743 744## ServiceExtensionContext.startServiceExtensionAbilityWithAccount 745 746startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>; 747 748Starts a new ServiceExtensionAbility with the account ID specified. This API uses a promise to return the result. 749 750> **NOTE** 751> 752> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user. 753 754**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 755 756**System capability**: SystemCapability.Ability.AbilityRuntime.Core 757 758**System API**: This is a system API and cannot be called by third-party applications. 759 760**Parameters** 761 762| Name| Type| Mandatory| Description| 763| -------- | -------- | -------- | -------- | 764| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 765| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1).| 766 767**Return value** 768 769| Type| Description| 770| -------- | -------- | 771| Promise<void> | Promise used to return the result.| 772 773**Error codes** 774 775| ID| Error Message| 776| ------- | -------------------------------- | 777| 16000001 | The specified ability does not exist. | 778| 16000002 | Incorrect ability type. | 779| 16000004 | Can not start invisible component. | 780| 16000005 | The specified process does not have the permission. | 781| 16000006 | Cross-user operations are not allowed. | 782| 16000008 | The crowdtesting application expires. | 783| 16000011 | The context does not exist. | 784| 16000012 | The application is controlled. | 785| 16000013 | The application is controlled by EDM. | 786| 16000050 | Internal error. | 787| 16200001 | The caller has been released. | 788 789For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 790 791**Example** 792 793```ts 794import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 795import Want from '@ohos.app.ability.Want'; 796import { BusinessError } from '@ohos.base'; 797 798class EntryAbility extends ServiceExtensionAbility { 799 onCreate() { 800 let want: Want = { 801 deviceId: '', 802 bundleName: 'com.example.myapplication', 803 abilityName: 'EntryAbility' 804 }; 805 let accountId = 100; 806 807 try { 808 this.context.startServiceExtensionAbilityWithAccount(want, accountId) 809 .then((data: void) => { 810 // Carry out normal service processing. 811 console.log('startServiceExtensionAbilityWithAccount succeed'); 812 }) 813 .catch((error: BusinessError) => { 814 // Process service logic errors. 815 console.error('startServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}'); 816 }); 817 } catch (paramError) { 818 // Process input parameter errors. 819 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 820 } 821 } 822} 823``` 824 825## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup> 826 827startAbilityAsCaller(want: Want, callback: AsyncCallback\<void>): void; 828 829Starts an ability with the caller information specified. The caller information is carried in **want** and identified at the system service layer. The ability can obtain the caller information from the **want** parameter in the **onCreate** lifecycle callback. When this API is used to start an ability, the caller information carried in **want** is not overwritten by the current application information. The system service layer can obtain the initial caller information. This API uses an asynchronous callback to return the result. 830 831Observe the following when using this API: 832 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 833 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 834 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 835 836**System capability**: SystemCapability.Ability.AbilityRuntime.Core 837 838**System API**: This is a system API and cannot be called by third-party applications. 839 840**Parameters** 841 842| Name| Type| Mandatory| Description| 843| -------- | -------- | -------- | -------- | 844| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 845| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the ability is started, **err** is **undefined**; otherwise, **err** is an error object.| 846 847**Error codes** 848 849| ID| Error Message| 850| ------- | -------------------------------- | 851| 16000001 | The specified ability does not exist. | 852| 16000002 | Incorrect ability type. | 853| 16000004 | Can not start invisible component. | 854| 16000005 | The specified process does not have the permission. | 855| 16000006 | Cross-user operations are not allowed. | 856| 16000008 | The crowdtesting application expires. | 857| 16000009 | An ability cannot be started or stopped in Wukong mode. | 858| 16000010 | The call with the continuation flag is forbidden. | 859| 16000011 | The context does not exist. | 860| 16000012 | The application is controlled. | 861| 16000013 | The application is controlled by EDM. | 862| 16000050 | Internal error. | 863| 16000053 | The ability is not on the top of the UI. | 864| 16000055 | Installation-free timed out. | 865| 16200001 | The caller has been released. | 866 867For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 868 869**Example** 870 871```ts 872import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 873import Want from '@ohos.app.ability.Want'; 874 875export default class EntryAbility extends ServiceExtensionAbility { 876 onCreate(want: Want) { 877 // want contains the information about the caller who starts the application. 878 let localWant: Want = want; 879 localWant.bundleName = 'com.example.demo'; 880 localWant.moduleName = 'entry'; 881 localWant.abilityName = 'TestAbility'; 882 883 // Start a new ability using the caller information. 884 this.context.startAbilityAsCaller(localWant, (err) => { 885 if (err && err.code != 0) { 886 console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err)); 887 } else { 888 console.log('startAbilityAsCaller success.'); 889 } 890 }) 891 } 892} 893 894``` 895 896## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup> 897 898startAbilityAsCaller(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void; 899 900Starts an ability with the caller information and start options specified. The caller information is carried in **want** and identified at the system service layer. The ability can obtain the caller information from the **want** parameter in the **onCreate** lifecycle callback. When this API is used to start an ability, the caller information carried in **want** is not overwritten by the current application information. The system service layer can obtain the initial caller information. This API uses an asynchronous callback to return the result. 901 902Observe the following when using this API: 903 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 904 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 905 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 906 907**System capability**: SystemCapability.Ability.AbilityRuntime.Core 908 909**System API**: This is a system API and cannot be called by third-party applications. 910 911**Parameters** 912 913| Name| Type| Mandatory| Description| 914| -------- | -------- | -------- | -------- | 915| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 916| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| 917| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the ability is started, **err** is **undefined**; otherwise, **err** is an error object.| 918 919**Error codes** 920 921| ID| Error Message| 922| ------- | -------------------------------- | 923| 16000001 | The specified ability does not exist. | 924| 16000004 | Can not start invisible component. | 925| 16000005 | The specified process does not have the permission. | 926| 16000006 | Cross-user operations are not allowed. | 927| 16000008 | The crowdtesting application expires. | 928| 16000009 | An ability cannot be started or stopped in Wukong mode. | 929| 16000011 | The context does not exist. | 930| 16000012 | The application is controlled. | 931| 16000013 | The application is controlled by EDM. | 932| 16000050 | Internal error. | 933| 16000053 | The ability is not on the top of the UI. | 934| 16000055 | Installation-free timed out. | 935| 16200001 | The caller has been released. | 936 937For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 938 939**Example** 940 941```ts 942import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 943import Want from '@ohos.app.ability.Want'; 944import StartOptions from '@ohos.app.ability.StartOptions'; 945 946export default class EntryAbility extends ServiceExtensionAbility { 947 onCreate(want: Want) { 948 // want contains the information about the caller who starts the application. 949 let localWant: Want = want; 950 localWant.bundleName = 'com.example.demo'; 951 localWant.moduleName = 'entry'; 952 localWant.abilityName = 'TestAbility'; 953 954 let option: StartOptions = { 955 displayId: 0 956 } 957 958 // Start a new ability using the caller information. 959 this.context.startAbilityAsCaller(localWant, option, (err) => { 960 if (err && err.code != 0) { 961 console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err)); 962 } else { 963 console.log('startAbilityAsCaller success.'); 964 } 965 }) 966 } 967} 968 969``` 970 971## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup> 972 973startAbilityAsCaller(want: Want, options?: StartOptions): Promise\<void>; 974 975Starts an ability with the caller information specified. The caller information is carried in **want** and identified at the system service layer. The ability can obtain the caller information from the **want** parameter in the **onCreate** lifecycle callback. When this API is used to start an ability, the caller information carried in **want** is not overwritten by the current application information. The system service layer can obtain the initial caller information. This API uses a promise to return the result. 976 977Observe the following when using this API: 978 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 979 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 980 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 981 982**System capability**: SystemCapability.Ability.AbilityRuntime.Core 983 984**System API**: This is a system API and cannot be called by third-party applications. 985 986**Parameters** 987 988| Name| Type| Mandatory| Description| 989| -------- | -------- | -------- | -------- | 990| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 991| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 992 993**Return value** 994 995| Type| Description| 996| -------- | -------- | 997| Promise<void> | Promise that returns no value.| 998 999**Error codes** 1000 1001| ID| Error Message| 1002| ------- | -------------------------------- | 1003| 16000001 | The specified ability does not exist. | 1004| 16000002 | Incorrect ability type. | 1005| 16000004 | Can not start invisible component. | 1006| 16000005 | The specified process does not have the permission. | 1007| 16000006 | Cross-user operations are not allowed. | 1008| 16000008 | The crowdtesting application expires. | 1009| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1010| 16000010 | The call with the continuation flag is forbidden. | 1011| 16000011 | The context does not exist. | 1012| 16000012 | The application is controlled. | 1013| 16000013 | The application is controlled by EDM. | 1014| 16000050 | Internal error. | 1015| 16000053 | The ability is not on the top of the UI. | 1016| 16000055 | Installation-free timed out. | 1017| 16200001 | The caller has been released. | 1018 1019For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1020 1021**Example** 1022 1023```ts 1024import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 1025import Want from '@ohos.app.ability.Want'; 1026import StartOptions from '@ohos.app.ability.StartOptions'; 1027import { BusinessError } from '@ohos.base'; 1028 1029export default class EntryAbility extends ServiceExtensionAbility { 1030 onCreate(want: Want) { 1031 // want contains the information about the caller who starts the application. 1032 let localWant: Want = want; 1033 localWant.bundleName = 'com.example.demo'; 1034 localWant.moduleName = 'entry'; 1035 localWant.abilityName = 'TestAbility'; 1036 1037 let option: StartOptions = { 1038 displayId: 0 1039 } 1040 1041 // Start a new ability using the caller information. 1042 this.context.startAbilityAsCaller(localWant, option) 1043 .then(() => { 1044 console.log('startAbilityAsCaller success.'); 1045 }) 1046 .catch((err: BusinessError) => { 1047 console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err)); 1048 }) 1049 } 1050} 1051 1052``` 1053 1054## ServiceExtensionContext.stopServiceExtensionAbility 1055 1056stopServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void; 1057 1058Stops a ServiceExtensionAbility in the same application. This API uses an asynchronous callback to return the result. 1059 1060**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1061 1062**System API**: This is a system API and cannot be called by third-party applications. 1063 1064**Parameters** 1065 1066| Name| Type| Mandatory| Description| 1067| -------- | -------- | -------- | -------- | 1068| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 1069| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.| 1070 1071**Error codes** 1072 1073| ID| Error Message| 1074| ------- | -------------------------------- | 1075| 16000001 | The specified ability does not exist. | 1076| 16000002 | Incorrect ability type. | 1077| 16000004 | Can not start invisible component. | 1078| 16000005 | The specified process does not have the permission. | 1079| 16000006 | Cross-user operations are not allowed. | 1080| 16000011 | The context does not exist. | 1081| 16000050 | Internal error. | 1082| 16200001 | The caller has been released. | 1083 1084For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1085 1086**Example** 1087 1088```ts 1089import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 1090import Want from '@ohos.app.ability.Want'; 1091import { BusinessError } from '@ohos.base'; 1092 1093class EntryAbility extends ServiceExtensionAbility { 1094 onCreate() { 1095 let want: Want = { 1096 deviceId: '', 1097 bundleName: 'com.example.myapplication', 1098 abilityName: 'EntryAbility' 1099 }; 1100 1101 try { 1102 this.context.stopServiceExtensionAbility(want, (error: BusinessError) => { 1103 if (error.code) { 1104 // Process service logic errors. 1105 console.error('stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}'); 1106 return; 1107 } 1108 // Carry out normal service processing. 1109 console.log('stopServiceExtensionAbility succeed'); 1110 }); 1111 } catch (paramError) { 1112 // Process input parameter errors. 1113 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 1114 } 1115 } 1116} 1117``` 1118 1119## ServiceExtensionContext.stopServiceExtensionAbility 1120 1121stopServiceExtensionAbility(want: Want): Promise\<void>; 1122 1123Stops a ServiceExtensionAbility in the same application. This API uses a promise to return the result. 1124 1125**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1126 1127**System API**: This is a system API and cannot be called by third-party applications. 1128 1129**Parameters** 1130 1131| Name| Type| Mandatory| Description| 1132| -------- | -------- | -------- | -------- | 1133| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 1134 1135**Return value** 1136 1137| Type| Description| 1138| -------- | -------- | 1139| Promise<void> | Promise used to return the result.| 1140 1141**Error codes** 1142 1143| ID| Error Message| 1144| ------- | -------------------------------- | 1145| 16000001 | The specified ability does not exist. | 1146| 16000002 | Incorrect ability type. | 1147| 16000004 | Can not start invisible component. | 1148| 16000005 | The specified process does not have the permission. | 1149| 16000006 | Cross-user operations are not allowed. | 1150| 16000011 | The context does not exist. | 1151| 16000050 | Internal error. | 1152| 16200001 | The caller has been released. | 1153 1154For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1155 1156**Example** 1157 1158```ts 1159import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 1160import Want from '@ohos.app.ability.Want'; 1161import { BusinessError } from '@ohos.base'; 1162 1163class EntryAbility extends ServiceExtensionAbility { 1164 onCreate() { 1165 let want: Want = { 1166 deviceId: '', 1167 bundleName: 'com.example.myapplication', 1168 abilityName: 'EntryAbility' 1169 }; 1170 1171 try { 1172 this.context.stopServiceExtensionAbility(want) 1173 .then(() => { 1174 // Carry out normal service processing. 1175 console.log('stopServiceExtensionAbility succeed'); 1176 }) 1177 .catch((error: BusinessError) => { 1178 // Process service logic errors. 1179 console.error('stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}'); 1180 }); 1181 } catch (paramError) { 1182 // Process input parameter errors. 1183 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 1184 } 1185 } 1186} 1187``` 1188 1189## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount 1190 1191stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void; 1192 1193Stops a ServiceExtensionAbility in the same application with the account ID specified. This API uses an asynchronous callback to return the result. 1194 1195> **NOTE** 1196> 1197> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user. 1198 1199**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1200 1201**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1202 1203**System API**: This is a system API and cannot be called by third-party applications. 1204 1205**Parameters** 1206 1207| Name| Type| Mandatory| Description| 1208| -------- | -------- | -------- | -------- | 1209| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 1210| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated).| 1211| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.| 1212 1213**Error codes** 1214 1215| ID| Error Message| 1216| ------- | -------------------------------- | 1217| 16000001 | The specified ability does not exist. | 1218| 16000002 | Incorrect ability type. | 1219| 16000004 | Can not start invisible component. | 1220| 16000005 | The specified process does not have the permission. | 1221| 16000006 | Cross-user operations are not allowed. | 1222| 16000011 | The context does not exist. | 1223| 16000050 | Internal error. | 1224| 16200001 | The caller has been released. | 1225 1226For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1227 1228**Example** 1229 1230```ts 1231import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 1232import Want from '@ohos.app.ability.Want'; 1233import { BusinessError } from '@ohos.base'; 1234 1235class EntryAbility extends ServiceExtensionAbility { 1236 onCreate() { 1237 let want: Want = { 1238 deviceId: '', 1239 bundleName: 'com.example.myapplication', 1240 abilityName: 'EntryAbility' 1241 }; 1242 let accountId = 100; 1243 1244 try { 1245 this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error: BusinessError) => { 1246 if (error.code) { 1247 // Process service logic errors. 1248 console.error('stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code, error.message: ${error.message}'); 1249 return; 1250 } 1251 // Carry out normal service processing. 1252 console.log('stopServiceExtensionAbilityWithAccount succeed'); 1253 }); 1254 } catch (paramError) { 1255 // Process input parameter errors. 1256 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 1257 } 1258 } 1259} 1260``` 1261 1262## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount 1263 1264stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>; 1265 1266Stops a ServiceExtensionAbility in the same application with the account ID specified. This API uses a promise to return the result. 1267 1268> **NOTE** 1269> 1270> The **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permission is not required when **accountId** specifies the current user. 1271 1272**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1273 1274**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1275 1276**System API**: This is a system API and cannot be called by third-party applications. 1277 1278**Parameters** 1279 1280| Name| Type| Mandatory| Description| 1281| -------- | -------- | -------- | -------- | 1282| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 1283| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1).| 1284 1285**Return value** 1286 1287| Type| Description| 1288| -------- | -------- | 1289| Promise<void> | Promise used to return the result.| 1290 1291**Error codes** 1292 1293| ID| Error Message| 1294| ------- | -------------------------------- | 1295| 16000001 | The specified ability does not exist. | 1296| 16000002 | Incorrect ability type. | 1297| 16000004 | Can not start invisible component. | 1298| 16000005 | The specified process does not have the permission. | 1299| 16000006 | Cross-user operations are not allowed. | 1300| 16000011 | The context does not exist. | 1301| 16000050 | Internal error. | 1302| 16200001 | The caller has been released. | 1303 1304For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1305 1306**Example** 1307 1308```ts 1309import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 1310import Want from '@ohos.app.ability.Want'; 1311import { BusinessError } from '@ohos.base'; 1312 1313class EntryAbility extends ServiceExtensionAbility { 1314 onCreate() { 1315 let want: Want = { 1316 deviceId: '', 1317 bundleName: 'com.example.myapplication', 1318 abilityName: 'EntryAbility' 1319 }; 1320 let accountId = 100; 1321 1322 try { 1323 this.context.stopServiceExtensionAbilityWithAccount(want, accountId) 1324 .then(() => { 1325 // Carry out normal service processing. 1326 console.log('stopServiceExtensionAbilityWithAccount succeed'); 1327 }) 1328 .catch((error: BusinessError) => { 1329 // Process service logic errors. 1330 console.error('stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}'); 1331 }); 1332 } catch (paramError) { 1333 // Process input parameter errors. 1334 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 1335 } 1336 } 1337} 1338``` 1339 1340## ServiceExtensionContext.terminateSelf 1341 1342terminateSelf(callback: AsyncCallback<void>): void; 1343 1344Terminates this ability. This API uses an asynchronous callback to return the result. 1345 1346**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1347 1348**System API**: This is a system API and cannot be called by third-party applications. 1349 1350**Parameters** 1351 1352| Name| Type| Mandatory| Description| 1353| -------- | -------- | -------- | -------- | 1354| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 1355 1356**Error codes** 1357 1358| ID| Error Message| 1359| ------- | -------------------------------- | 1360| 16000001 | The specified ability does not exist. | 1361| 16000004 | Can not start invisible component. | 1362| 16000005 | The specified process does not have the permission. | 1363| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1364| 16000011 | The context does not exist. | 1365| 16000050 | Internal error. | 1366 1367For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1368 1369**Example** 1370 1371```ts 1372import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 1373import { BusinessError } from '@ohos.base'; 1374 1375class EntryAbility extends ServiceExtensionAbility { 1376 onCreate() { 1377 this.context.terminateSelf((error: BusinessError) => { 1378 if (error.code) { 1379 // Process service logic errors. 1380 console.error('terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}'); 1381 return; 1382 } 1383 // Carry out normal service processing. 1384 console.log('terminateSelf succeed'); 1385 }); 1386 } 1387} 1388``` 1389 1390## ServiceExtensionContext.terminateSelf 1391 1392terminateSelf(): Promise<void>; 1393 1394Terminates this ability. This API uses a promise to return the result. 1395 1396**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1397 1398**System API**: This is a system API and cannot be called by third-party applications. 1399 1400**Return value** 1401 1402| Type| Description| 1403| -------- | -------- | 1404| Promise<void> | Promise used to return the result.| 1405 1406**Error codes** 1407 1408| ID| Error Message| 1409| ------- | -------------------------------- | 1410| 16000001 | The specified ability does not exist. | 1411| 16000004 | Can not start invisible component. | 1412| 16000005 | The specified process does not have the permission. | 1413| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1414| 16000011 | The context does not exist. | 1415| 16000050 | Internal error. | 1416 1417For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1418 1419**Example** 1420 1421```ts 1422import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 1423import { BusinessError } from '@ohos.base'; 1424 1425class EntryAbility extends ServiceExtensionAbility { 1426 onCreate() { 1427 this.context.terminateSelf().then(() => { 1428 // Carry out normal service processing. 1429 console.log('terminateSelf succeed'); 1430 }).catch((error: BusinessError) => { 1431 // Process service logic errors. 1432 console.error('terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}'); 1433 }); 1434 } 1435} 1436``` 1437 1438## ServiceExtensionContext.connectServiceExtensionAbility 1439 1440connectServiceExtensionAbility(want: Want, options: ConnectOptions): number; 1441 1442Connects this ability to a ServiceExtensionAbility. 1443 1444**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1445 1446**System API**: This is a system API and cannot be called by third-party applications. 1447 1448**Parameters** 1449 1450| Name| Type| Mandatory| Description| 1451| -------- | -------- | -------- | -------- | 1452| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability, such as the ability name and bundle name.| 1453| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | Yes| Callback used to return the information indicating that the connection is successful, interrupted, or failed.| 1454 1455**Return value** 1456 1457| Type| Description| 1458| -------- | -------- | 1459| number | A number, based on which the connection will be interrupted.| 1460 1461**Error codes** 1462 1463| ID| Error Message| 1464| ------- | -------------------------------- | 1465| 16000001 | The specified ability does not exist. | 1466| 16000002 | Incorrect ability type. | 1467| 16000004 | Can not start invisible component. | 1468| 16000005 | The specified process does not have the permission. | 1469| 16000006 | Cross-user operations are not allowed. | 1470| 16000008 | The crowdtesting application expires. | 1471| 16000053 | The ability is not on the top of the UI. | 1472| 16000055 | Installation-free timed out. | 1473| 16000011 | The context does not exist. | 1474| 16000050 | Internal error. | 1475 1476For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1477 1478**Example** 1479 1480```ts 1481import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 1482import rpc from '@ohos.rpc'; 1483import common from '@ohos.app.ability.common'; 1484import Want from '@ohos.app.ability.Want'; 1485import { BusinessError } from '@ohos.base'; 1486 1487let commRemote: rpc.IRemoteObject; // Release the instance when the connection is disconnected. 1488class EntryAbility extends ServiceExtensionAbility { 1489 onCreate() { 1490 let want: Want = { 1491 bundleName: 'com.example.myapp', 1492 abilityName: 'MyAbility' 1493 }; 1494 let options: common.ConnectOptions = { 1495 onConnect(elementName, remote) { 1496 commRemote = remote; 1497 console.log('----------- onConnect -----------'); 1498 }, 1499 onDisconnect(elementName) { console.log('----------- onDisconnect -----------') }, 1500 onFailed(code) { console.error('----------- onFailed -----------') } 1501 }; 1502 let connection: number; 1503 try { 1504 connection = this.context.connectServiceExtensionAbility(want, options); 1505 } catch (paramError) { 1506 // Process input parameter errors. 1507 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 1508 } 1509 } 1510} 1511``` 1512 1513## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount 1514 1515connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number; 1516 1517Uses the **AbilityInfo.AbilityType.SERVICE** template and account ID to connect this ability to another ability. 1518 1519**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1520 1521**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1522 1523**System API**: This is a system API and cannot be called by third-party applications. 1524 1525**Parameters** 1526 1527| Name| Type| Mandatory| Description| 1528| -------- | -------- | -------- | -------- | 1529| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 1530| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated).| 1531| options | ConnectOptions | Yes| Remote object instance.| 1532 1533**Return value** 1534 1535| Type| Description| 1536| -------- | -------- | 1537| number | Result code of the connection.| 1538 1539**Error codes** 1540 1541| ID| Error Message| 1542| ------- | -------------------------------- | 1543| 16000001 | The specified ability does not exist. | 1544| 16000002 | Incorrect ability type. | 1545| 16000004 | Can not start invisible component. | 1546| 16000005 | The specified process does not have the permission. | 1547| 16000006 | Cross-user operations are not allowed. | 1548| 16000008 | The crowdtesting application expires. | 1549| 16000053 | The ability is not on the top of the UI. | 1550| 16000055 | Installation-free timed out. | 1551| 16000011 | The context does not exist. | 1552| 16000050 | Internal error. | 1553 1554For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1555 1556**Example** 1557 1558```ts 1559import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 1560import rpc from '@ohos.rpc'; 1561import common from '@ohos.app.ability.common'; 1562import Want from '@ohos.app.ability.Want'; 1563import { BusinessError } from '@ohos.base'; 1564 1565let commRemote: rpc.IRemoteObject; // Release the instance when the connection is disconnected. 1566class EntryAbility extends ServiceExtensionAbility { 1567 onCreate() { 1568 let want: Want = { 1569 deviceId: '', 1570 bundleName: 'com.example.myapplication', 1571 abilityName: 'EntryAbility' 1572 }; 1573 let accountId = 100; 1574 let options: common.ConnectOptions = { 1575 onConnect(elementName, remote) { 1576 commRemote = remote; 1577 console.log('----------- onConnect -----------'); 1578 }, 1579 onDisconnect(elementName) { console.log('----------- onDisconnect -----------'); }, 1580 onFailed(code) { console.log('----------- onFailed -----------'); } 1581 }; 1582 let connection: number; 1583 try { 1584 connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options); 1585 } catch (paramError) { 1586 // Process input parameter errors. 1587 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 1588 } 1589 } 1590} 1591``` 1592 1593## ServiceExtensionContext.disconnectServiceExtensionAbility 1594 1595disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback<void>): void; 1596 1597Disconnects this ability from a ServiceExtensionAbility and after the successful disconnection, sets the remote object returned upon the connection to void. This API uses an asynchronous callback to return the result. 1598 1599**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1600 1601**System API**: This is a system API and cannot be called by third-party applications. 1602 1603**Parameters** 1604 1605| Name| Type| Mandatory| Description| 1606| -------- | -------- | -------- | -------- | 1607| connection | number | Yes| Number returned after **connectServiceExtensionAbility** is called.| 1608| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 1609 1610**Error codes** 1611 1612| ID| Error Message| 1613| ------- | -------------------------------- | 1614| 16000011 | The context does not exist. | 1615| 16000050 | Internal error. | 1616 1617For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1618 1619**Example** 1620 1621```ts 1622import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 1623import rpc from '@ohos.rpc'; 1624import { BusinessError } from '@ohos.base'; 1625 1626let commRemote: rpc.IRemoteObject | null; // Release the instance when the connection is disconnected. 1627class EntryAbility extends ServiceExtensionAbility { 1628 onCreate() { 1629 // connection is the return value of connectServiceExtensionAbility. 1630 let connection = 1; 1631 try { 1632 this.context.disconnectServiceExtensionAbility(connection, (error: BusinessError) => { 1633 commRemote = null; 1634 if (error.code) { 1635 // Process service logic errors. 1636 console.error(`disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 1637 return; 1638 } 1639 // Carry out normal service processing. 1640 console.log('disconnectServiceExtensionAbility succeed'); 1641 }); 1642 } catch (paramError) { 1643 commRemote = null; 1644 // Process input parameter errors. 1645 console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`); 1646 } 1647 } 1648} 1649``` 1650 1651## ServiceExtensionContext.disconnectServiceExtensionAbility 1652 1653disconnectServiceExtensionAbility(connection: number): Promise<void>; 1654 1655Disconnects this ability from a ServiceExtensionAbility and after the successful disconnection, sets the remote object returned upon the connection to void. This API uses a promise to return the result. 1656 1657**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1658 1659**System API**: This is a system API and cannot be called by third-party applications. 1660 1661**Parameters** 1662 1663| Name| Type| Mandatory| Description| 1664| -------- | -------- | -------- | -------- | 1665| connection | number | Yes| Number returned after **connectServiceExtensionAbility** is called.| 1666 1667**Return value** 1668 1669| Type| Description| 1670| -------- | -------- | 1671| Promise<void> | Promise used to return the result.| 1672 1673**Error codes** 1674 1675| ID| Error Message| 1676| ------- | -------------------------------- | 1677| 16000011 | The context does not exist. | 1678| 16000050 | Internal error. | 1679 1680For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1681 1682**Example** 1683 1684```ts 1685import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 1686import rpc from '@ohos.rpc'; 1687import { BusinessError } from '@ohos.base'; 1688 1689let commRemote: rpc.IRemoteObject | null; // Release the instance when the connection is disconnected. 1690class EntryAbility extends ServiceExtensionAbility { 1691 onCreate() { 1692 // connection is the return value of connectServiceExtensionAbility. 1693 let connection = 1; 1694 try { 1695 this.context.disconnectServiceExtensionAbility(connection) 1696 .then(() => { 1697 commRemote = null; 1698 // Carry out normal service processing. 1699 console.log('disconnectServiceExtensionAbility succeed'); 1700 }) 1701 .catch((error: BusinessError) => { 1702 commRemote = null; 1703 // Process service logic errors. 1704 console.error(`disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 1705 }); 1706 } catch (paramError) { 1707 commRemote = null; 1708 // Process input parameter errors. 1709 console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`); 1710 } 1711 } 1712} 1713``` 1714 1715## ServiceExtensionContext.startAbilityByCall 1716 1717startAbilityByCall(want: Want): Promise<Caller>; 1718 1719Starts an ability in the foreground or background and obtains the caller object for communicating with the ability. 1720 1721Observe the following when using this API: 1722 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 1723 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 1724 - The rules for using this API in the same-device and cross-device scenarios are different. For details, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 1725 1726**Required permissions**: ohos.permission.ABILITY_BACKGROUND_COMMUNICATION 1727 1728**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1729 1730**System API**: This is a system API and cannot be called by third-party applications. 1731 1732**Parameters** 1733 1734| Name| Type| Mandatory| Description| 1735| -------- | -------- | -------- | -------- | 1736| want | [Want](js-apis-app-ability-want.md) | Yes| Information about the ability to start, including **abilityName**, **moduleName**, **bundleName**, **deviceId** (optional), and **parameters** (optional). If **deviceId** is left blank or null, the local ability is started. If **parameters** is left blank or null, the ability is started in the background.| 1737 1738**Return value** 1739 1740| Type| Description| 1741| -------- | -------- | 1742| Promise<Caller> | Promise used to return the caller object to communicate with.| 1743 1744**Error codes** 1745 1746| ID| Error Message| 1747| ------- | -------------------------------- | 1748| 16000001 | The specified ability does not exist. | 1749| 16000002 | Incorrect ability type. | 1750| 16000004 | Can not start invisible component. | 1751| 16000005 | Static permission denied. The specified process does not have the permission. | 1752| 16000006 | Cross-user operations are not allowed. | 1753| 16000008 | The crowdtesting application expires. | 1754| 16000011 | The context does not exist. | 1755| 16000050 | Internal error. | 1756| 16200001 | The caller has been released. | 1757 1758For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1759 1760**Example** 1761 1762Start an ability in the background. 1763 1764```ts 1765import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 1766import { Caller } from '@ohos.app.ability.UIAbility'; 1767import Want from '@ohos.app.ability.Want'; 1768import { BusinessError } from '@ohos.base'; 1769 1770class EntryAbility extends ServiceExtensionAbility { 1771 onCreate() { 1772 let caller: Caller; 1773 1774 // Start an ability in the background by not passing parameters. 1775 let wantBackground: Want = { 1776 bundleName: 'com.example.myservice', 1777 moduleName: 'entry', 1778 abilityName: 'EntryAbility', 1779 deviceId: '' 1780 }; 1781 1782 try { 1783 this.context.startAbilityByCall(wantBackground) 1784 .then((obj: Caller) => { 1785 // Carry out normal service processing. 1786 caller = obj; 1787 console.log('startAbilityByCall succeed'); 1788 }).catch((error: BusinessError) => { 1789 // Process service logic errors. 1790 console.error('startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}'); 1791 }); 1792 } catch (paramError) { 1793 // Process input parameter errors. 1794 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 1795 } 1796 } 1797} 1798``` 1799 1800Start an ability in the foreground. 1801 1802```ts 1803import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 1804import { Caller } from '@ohos.app.ability.UIAbility'; 1805import Want from '@ohos.app.ability.Want'; 1806import { BusinessError } from '@ohos.base'; 1807 1808class EntryAbility extends ServiceExtensionAbility { 1809 onCreate() { 1810 let caller: Caller; 1811 1812 // Start an ability in the foreground with 'ohos.aafwk.param.callAbilityToForeground' in parameters set to true. 1813 let wantForeground: Want = { 1814 bundleName: 'com.example.myservice', 1815 moduleName: 'entry', 1816 abilityName: 'EntryAbility', 1817 deviceId: '', 1818 parameters: { 1819 'ohos.aafwk.param.callAbilityToForeground': true 1820 } 1821 }; 1822 1823 try { 1824 this.context.startAbilityByCall(wantForeground) 1825 .then((obj: Caller) => { 1826 // Carry out normal service processing. 1827 caller = obj; 1828 console.log('startAbilityByCall succeed'); 1829 }).catch((error: BusinessError) => { 1830 // Process service logic errors. 1831 console.error('startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}'); 1832 }); 1833 } catch (paramError) { 1834 // Process input parameter errors. 1835 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 1836 } 1837 } 1838} 1839``` 1840## ServiceExtensionContext.startRecentAbility 1841 1842startRecentAbility(want: Want, callback: AsyncCallback\<void>): void; 1843 1844Starts an ability. If the ability has multiple instances, the latest instance is started. This API uses an asynchronous callback to return the result. 1845 1846Observe the following when using this API: 1847 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 1848 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 1849 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 1850 1851**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1852 1853**System API**: This is a system API and cannot be called by third-party applications. 1854 1855**Parameters** 1856 1857| Name| Type| Mandatory| Description| 1858| -------- | -------- | -------- | -------- | 1859| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 1860| callback | AsyncCallback\<void> | Yes| Callback used to return the result.| 1861 1862**Error codes** 1863 1864For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1865 1866| ID| Error Message| 1867| ------- | -------------------------------- | 1868| 16000001 | The specified ability does not exist. | 1869| 16000002 | Incorrect ability type. | 1870| 16000004 | Can not start invisible component. | 1871| 16000005 | The specified process does not have the permission. | 1872| 16000006 | Cross-user operations are not allowed. | 1873| 16000008 | The crowdtesting application expires. | 1874| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1875| 16000010 | The call with the continuation flag is forbidden. | 1876| 16000011 | The context does not exist. | 1877| 16000050 | Internal error. | 1878| 16000053 | The ability is not on the top of the UI. | 1879| 16000055 | Installation-free timed out. | 1880| 16200001 | The caller has been released. | 1881 1882**Example** 1883 1884```ts 1885import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 1886import Want from '@ohos.app.ability.Want'; 1887import { BusinessError } from '@ohos.base'; 1888 1889class EntryAbility extends ServiceExtensionAbility { 1890 onCreate() { 1891 let want: Want = { 1892 bundleName: 'com.example.myapplication', 1893 abilityName: 'EntryAbility' 1894 }; 1895 1896 try { 1897 this.context.startRecentAbility(want, (err: BusinessError) => { 1898 if (err.code) { 1899 // Process service logic errors. 1900 console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); 1901 return; 1902 } 1903 // Carry out normal service processing. 1904 console.info('startRecentAbility succeed'); 1905 }); 1906 } catch (err) { 1907 // Process input parameter errors. 1908 let code = (err as BusinessError).code; 1909 let message = (err as BusinessError).message; 1910 console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); 1911 } 1912 } 1913} 1914``` 1915## ServiceExtensionContext.startRecentAbility 1916 1917startRecentAbility(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void; 1918 1919Starts an ability with the start options specified. If the ability has multiple instances, the latest instance is started. This API uses an asynchronous callback to return the result. 1920You can use this API to carry start options. 1921 1922Observe the following when using this API: 1923 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 1924 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 1925 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 1926 1927**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1928 1929**System API**: This is a system API and cannot be called by third-party applications. 1930 1931**Parameters** 1932 1933| Name| Type| Mandatory| Description| 1934| -------- | -------- | -------- | -------- | 1935| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 1936| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| 1937| callback | AsyncCallback\<void> | Yes| Callback used to return the result.| 1938 1939**Error codes** 1940 1941For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1942 1943| ID| Error Message| 1944| ------- | -------------------------------- | 1945| 16000001 | The specified ability does not exist. | 1946| 16000002 | Incorrect ability type. | 1947| 16000004 | Can not start invisible component. | 1948| 16000005 | The specified process does not have the permission. | 1949| 16000006 | Cross-user operations are not allowed. | 1950| 16000008 | The crowdtesting application expires. | 1951| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1952| 16000010 | The call with the continuation flag is forbidden. | 1953| 16000011 | The context does not exist. | 1954| 16000050 | Internal error. | 1955| 16000053 | The ability is not on the top of the UI. | 1956| 16000055 | Installation-free timed out. | 1957| 16200001 | The caller has been released. | 1958 1959**Example** 1960 1961```ts 1962import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 1963import Want from '@ohos.app.ability.Want'; 1964import StartOptions from '@ohos.app.ability.StartOptions'; 1965import { BusinessError } from '@ohos.base'; 1966 1967class EntryAbility extends ServiceExtensionAbility { 1968 onCreate() { 1969 let want: Want = { 1970 deviceId: '', 1971 bundleName: 'com.example.myapplication', 1972 abilityName: 'EntryAbility' 1973 }; 1974 let options: StartOptions = { 1975 windowMode: 0 1976 }; 1977 1978 try { 1979 this.context.startRecentAbility(want, options, (err: BusinessError) => { 1980 if (err.code) { 1981 // Process service logic errors. 1982 console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); 1983 return; 1984 } 1985 // Carry out normal service processing. 1986 console.info('startRecentAbility succeed'); 1987 }); 1988 } catch (err) { 1989 // Process input parameter errors. 1990 let code = (err as BusinessError).code; 1991 let message = (err as BusinessError).message; 1992 console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); 1993 } 1994 } 1995} 1996``` 1997## ServiceExtensionContext.startRecentAbility 1998 1999startRecentAbility(want: Want, options?: StartOptions): Promise\<void>; 2000 2001Starts an ability. If the ability has multiple instances, the latest instance is started. 2002This API uses a promise to return the result. 2003 2004Observe the following when using this API: 2005 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 2006 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 2007 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 2008 2009**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2010 2011**System API**: This is a system API and cannot be called by third-party applications. 2012 2013**Parameters** 2014 2015| Name| Type| Mandatory| Description| 2016| -------- | -------- | -------- | -------- | 2017| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 2018| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 2019 2020**Error codes** 2021 2022For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 2023 2024| ID| Error Message| 2025| ------- | -------------------------------- | 2026| 16000001 | The specified ability does not exist. | 2027| 16000002 | Incorrect ability type. | 2028| 16000004 | Can not start invisible component. | 2029| 16000005 | The specified process does not have the permission. | 2030| 16000006 | Cross-user operations are not allowed. | 2031| 16000008 | The crowdtesting application expires. | 2032| 16000009 | An ability cannot be started or stopped in Wukong mode. | 2033| 16000010 | The call with the continuation flag is forbidden. | 2034| 16000011 | The context does not exist. | 2035| 16000050 | Internal error. | 2036| 16000053 | The ability is not on the top of the UI. | 2037| 16000055 | Installation-free timed out. | 2038| 16200001 | The caller has been released. | 2039 2040**Example** 2041 2042```ts 2043import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 2044import Want from '@ohos.app.ability.Want'; 2045import StartOptions from '@ohos.app.ability.StartOptions'; 2046import { BusinessError } from '@ohos.base'; 2047 2048class EntryAbility extends ServiceExtensionAbility { 2049 onCreate() { 2050 let want: Want = { 2051 bundleName: 'com.example.myapplication', 2052 abilityName: 'EntryAbility' 2053 }; 2054 let options: StartOptions = { 2055 windowMode: 0, 2056 }; 2057 2058 try { 2059 this.context.startRecentAbility(want, options) 2060 .then(() => { 2061 // Carry out normal service processing. 2062 console.info('startRecentAbility succeed'); 2063 }) 2064 .catch((err: BusinessError) => { 2065 // Process service logic errors. 2066 console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); 2067 }); 2068 } catch (err) { 2069 // Process input parameter errors. 2070 let code = (err as BusinessError).code; 2071 let message = (err as BusinessError).message; 2072 console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); 2073 } 2074 } 2075} 2076``` 2077 2078## ServiceExtensionContext.startAbilityByCallWithAccount<sup>10+</sup> 2079 2080startAbilityByCallWithAccount(want: Want, accountId: number): Promise<Caller>; 2081 2082Starts an ability with the account ID specified and obtains the caller object for communicating with the ability. 2083 2084Observe the following when using this API: 2085 - If an application needs to call this API to start an ability that belongs to another user, it must have the **ohos.permission.ABILITY_BACKGROUND_COMMUNICATION** and **ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS** permissions. 2086 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 2087 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 2088 - The rules for using this API in the same-device and cross-device scenarios are different. For details, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 2089 2090**Required permissions**: ohos.permission.ABILITY_BACKGROUND_COMMUNICATION and ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 2091 2092**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2093 2094**System API**: This is a system API and cannot be called by third-party applications. 2095 2096**Parameters** 2097 2098| Name| Type| Mandatory| Description| 2099| -------- | -------- | -------- | -------- | 2100| want | [Want](js-apis-app-ability-want.md) | Yes| Information about the ability to start, including **abilityName**, **moduleName**, **bundleName**, **deviceId** (optional), and **parameters** (optional). If **deviceId** is left blank or null, the local ability is started. If **parameters** is left blank or null, the ability is started in the background.| 2101| accountId | number | Yes| ID of a system account. The value **-1** indicates the current user. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1).| 2102 2103**Return value** 2104 2105| Type| Description| 2106| -------- | -------- | 2107| Promise<Caller> | Promise used to return the caller object to communicate with.| 2108 2109**Error codes** 2110 2111| ID| Error Message| 2112| ------- | -------------------------------- | 2113| 16000001 | The specified ability does not exist. | 2114| 16000002 | Incorrect ability type. | 2115| 16000004 | Can not start invisible component. | 2116| 16000005 | Static permission denied. The specified process does not have the permission. | 2117| 16000006 | Cross-user operations are not allowed. | 2118| 16000008 | The crowdtesting application expires. | 2119| 16000011 | The context does not exist. | 2120| 16000012 | The application is controlled. | 2121| 16000013 | The application is controlled by EDM. | 2122| 16000050 | Internal error. | 2123| 16200001 | The caller has been released. | 2124 2125For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 2126 2127**Example** 2128 2129```ts 2130import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 2131import { Caller } from '@ohos.app.ability.UIAbility'; 2132import Want from '@ohos.app.ability.Want'; 2133import StartOptions from '@ohos.app.ability.StartOptions'; 2134import { BusinessError } from '@ohos.base'; 2135 2136class EntryAbility extends ServiceExtensionAbility { 2137 onCreate() { 2138 let caller: Caller; 2139 // ID of a system account. The value -1 indicates the current user. 2140 let accountId = -1; 2141 // Specify the ability to start. 2142 let want: Want = { 2143 bundleName: 'com.acts.actscalleeabilityrely', 2144 moduleName: 'entry', 2145 abilityName: 'EntryAbility', 2146 deviceId: '', 2147 parameters: { 2148 // If the value of 'ohos.aafwk.param.callAbilityToForeground' is true, the ability is started in the foreground. If the value is false or not set, the ability is started in the background. 2149 'ohos.aafwk.param.callAbilityToForeground': true 2150 } 2151 }; 2152 2153 try { 2154 this.context.startAbilityByCallWithAccount(want, accountId) 2155 .then((obj: Caller) => { 2156 // Carry out normal service processing. 2157 caller = obj; 2158 console.log('startAbilityByCallWithAccount succeed'); 2159 }).catch((error: BusinessError) => { 2160 // Process service logic errors. 2161 console.error('startAbilityByCallWithAccount failed, error.code: ${error.code}, error.message: ${error.message}'); 2162 }); 2163 } catch (paramError) { 2164 // Process input parameter errors. 2165 console.error('error.code: ${paramError.code}, error.message: ${paramError.message}'); 2166 } 2167 } 2168} 2169``` 2170 2171## ServiceExtensionContext.requestModalUIExtension<sup>11+<sup> 2172 2173requestModalUIExtension(pickerWant: Want): Promise\<void> 2174 2175Requests the specified foreground application to start the UIExtensionAbility of the corresponding type. The foreground application is specified by **bundleName** in **want.parameters**. If **bundleName** is left unspecified, or if the application specified by **bundleName** is not running in the foreground or does not exist, the UIExtensionAbility is directly started on the system UI. The UIExtensionAbility to start is determined by the combination of the **bundleName**, **abilityName**, and **moduleName** fields in **want**, and its type is determined by the **ability.want.params.uiExtensionType** field in **want.parameters**. This API uses a promise to return the result. 2176 2177Observe the following when using this API: 2178- If **exported** of the target ability is **false** in cross-application scenarios, the specified foreground application or the caller (when the UIExtensionAbility is directly started on the system UI) must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 2179- For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 2180 2181**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2182 2183**System API**: This is a system API. 2184 2185**Parameters** 2186 2187| Name| Type| Mandatory| Description| 2188| -------- | -------- | -------- | -------- | 2189| pickerWant | [Want](js-apis-app-ability-want.md) | Yes| Want information used to start the modal window.| 2190 2191**Return value** 2192 2193| Type| Description| 2194| -------- | -------- | 2195| Promise<void> | Promise that returns no value.| 2196 2197**Error codes** 2198 2199| ID| Error Message| 2200| ------- | -------------------------------- | 2201| 16000001 | The specified ability does not exist. | 2202| 16000002 | Incorrect ability type. | 2203| 16000004 | Can not start invisible component. | 2204| 16000050 | Internal error. | 2205| 16200001 | The caller has been released. | 2206 2207For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 2208 2209**Example** 2210 2211```ts 2212import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 2213import Want from '@ohos.app.ability.Want'; 2214import { BusinessError } from '@ohos.base'; 2215 2216class ServiceExtension extends ServiceExtensionAbility { 2217 onCreate() { 2218 let pickerWant: Want = { 2219 bundleName: 'com.example.myapplication', 2220 abilityName: 'com.example.myapplication.UIExtAbility', 2221 moduleName: 'entry_test', 2222 parameters: { 2223 'bundleName': 'com.example.myapplication', 2224 // The value is the same as the value of type configured for com.example.myapplication.UIExtAbility. 2225 'ability.want.params.uiExtensionType': 'sys/commonUI' 2226 } 2227 }; 2228 2229 try { 2230 this.context.requestModalUIExtension(pickerWant) 2231 .then(() => { 2232 // Carry out normal service processing. 2233 console.info('requestModalUIExtension succeed'); 2234 }) 2235 .catch((err: BusinessError) => { 2236 // Process service logic errors. 2237 console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`); 2238 }); 2239 } catch (err) { 2240 // Process input parameter errors. 2241 let code = (err as BusinessError).code; 2242 let message = (err as BusinessError).message; 2243 console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`); 2244 } 2245 } 2246} 2247``` 2248 2249## ServiceExtensionContext.requestModalUIExtension<sup>11+<sup> 2250 2251requestModalUIExtension(pickerWant: Want, callback: AsyncCallback\<void>): void 2252 2253Requests the specified foreground application to start the UIExtensionAbility of the corresponding type. The foreground application is specified by **bundleName** in **want.parameters**. If **bundleName** is left unspecified, or if the application specified by **bundleName** is not running in the foreground or does not exist, the UIExtensionAbility is directly started on the system UI. The UIExtensionAbility to start is determined by the combination of the **bundleName**, **abilityName**, and **moduleName** fields in **want**, and its type is determined by the **ability.want.params.uiExtensionType** field in **want.parameters**. This API uses an asynchronous callback to return the result. 2254 2255Observe the following when using this API: 2256- If **exported** of the target ability is **false** in cross-application scenarios, the specified foreground application or the caller (when the UIExtensionAbility is directly started on the system UI) must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 2257- For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 2258 2259**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2260 2261**System API**: This is a system API. 2262 2263**Parameters** 2264 2265| Name| Type| Mandatory| Description| 2266| -------- | -------- | -------- | -------- | 2267| pickerWant | [Want](js-apis-app-ability-want.md) | Yes| Want information used to start the modal window.| 2268| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the UIExtensionAbility is started, **err** is **undefined**; otherwise, **err** is an error object.| 2269 2270**Error codes** 2271 2272| ID| Error Message| 2273| ------- | -------------------------------- | 2274| 16000001 | The specified ability does not exist. | 2275| 16000002 | Incorrect ability type. | 2276| 16000004 | Can not start invisible component. | 2277| 16000050 | Internal error. | 2278| 16200001 | The caller has been released. | 2279 2280For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 2281 2282**Example** 2283 2284```ts 2285import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 2286import Want from '@ohos.app.ability.Want'; 2287import { BusinessError } from '@ohos.base'; 2288 2289class ServiceExtension extends ServiceExtensionAbility { 2290 onCreate() { 2291 let pickerWant: Want = { 2292 bundleName: 'com.example.myapplication', 2293 abilityName: 'com.example.myapplication.UIExtAbility', 2294 moduleName: 'entry_test', 2295 parameters: { 2296 'bundleName': 'com.example.myapplication', 2297 // The value is the same as the value of type configured for com.example.myapplication.UIExtAbility. 2298 'ability.want.params.uiExtensionType': 'sys/commonUI' 2299 } 2300 }; 2301 2302 try { 2303 this.context.requestModalUIExtension(pickerWant, (err: BusinessError) => { 2304 if (err.code) { 2305 // Process service logic errors. 2306 console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`); 2307 return; 2308 } 2309 // Carry out normal service processing. 2310 console.info('requestModalUIExtension succeed'); 2311 }); 2312 } catch (err) { 2313 // Process input parameter errors. 2314 let code = (err as BusinessError).code; 2315 let message = (err as BusinessError).message; 2316 console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`); 2317 } 2318 } 2319} 2320``` 2321