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