1# ServiceExtensionContext (System API) 2<!--Kit: Ability Kit--> 3<!--Subsystem: Ability--> 4<!--Owner: @yewei0794--> 5<!--Designer: @jsjzju--> 6<!--Tester: @lixueqing513--> 7<!--Adviser: @huipeizi--> 8 9The ServiceExtensionContext module provides the context environment for the ServiceExtensionAbility. It inherits from ExtensionContext. 10 11You can use the APIs of this module to start, terminate, connect, and disconnect an ability. 12 13> **NOTE** 14> 15> - 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. 16> - The APIs of this module can be used only in the stage model. 17> - The APIs of this module must be used in the main thread, but not in child threads such as Worker and TaskPool. 18> - The APIs provided by this module are system APIs. 19 20## Modules to Import 21 22```ts 23import { common } from '@kit.AbilityKit'; 24``` 25 26## Usage 27 28Before using the ServiceExtensionContext module, you must define a child class that inherits from ServiceExtensionAbility. 29 30**Example** 31 32```ts 33import { ServiceExtensionAbility } from '@kit.AbilityKit'; 34import { rpc } from '@kit.IPCKit'; 35 36let commRemote: rpc.IRemoteObject | null; // Release the instance when the connection is disconnected. 37 38class EntryAbility extends ServiceExtensionAbility { 39 onCreate() { 40 let context = this.context; // Obtain a ServiceExtensionContext instance. 41 } 42} 43``` 44 45## ServiceExtensionContext.startAbility 46 47startAbility(want: Want, callback: AsyncCallback<void>): void 48 49Starts an ability. This API can be called only by the main thread. It uses an asynchronous callback to return the result. 50 51**System capability**: SystemCapability.Ability.AbilityRuntime.Core 52 53**System API**: This is a system API. 54 55**Parameters** 56 57| Name| Type| Mandatory| Description| 58| -------- | -------- | -------- | -------- | 59| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability, such as the ability name and bundle name.| 60| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the ability is started, **err** is **undefined**; otherwise, **err** is an error object.| 61 62**Error codes** 63 64For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 65 66| ID| Error Message| 67| ------- | -------- | 68| 201 | The application does not have permission to call the interface. | 69| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 70| 16000001 | The specified ability does not exist. | 71| 16000002 | Incorrect ability type. | 72| 16000004 | Cannot start an invisible component. | 73| 16000005 | The specified process does not have the permission. | 74| 16000006 | Cross-user operations are not allowed. | 75| 16000008 | The crowdtesting application expires. | 76| 16000009 | An ability cannot be started or stopped in Wukong mode. | 77| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 78| 16000011 | The context does not exist. | 79| 16000012 | The application is controlled. | 80| 16000013 | The application is controlled by EDM. | 81| 16000019 | No matching ability is found. | 82| 16000050 | Internal error. | 83| 16000053 | The ability is not on the top of the UI. | 84| 16000055 | Installation-free timed out. | 85| 16000071 | App clone is not supported. | 86| 16000072 | App clone or multi-instance is not supported. | 87| 16000073 | The app clone index is invalid. | 88| 16000076 | The app instance key is invalid. | 89| 16000077 | The number of app instances reaches the limit. | 90| 16000078 | The multi-instance is not supported. | 91| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 92| 16000080 | Creating a new instance is not supported. | 93| 16200001 | The caller has been released. | 94 95**Example** 96 97```ts 98import { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 99import { BusinessError } from '@kit.BasicServicesKit'; 100 101class EntryAbility extends ServiceExtensionAbility { 102 onCreate() { 103 let want: Want = { 104 bundleName: 'com.example.myapp', 105 abilityName: 'MyAbility' 106 }; 107 108 try { 109 this.context.startAbility(want, (error: BusinessError) => { 110 if (error.code) { 111 // Process service logic errors. 112 console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 113 return; 114 } 115 // Carry out normal service processing. 116 console.info('startAbility succeed'); 117 }); 118 } catch (paramError) { 119 // Process input parameter errors. 120 console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`); 121 } 122 } 123} 124``` 125 126## ServiceExtensionContext.startAbility 127 128startAbility(want: Want, options?: StartOptions): Promise\<void> 129 130Starts an ability. This API can be called only by the main thread. It uses a promise to return the result. 131 132**System capability**: SystemCapability.Ability.AbilityRuntime.Core 133 134**System API**: This is a system API. 135 136**Parameters** 137 138| Name| Type| Mandatory| Description| 139| -------- | -------- | -------- | -------- | 140| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability, such as the ability name and bundle name.| 141| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 142 143**Return value** 144 145| Type| Description| 146| -------- | -------- | 147| Promise<void> | Promise that returns no value.| 148 149**Error codes** 150 151For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 152 153| ID| Error Message| 154| ------- | -------- | 155| 201 | The application does not have permission to call the interface. | 156| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 157| 16000001 | The specified ability does not exist. | 158| 16000002 | Incorrect ability type. | 159| 16000004 | Cannot start an invisible component. | 160| 16000005 | The specified process does not have the permission. | 161| 16000006 | Cross-user operations are not allowed. | 162| 16000008 | The crowdtesting application expires. | 163| 16000009 | An ability cannot be started or stopped in Wukong mode. | 164| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 165| 16000011 | The context does not exist. | 166| 16000012 | The application is controlled. | 167| 16000013 | The application is controlled by EDM. | 168| 16000019 | No matching ability is found. | 169| 16000050 | Internal error. | 170| 16000053 | The ability is not on the top of the UI. | 171| 16000055 | Installation-free timed out. | 172| 16000071 | App clone is not supported. | 173| 16000072 | App clone or multi-instance is not supported. | 174| 16000073 | The app clone index is invalid. | 175| 16000076 | The app instance key is invalid. | 176| 16000077 | The number of app instances reaches the limit. | 177| 16000078 | The multi-instance is not supported. | 178| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 179| 16000080 | Creating a new instance is not supported. | 180| 16200001 | The caller has been released. | 181 182**Example** 183 184```ts 185import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 186import { BusinessError } from '@kit.BasicServicesKit'; 187 188class EntryAbility extends ServiceExtensionAbility { 189 onCreate() { 190 let want: Want = { 191 bundleName: 'com.example.myapp', 192 abilityName: 'MyAbility' 193 }; 194 let options: StartOptions = { 195 windowMode: 0, 196 }; 197 198 try { 199 this.context.startAbility(want, options) 200 .then((data: void) => { 201 // Carry out normal service processing. 202 console.info('startAbility succeed'); 203 }) 204 .catch((error: BusinessError) => { 205 // Process service logic errors. 206 console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 207 }); 208 } catch (paramError) { 209 // Process input parameter errors. 210 console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`); 211 } 212 } 213} 214``` 215 216## ServiceExtensionContext.startAbility 217 218startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void 219 220Starts an ability. This API can be called only by the main thread. It uses an asynchronous callback to return the result. 221 222**System capability**: SystemCapability.Ability.AbilityRuntime.Core 223 224**System API**: This is a system API. 225 226**Parameters** 227 228| Name| Type| Mandatory| Description| 229| -------- | -------- | -------- | -------- | 230| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 231| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| 232| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the ability is started, **err** is **undefined**; otherwise, **err** is an error object.| 233 234**Error codes** 235 236For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 237 238| ID| Error Message| 239| ------- | -------- | 240| 201 | The application does not have permission to call the interface. | 241| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 242| 16000001 | The specified ability does not exist. | 243| 16000002 | Incorrect ability type. | 244| 16000004 | Cannot start an invisible component. | 245| 16000005 | The specified process does not have the permission. | 246| 16000006 | Cross-user operations are not allowed. | 247| 16000008 | The crowdtesting application expires. | 248| 16000009 | An ability cannot be started or stopped in Wukong mode. | 249| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 250| 16000011 | The context does not exist. | 251| 16000012 | The application is controlled. | 252| 16000013 | The application is controlled by EDM. | 253| 16000019 | No matching ability is found. | 254| 16000050 | Internal error. | 255| 16000053 | The ability is not on the top of the UI. | 256| 16000055 | Installation-free timed out. | 257| 16000071 | App clone is not supported. | 258| 16000072 | App clone or multi-instance is not supported. | 259| 16000073 | The app clone index is invalid. | 260| 16000076 | The app instance key is invalid. | 261| 16000077 | The number of app instances reaches the limit. | 262| 16000078 | The multi-instance is not supported. | 263| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 264| 16000080 | Creating a new instance is not supported. | 265| 16200001 | The caller has been released. | 266 267**Example** 268 269```ts 270import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 271import { BusinessError } from '@kit.BasicServicesKit'; 272 273class EntryAbility extends ServiceExtensionAbility { 274 onCreate() { 275 let want: Want = { 276 deviceId: '', 277 bundleName: 'com.example.myapplication', 278 abilityName: 'EntryAbility' 279 }; 280 let options: StartOptions = { 281 windowMode: 0 282 }; 283 284 try { 285 this.context.startAbility(want, options, (error: BusinessError) => { 286 if (error.code) { 287 // Process service logic errors. 288 console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 289 return; 290 } 291 // Carry out normal service processing. 292 console.info('startAbility succeed'); 293 }); 294 } catch (paramError) { 295 // Process input parameter errors. 296 console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`); 297 } 298 } 299} 300``` 301 302## ServiceExtensionContext.startAbilityWithAccount 303 304startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void 305 306Starts an ability with the account ID specified. This API can be called only by the main thread. It uses an asynchronous callback to return the result. 307 308> **NOTE** 309> 310> 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). 311 312**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 313 314**System capability**: SystemCapability.Ability.AbilityRuntime.Core 315 316**System API**: This is a system API. 317 318**Parameters** 319 320| Name| Type| Mandatory| Description| 321| -------- | -------- | -------- | -------- | 322| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 323| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated).| 324| callback | AsyncCallback\<void\> | Yes| Callback used to return the result. If the ability is started, **err** is **undefined**; otherwise, **err** is an error object.| 325 326**Error codes** 327 328For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 329 330| ID| Error Message| 331| ------- | -------- | 332| 201 | The application does not have permission to call the interface. | 333| 202 | The application is not system-app, can not use system-api. | 334| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 335| 16000001 | The specified ability does not exist. | 336| 16000002 | Incorrect ability type. | 337| 16000004 | Cannot start an invisible component. | 338| 16000005 | The specified process does not have the permission. | 339| 16000006 | Cross-user operations are not allowed. | 340| 16000008 | The crowdtesting application expires. | 341| 16000009 | An ability cannot be started or stopped in Wukong mode. | 342| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 343| 16000011 | The context does not exist. | 344| 16000012 | The application is controlled. | 345| 16000013 | The application is controlled by EDM. | 346| 16000019 | No matching ability is found. | 347| 16000050 | Internal error. | 348| 16000053 | The ability is not on the top of the UI. | 349| 16000055 | Installation-free timed out. | 350| 16000071 | App clone is not supported. | 351| 16000072 | App clone or multi-instance is not supported. | 352| 16000073 | The app clone index is invalid. | 353| 16000076 | The app instance key is invalid. | 354| 16000077 | The number of app instances reaches the limit. | 355| 16000078 | The multi-instance is not supported. | 356| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 357| 16000080 | Creating a new instance is not supported. | 358| 16200001 | The caller has been released. | 359 360**Example** 361 362```ts 363import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 364import { BusinessError } from '@kit.BasicServicesKit'; 365 366class EntryAbility extends ServiceExtensionAbility { 367 onCreate() { 368 let want: Want = { 369 deviceId: '', 370 bundleName: 'com.example.myapplication', 371 abilityName: 'EntryAbility' 372 }; 373 let accountId = 100; 374 375 try { 376 this.context.startAbilityWithAccount(want, accountId, (error: BusinessError) => { 377 if (error.code) { 378 // Process service logic errors. 379 console.error(`startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`); 380 return; 381 } 382 // Carry out normal service processing. 383 console.info('startAbilityWithAccount succeed'); 384 }); 385 } catch (paramError) { 386 // Process input parameter errors. 387 console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`); 388 } 389 } 390} 391``` 392 393## ServiceExtensionContext.startAbilityWithAccount 394 395startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void 396 397Starts an ability with the account ID and start options specified This API can be called only by the main thread. It uses an asynchronous callback to return the result. 398 399> **NOTE** 400> 401> 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). 402 403**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 404 405**System capability**: SystemCapability.Ability.AbilityRuntime.Core 406 407**System API**: This is a system API. 408 409**Parameters** 410 411| Name| Type| Mandatory| Description| 412| -------- | -------- | -------- | -------- | 413| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 414| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated).| 415| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| 416| callback | AsyncCallback\<void\> | Yes| Callback used to return the result. If the ability is started, **err** is **undefined**; otherwise, **err** is an error object.| 417 418**Error codes** 419 420For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 421 422| ID| Error Message| 423| ------- | -------- | 424| 201 | The application does not have permission to call the interface. | 425| 202 | The application is not system-app, can not use system-api. | 426| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 427| 16000001 | The specified ability does not exist. | 428| 16000002 | Incorrect ability type. | 429| 16000004 | Cannot start an invisible component. | 430| 16000005 | The specified process does not have the permission. | 431| 16000006 | Cross-user operations are not allowed. | 432| 16000008 | The crowdtesting application expires. | 433| 16000009 | An ability cannot be started or stopped in Wukong mode. | 434| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 435| 16000011 | The context does not exist. | 436| 16000012 | The application is controlled. | 437| 16000013 | The application is controlled by EDM. | 438| 16000019 | No matching ability is found. | 439| 16000050 | Internal error. | 440| 16000053 | The ability is not on the top of the UI. | 441| 16000055 | Installation-free timed out. | 442| 16000071 | App clone is not supported. | 443| 16000072 | App clone or multi-instance is not supported. | 444| 16000073 | The app clone index is invalid. | 445| 16000076 | The app instance key is invalid. | 446| 16000077 | The number of app instances reaches the limit. | 447| 16000078 | The multi-instance is not supported. | 448| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 449| 16000080 | Creating a new instance is not supported. | 450| 16200001 | The caller has been released. | 451 452**Example** 453 454```ts 455import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 456import { BusinessError } from '@kit.BasicServicesKit'; 457 458class EntryAbility extends ServiceExtensionAbility { 459 onCreate() { 460 let want: Want = { 461 deviceId: '', 462 bundleName: 'com.example.myapplication', 463 abilityName: 'EntryAbility' 464 }; 465 let accountId = 100; 466 let options: StartOptions = { 467 windowMode: 0 468 }; 469 470 try { 471 this.context.startAbilityWithAccount(want, accountId, options, (error: BusinessError) => { 472 if (error.code) { 473 // Process service logic errors. 474 console.error(`startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`); 475 return; 476 } 477 // Carry out normal service processing. 478 console.info('startAbilityWithAccount succeed'); 479 }); 480 } catch (paramError) { 481 // Process input parameter errors. 482 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 483 } 484 } 485} 486``` 487 488 489## ServiceExtensionContext.startAbilityWithAccount 490 491startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void> 492 493Starts an ability with the account ID specified. This API can be called only by the main thread. It uses a promise to return the result. 494 495> **NOTE** 496> 497> 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). 498 499**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 500 501**System capability**: SystemCapability.Ability.AbilityRuntime.Core 502 503**System API**: This is a system API. 504 505**Parameters** 506 507| Name| Type| Mandatory| Description| 508| -------- | -------- | -------- | -------- | 509| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 510| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1).| 511| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 512 513**Return value** 514 515| Type| Description| 516| -------- | -------- | 517| Promise<void> | Promise that returns no value.| 518 519**Error codes** 520 521For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 522 523| ID| Error Message| 524| ------- | -------- | 525| 201 | The application does not have permission to call the interface. | 526| 202 | The application is not system-app, can not use system-api. | 527| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 528| 16000001 | The specified ability does not exist. | 529| 16000002 | Incorrect ability type. | 530| 16000004 | Cannot start an invisible component. | 531| 16000005 | The specified process does not have the permission. | 532| 16000006 | Cross-user operations are not allowed. | 533| 16000008 | The crowdtesting application expires. | 534| 16000009 | An ability cannot be started or stopped in Wukong mode. | 535| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 536| 16000011 | The context does not exist. | 537| 16000012 | The application is controlled. | 538| 16000013 | The application is controlled by EDM. | 539| 16000019 | No matching ability is found. | 540| 16000050 | Internal error. | 541| 16000053 | The ability is not on the top of the UI. | 542| 16000055 | Installation-free timed out. | 543| 16000071 | App clone is not supported. | 544| 16000072 | App clone or multi-instance is not supported. | 545| 16000073 | The app clone index is invalid. | 546| 16000076 | The app instance key is invalid. | 547| 16000077 | The number of app instances reaches the limit. | 548| 16000078 | The multi-instance is not supported. | 549| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 550| 16000080 | Creating a new instance is not supported. | 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.info('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 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. 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. If the ServiceExtensionAbility is started, **err** is **undefined**; otherwise, **err** is an error object.| 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 | Cannot start an invisible component. | 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.info('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. 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 that returns no value.| 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 | Cannot start an invisible component. | 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.info('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 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. 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. If the ServiceExtensionAbility is started, **err** is **undefined**; otherwise, **err** is an error object.| 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 | Cannot start an invisible component. | 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.info('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. 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 that returns no value.| 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 | Cannot start an invisible component. | 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.info('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 can be called only by the main thread. It uses an asynchronous callback to return the result. 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. 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 | Cannot start an invisible component. | 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 and prepare 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 a new instance is not supported. | 953| 16200001 | The caller has been released. | 954 955**Example** 956 957```ts 958import { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 959 960class EntryAbility extends ServiceExtensionAbility { 961 onCreate(want: Want) { 962 // want contains the information about the caller who starts the application. 963 let localWant: Want = want; 964 localWant.bundleName = 'com.example.demo'; 965 localWant.moduleName = 'entry'; 966 localWant.abilityName = 'TestAbility'; 967 968 // Start a new ability using the caller information. 969 this.context.startAbilityAsCaller(localWant, (err) => { 970 if (err && err.code != 0) { 971 console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err)); 972 } else { 973 console.info('startAbilityAsCaller success.'); 974 } 975 }) 976 } 977} 978``` 979 980## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup> 981 982startAbilityAsCaller(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void 983 984Starts 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 can be called only by the main thread. It uses an asynchronous callback to return the result. 985 986> **NOTE** 987> 988> 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). 989 990**System capability**: SystemCapability.Ability.AbilityRuntime.Core 991 992**System API**: This is a system API. 993 994**Parameters** 995 996| Name| Type| Mandatory| Description| 997| -------- | -------- | -------- | -------- | 998| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 999| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| 1000| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the ability is started, **err** is **undefined**; otherwise, **err** is an error object.| 1001 1002**Error codes** 1003 1004For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1005 1006| ID| Error Message| 1007| ------- | -------- | 1008| 201 | The application does not have permission to call the interface. | 1009| 202 | The application is not system-app, can not use system-api. | 1010| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1011| 16000001 | The specified ability does not exist. | 1012| 16000004 | Cannot start an invisible component. | 1013| 16000005 | The specified process does not have the permission. | 1014| 16000006 | Cross-user operations are not allowed. | 1015| 16000008 | The crowdtesting application expires. | 1016| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1017| 16000011 | The context does not exist. | 1018| 16000012 | The application is controlled. | 1019| 16000013 | The application is controlled by EDM. | 1020| 16000050 | Internal error. | 1021| 16000053 | The ability is not on the top of the UI. | 1022| 16000055 | Installation-free timed out. | 1023| 16000071 | App clone is not supported. | 1024| 16000072 | App clone or multi-instance is not supported. | 1025| 16000073 | The app clone index is invalid. | 1026| 16000076 | The app instance key is invalid. | 1027| 16000077 | The number of app instances reaches the limit. | 1028| 16000078 | The multi-instance is not supported. | 1029| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 1030| 16000080 | Creating a new instance is not supported. | 1031| 16200001 | The caller has been released. | 1032 1033**Example** 1034 1035```ts 1036import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 1037 1038class EntryAbility extends ServiceExtensionAbility { 1039 onCreate(want: Want) { 1040 // want contains the information about the caller who starts the application. 1041 let localWant: Want = want; 1042 localWant.bundleName = 'com.example.demo'; 1043 localWant.moduleName = 'entry'; 1044 localWant.abilityName = 'TestAbility'; 1045 1046 let option: StartOptions = { 1047 displayId: 0 1048 } 1049 1050 // Start a new ability using the caller information. 1051 this.context.startAbilityAsCaller(localWant, option, (err) => { 1052 if (err && err.code != 0) { 1053 console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err)); 1054 } else { 1055 console.info('startAbilityAsCaller success.'); 1056 } 1057 }) 1058 } 1059} 1060``` 1061 1062## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup> 1063 1064startAbilityAsCaller(want: Want, options?: StartOptions): Promise\<void> 1065 1066Starts 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 can be called only by the main thread. It uses a promise to return the result. 1067 1068> **NOTE** 1069> 1070> 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). 1071 1072**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1073 1074**System API**: This is a system API. 1075 1076**Parameters** 1077 1078| Name| Type| Mandatory| Description| 1079| -------- | -------- | -------- | -------- | 1080| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 1081| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 1082 1083**Return value** 1084 1085| Type| Description| 1086| -------- | -------- | 1087| Promise<void> | Promise that returns no value.| 1088 1089**Error codes** 1090 1091For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1092 1093| ID| Error Message| 1094| ------- | -------- | 1095| 201 | The application does not have permission to call the interface. | 1096| 202 | The application is not system-app, can not use system-api. | 1097| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1098| 16000001 | The specified ability does not exist. | 1099| 16000002 | Incorrect ability type. | 1100| 16000004 | Cannot start an invisible component. | 1101| 16000005 | The specified process does not have the permission. | 1102| 16000006 | Cross-user operations are not allowed. | 1103| 16000008 | The crowdtesting application expires. | 1104| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1105| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 1106| 16000011 | The context does not exist. | 1107| 16000012 | The application is controlled. | 1108| 16000013 | The application is controlled by EDM. | 1109| 16000050 | Internal error. | 1110| 16000053 | The ability is not on the top of the UI. | 1111| 16000055 | Installation-free timed out. | 1112| 16000071 | App clone is not supported. | 1113| 16000072 | App clone or multi-instance is not supported. | 1114| 16000073 | The app clone index is invalid. | 1115| 16000076 | The app instance key is invalid. | 1116| 16000077 | The number of app instances reaches the limit. | 1117| 16000078 | The multi-instance is not supported. | 1118| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 1119| 16000080 | Creating a new instance is not supported. | 1120| 16200001 | The caller has been released. | 1121 1122**Example** 1123 1124```ts 1125import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 1126import { BusinessError } from '@kit.BasicServicesKit'; 1127 1128class EntryAbility extends ServiceExtensionAbility { 1129 onCreate(want: Want) { 1130 // want contains the information about the caller who starts the application. 1131 let localWant: Want = want; 1132 localWant.bundleName = 'com.example.demo'; 1133 localWant.moduleName = 'entry'; 1134 localWant.abilityName = 'TestAbility'; 1135 1136 let option: StartOptions = { 1137 displayId: 0 1138 }; 1139 1140 // Start a new ability using the caller information. 1141 this.context.startAbilityAsCaller(localWant, option) 1142 .then(() => { 1143 console.info('startAbilityAsCaller success.'); 1144 }) 1145 .catch((err: BusinessError) => { 1146 console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err)); 1147 }) 1148 } 1149} 1150``` 1151 1152## ServiceExtensionContext.stopServiceExtensionAbility 1153 1154stopServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void 1155 1156Stops a ServiceExtensionAbility in the same application. This API uses an asynchronous callback to return the result. 1157 1158**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1159 1160**System API**: This is a system API. 1161 1162**Parameters** 1163 1164| Name| Type| Mandatory| Description| 1165| -------- | -------- | -------- | -------- | 1166| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 1167| callback | AsyncCallback\<void\> | Yes| Callback used to return the result. If the ServiceExtensionAbility is stopped, **err** is **undefined**; otherwise, **err** is an error object.| 1168 1169**Error codes** 1170 1171For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1172 1173| ID| Error Message| 1174| ------- | -------- | 1175| 201 | The application does not have permission to call the interface. | 1176| 202 | The application is not system-app, can not use system-api. | 1177| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1178| 16000001 | The specified ability does not exist. | 1179| 16000002 | Incorrect ability type. | 1180| 16000004 | Cannot start an invisible component. | 1181| 16000005 | The specified process does not have the permission. | 1182| 16000006 | Cross-user operations are not allowed. | 1183| 16000011 | The context does not exist. | 1184| 16000050 | Internal error. | 1185| 16200001 | The caller has been released. | 1186 1187**Example** 1188 1189```ts 1190import { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 1191import { BusinessError } from '@kit.BasicServicesKit'; 1192 1193class EntryAbility extends ServiceExtensionAbility { 1194 onCreate() { 1195 let want: Want = { 1196 deviceId: '', 1197 bundleName: 'com.example.myapplication', 1198 abilityName: 'EntryAbility' 1199 }; 1200 1201 try { 1202 this.context.stopServiceExtensionAbility(want, (error: BusinessError) => { 1203 if (error.code) { 1204 // Process service logic errors. 1205 console.error(`stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 1206 return; 1207 } 1208 // Carry out normal service processing. 1209 console.info('stopServiceExtensionAbility succeed'); 1210 }); 1211 } catch (paramError) { 1212 // Process input parameter errors. 1213 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1214 } 1215 } 1216} 1217``` 1218 1219## ServiceExtensionContext.stopServiceExtensionAbility 1220 1221stopServiceExtensionAbility(want: Want): Promise\<void> 1222 1223Stops a ServiceExtensionAbility in the same application. This API uses a promise to return the result. 1224 1225**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1226 1227**System API**: This is a system API. 1228 1229**Parameters** 1230 1231| Name| Type| Mandatory| Description| 1232| -------- | -------- | -------- | -------- | 1233| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 1234 1235**Return value** 1236 1237| Type| Description| 1238| -------- | -------- | 1239| Promise<void> | Promise that returns no value.| 1240 1241**Error codes** 1242 1243For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1244 1245| ID| Error Message| 1246| ------- | -------- | 1247| 201 | The application does not have permission to call the interface. | 1248| 202 | The application is not system-app, can not use system-api. | 1249| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1250| 16000001 | The specified ability does not exist. | 1251| 16000002 | Incorrect ability type. | 1252| 16000004 | Cannot start an invisible component. | 1253| 16000005 | The specified process does not have the permission. | 1254| 16000006 | Cross-user operations are not allowed. | 1255| 16000011 | The context does not exist. | 1256| 16000050 | Internal error. | 1257| 16200001 | The caller has been released. | 1258 1259**Example** 1260 1261```ts 1262import { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 1263import { BusinessError } from '@kit.BasicServicesKit'; 1264 1265class EntryAbility extends ServiceExtensionAbility { 1266 onCreate() { 1267 let want: Want = { 1268 deviceId: '', 1269 bundleName: 'com.example.myapplication', 1270 abilityName: 'EntryAbility' 1271 }; 1272 1273 try { 1274 this.context.stopServiceExtensionAbility(want) 1275 .then(() => { 1276 // Carry out normal service processing. 1277 console.info('stopServiceExtensionAbility succeed'); 1278 }) 1279 .catch((error: BusinessError) => { 1280 // Process service logic errors. 1281 console.error(`stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 1282 }); 1283 } catch (paramError) { 1284 // Process input parameter errors. 1285 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1286 } 1287 } 1288} 1289``` 1290 1291## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount 1292 1293stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void 1294 1295Stops a ServiceExtensionAbility in the same application with the account ID specified. This API uses an asynchronous callback to return the result. 1296 1297> **NOTE** 1298> 1299> Permission verification is not required when **accountId** specifies the current user. 1300 1301**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1302 1303**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1304 1305**System API**: This is a system API. 1306 1307**Parameters** 1308 1309| Name| Type| Mandatory| Description| 1310| -------- | -------- | -------- | -------- | 1311| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 1312| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated).| 1313| callback | AsyncCallback\<void\> | Yes| Callback used to return the result. If the ServiceExtensionAbility is stopped, **err** is **undefined**; otherwise, **err** is an error object.| 1314 1315**Error codes** 1316 1317For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1318 1319| ID| Error Message| 1320| ------- | -------- | 1321| 201 | The application does not have permission to call the interface. | 1322| 202 | The application is not system-app, can not use system-api. | 1323| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1324| 16000001 | The specified ability does not exist. | 1325| 16000002 | Incorrect ability type. | 1326| 16000004 | Cannot start an invisible component. | 1327| 16000005 | The specified process does not have the permission. | 1328| 16000006 | Cross-user operations are not allowed. | 1329| 16000011 | The context does not exist. | 1330| 16000050 | Internal error. | 1331| 16200001 | The caller has been released. | 1332 1333**Example** 1334 1335```ts 1336import { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 1337import { BusinessError } from '@kit.BasicServicesKit'; 1338 1339class EntryAbility extends ServiceExtensionAbility { 1340 onCreate() { 1341 let want: Want = { 1342 deviceId: '', 1343 bundleName: 'com.example.myapplication', 1344 abilityName: 'EntryAbility' 1345 }; 1346 let accountId = 100; 1347 1348 try { 1349 this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error: BusinessError) => { 1350 if (error.code) { 1351 // Process service logic errors. 1352 console.error(`stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`); 1353 return; 1354 } 1355 // Carry out normal service processing. 1356 console.info('stopServiceExtensionAbilityWithAccount succeed'); 1357 }); 1358 } catch (paramError) { 1359 // Process input parameter errors. 1360 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1361 } 1362 } 1363} 1364``` 1365 1366## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount 1367 1368stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void> 1369 1370Stops a ServiceExtensionAbility in the same application with the account ID specified. This API uses a promise to return the result. 1371 1372> **NOTE** 1373> 1374> Permission verification is not required when **accountId** specifies the current user. 1375 1376**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1377 1378**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1379 1380**System API**: This is a system API. 1381 1382**Parameters** 1383 1384| Name| Type| Mandatory| Description| 1385| -------- | -------- | -------- | -------- | 1386| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 1387| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1).| 1388 1389**Return value** 1390 1391| Type| Description| 1392| -------- | -------- | 1393| Promise<void> | Promise that returns no value.| 1394 1395**Error codes** 1396 1397For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1398 1399| ID| Error Message| 1400| ------- | -------- | 1401| 201 | The application does not have permission to call the interface. | 1402| 202 | The application is not system-app, can not use system-api. | 1403| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1404| 16000001 | The specified ability does not exist. | 1405| 16000002 | Incorrect ability type. | 1406| 16000004 | Cannot start an invisible component. | 1407| 16000005 | The specified process does not have the permission. | 1408| 16000006 | Cross-user operations are not allowed. | 1409| 16000011 | The context does not exist. | 1410| 16000050 | Internal error. | 1411| 16200001 | The caller has been released. | 1412 1413**Example** 1414 1415```ts 1416import { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 1417import { BusinessError } from '@kit.BasicServicesKit'; 1418 1419class EntryAbility extends ServiceExtensionAbility { 1420 onCreate() { 1421 let want: Want = { 1422 deviceId: '', 1423 bundleName: 'com.example.myapplication', 1424 abilityName: 'EntryAbility' 1425 }; 1426 let accountId = 100; 1427 1428 try { 1429 this.context.stopServiceExtensionAbilityWithAccount(want, accountId) 1430 .then(() => { 1431 // Carry out normal service processing. 1432 console.info('stopServiceExtensionAbilityWithAccount succeed'); 1433 }) 1434 .catch((error: BusinessError) => { 1435 // Process service logic errors. 1436 console.error(`stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`); 1437 }); 1438 } catch (paramError) { 1439 // Process input parameter errors. 1440 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1441 } 1442 } 1443} 1444``` 1445 1446## ServiceExtensionContext.terminateSelf 1447 1448terminateSelf(callback: AsyncCallback<void>): void 1449 1450Terminates this ability. This API can be called only by the main thread. It uses an asynchronous callback to return the result. 1451 1452**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1453 1454**System API**: This is a system API. 1455 1456**Parameters** 1457 1458| Name| Type| Mandatory| Description| 1459| -------- | -------- | -------- | -------- | 1460| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the ability is terminated, **err** is **undefined**; otherwise, **err** is an error object.| 1461 1462**Error codes** 1463 1464For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1465 1466| ID| Error Message| 1467| ------- | -------- | 1468| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1469| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1470| 16000011 | The context does not exist. | 1471| 16000050 | Internal error. | 1472 1473**Example** 1474 1475```ts 1476import { ServiceExtensionAbility } from '@kit.AbilityKit'; 1477import { BusinessError } from '@kit.BasicServicesKit'; 1478 1479class EntryAbility extends ServiceExtensionAbility { 1480 onCreate() { 1481 this.context.terminateSelf((error: BusinessError) => { 1482 if (error.code) { 1483 // Process service logic errors. 1484 console.error(`terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}`); 1485 return; 1486 } 1487 // Carry out normal service processing. 1488 console.info('terminateSelf succeed'); 1489 }); 1490 } 1491} 1492``` 1493 1494## ServiceExtensionContext.terminateSelf 1495 1496terminateSelf(): Promise<void> 1497 1498Terminates this ability. This API can be called only by the main thread. It uses a promise to return the result. 1499 1500**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1501 1502**System API**: This is a system API. 1503 1504**Return value** 1505 1506| Type| Description| 1507| -------- | -------- | 1508| Promise<void> | Promise that returns no value.| 1509 1510**Error codes** 1511 1512For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1513 1514| ID| Error Message| 1515| ------- | -------------------------------- | 1516| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1517| 16000011 | The context does not exist. | 1518| 16000050 | Internal error. | 1519 1520**Example** 1521 1522```ts 1523import { ServiceExtensionAbility } from '@kit.AbilityKit'; 1524import { BusinessError } from '@kit.BasicServicesKit'; 1525 1526class EntryAbility extends ServiceExtensionAbility { 1527 onCreate() { 1528 this.context.terminateSelf().then(() => { 1529 // Carry out normal service processing. 1530 console.info('terminateSelf succeed'); 1531 }).catch((error: BusinessError) => { 1532 // Process service logic errors. 1533 console.error(`terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}`); 1534 }); 1535 } 1536} 1537``` 1538 1539## ServiceExtensionContext.connectServiceExtensionAbility 1540 1541connectServiceExtensionAbility(want: Want, options: ConnectOptions): number 1542 1543Connects this ability to a ServiceExtensionAbility. This API can be called only by the main thread. 1544 1545> **NOTE** 1546> 1547> 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). 1548 1549**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1550 1551**System API**: This is a system API. 1552 1553**Parameters** 1554 1555| Name| Type| Mandatory| Description| 1556| -------- | -------- | -------- | -------- | 1557| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability, such as the ability name and bundle name.| 1558| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | Yes| Callback used to return the information indicating that the connection is successful, interrupted, or failed.| 1559 1560**Return value** 1561 1562| Type| Description| 1563| -------- | -------- | 1564| number | A number, based on which the connection will be interrupted.| 1565 1566**Error codes** 1567 1568For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1569 1570| ID| Error Message| 1571| ------- | -------- | 1572| 201 | The application does not have permission to call the interface. | 1573| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1574| 16000001 | The specified ability does not exist. | 1575| 16000002 | Incorrect ability type. | 1576| 16000004 | Cannot start an invisible component. | 1577| 16000005 | The specified process does not have the permission. | 1578| 16000006 | Cross-user operations are not allowed. | 1579| 16000008 | The crowdtesting application expires. | 1580| 16000053 | The ability is not on the top of the UI. | 1581| 16000055 | Installation-free timed out. | 1582| 16000011 | The context does not exist. | 1583| 16000050 | Internal error. | 1584 1585**Example** 1586 1587```ts 1588import { ServiceExtensionAbility, Want, common } from '@kit.AbilityKit'; 1589import { rpc } from '@kit.IPCKit'; 1590import { BusinessError } from '@kit.BasicServicesKit'; 1591 1592let commRemote: rpc.IRemoteObject; // Release the instance when the connection is disconnected. 1593 1594class EntryAbility extends ServiceExtensionAbility { 1595 onCreate() { 1596 let want: Want = { 1597 bundleName: 'com.example.myapp', 1598 abilityName: 'MyAbility' 1599 }; 1600 let options: common.ConnectOptions = { 1601 onConnect(elementName, remote) { 1602 commRemote = remote; 1603 console.info('----------- onConnect -----------'); 1604 }, 1605 onDisconnect(elementName) { 1606 console.info('----------- onDisconnect -----------'); 1607 }, 1608 onFailed(code) { 1609 console.error('----------- onFailed -----------'); 1610 } 1611 }; 1612 let connection: number; 1613 1614 try { 1615 connection = this.context.connectServiceExtensionAbility(want, options); 1616 } catch (paramError) { 1617 // Process input parameter errors. 1618 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1619 } 1620 } 1621} 1622``` 1623 1624## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount 1625 1626connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number 1627 1628Connects this ability to a ServiceExtensionAbility of a given account. This API can be called only by the main thread. 1629 1630Currently, this API takes effect only on phones and tablets. 1631 1632> **NOTE** 1633> 1634> 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). 1635> Permission verification is not required when **accountId** specifies the current user. 1636 1637**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1638 1639**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1640 1641**System API**: This is a system API. 1642 1643**Parameters** 1644 1645| Name| Type| Mandatory| Description| 1646| -------- | -------- | -------- | -------- | 1647| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 1648| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated).| 1649| options | ConnectOptions | Yes| Remote object instance.| 1650 1651**Return value** 1652 1653| Type| Description| 1654| -------- | -------- | 1655| number | Result code of the connection.| 1656 1657**Error codes** 1658 1659For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1660 1661| ID| Error Message| 1662| ------- | -------- | 1663| 201 | The application does not have permission to call the interface. | 1664| 202 | The application is not system-app, can not use system-api. | 1665| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1666| 16000001 | The specified ability does not exist. | 1667| 16000002 | Incorrect ability type. | 1668| 16000004 | Cannot start an invisible component. | 1669| 16000005 | The specified process does not have the permission. | 1670| 16000006 | Cross-user operations are not allowed. | 1671| 16000008 | The crowdtesting application expires. | 1672| 16000053 | The ability is not on the top of the UI. | 1673| 16000055 | Installation-free timed out. | 1674| 16000011 | The context does not exist. | 1675| 16000050 | Internal error. | 1676 1677**Example** 1678 1679```ts 1680import { ServiceExtensionAbility, Want, common } from '@kit.AbilityKit'; 1681import { rpc } from '@kit.IPCKit'; 1682import { BusinessError } from '@kit.BasicServicesKit'; 1683 1684let commRemote: rpc.IRemoteObject; // Release the instance when the connection is disconnected. 1685 1686class EntryAbility extends ServiceExtensionAbility { 1687 onCreate() { 1688 let want: Want = { 1689 deviceId: '', 1690 bundleName: 'com.example.myapplication', 1691 abilityName: 'EntryAbility' 1692 }; 1693 let accountId = 100; 1694 let options: common.ConnectOptions = { 1695 onConnect(elementName, remote) { 1696 commRemote = remote; 1697 console.info('----------- onConnect -----------'); 1698 }, 1699 onDisconnect(elementName) { 1700 console.info('----------- onDisconnect -----------'); 1701 }, 1702 onFailed(code) { 1703 console.info('----------- onFailed -----------'); 1704 } 1705 }; 1706 let connection: number; 1707 1708 try { 1709 connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options); 1710 } catch (paramError) { 1711 // Process input parameter errors. 1712 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1713 } 1714 } 1715} 1716``` 1717 1718## ServiceExtensionContext.disconnectServiceExtensionAbility 1719 1720disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback<void>): void 1721 1722Disconnects this ability from a ServiceExtensionAbility and after the successful disconnection, sets the remote object returned upon the connection to void. This API can be called only by the main thread. It uses an asynchronous callback to return the result. 1723 1724**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1725 1726**System API**: This is a system API. 1727 1728**Parameters** 1729 1730| Name| Type| Mandatory| Description| 1731| -------- | -------- | -------- | -------- | 1732| connection | number | Yes| Number returned after **connectServiceExtensionAbility** is called.| 1733| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the ability is disconnected, **err** is **undefined**; otherwise, **err** is an error object.| 1734 1735**Error codes** 1736 1737For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1738 1739| ID| Error Message| 1740| ------- | -------- | 1741| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1742| 16000011 | The context does not exist. | 1743| 16000050 | Internal error. | 1744 1745**Example** 1746 1747```ts 1748import { ServiceExtensionAbility } from '@kit.AbilityKit'; 1749import { rpc } from '@kit.IPCKit'; 1750import { BusinessError } from '@kit.BasicServicesKit'; 1751 1752let commRemote: rpc.IRemoteObject | null; // Release the instance when the connection is disconnected. 1753 1754class EntryAbility extends ServiceExtensionAbility { 1755 onCreate() { 1756 // connection is the return value of connectServiceExtensionAbility. 1757 let connection = 1; 1758 try { 1759 this.context.disconnectServiceExtensionAbility(connection, (error: BusinessError) => { 1760 commRemote = null; 1761 if (error.code) { 1762 // Process service logic errors. 1763 console.error(`disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 1764 return; 1765 } 1766 // Carry out normal service processing. 1767 console.info('disconnectServiceExtensionAbility succeed'); 1768 }); 1769 } catch (paramError) { 1770 commRemote = null; 1771 // Process input parameter errors. 1772 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1773 } 1774 } 1775} 1776``` 1777 1778## ServiceExtensionContext.disconnectServiceExtensionAbility 1779 1780disconnectServiceExtensionAbility(connection: number): Promise<void> 1781 1782Disconnects this ability from a ServiceExtensionAbility and after the successful disconnection, sets the remote object returned upon the connection to void. This API can be called only by the main thread. It uses a promise to return the result. 1783 1784**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1785 1786**System API**: This is a system API. 1787 1788**Parameters** 1789 1790| Name| Type| Mandatory| Description| 1791| -------- | -------- | -------- | -------- | 1792| connection | number | Yes| Number returned after **connectServiceExtensionAbility** is called.| 1793 1794**Return value** 1795 1796| Type| Description| 1797| -------- | -------- | 1798| Promise<void> | Promise that returns no value.| 1799 1800**Error codes** 1801 1802For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1803 1804| ID| Error Message| 1805| ------- | -------- | 1806| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1807| 16000011 | The context does not exist. | 1808| 16000050 | Internal error. | 1809 1810**Example** 1811 1812```ts 1813import { ServiceExtensionAbility } from '@kit.AbilityKit'; 1814import { rpc } from '@kit.IPCKit'; 1815import { BusinessError } from '@kit.BasicServicesKit'; 1816 1817let commRemote: rpc.IRemoteObject | null; // Release the instance when the connection is disconnected. 1818 1819class EntryAbility extends ServiceExtensionAbility { 1820 onCreate() { 1821 // connection is the return value of connectServiceExtensionAbility. 1822 let connection = 1; 1823 try { 1824 this.context.disconnectServiceExtensionAbility(connection) 1825 .then(() => { 1826 commRemote = null; 1827 // Carry out normal service processing. 1828 console.info('disconnectServiceExtensionAbility succeed'); 1829 }) 1830 .catch((error: BusinessError) => { 1831 commRemote = null; 1832 // Process service logic errors. 1833 console.error(`disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 1834 }); 1835 } catch (paramError) { 1836 commRemote = null; 1837 // Process input parameter errors. 1838 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1839 } 1840 } 1841} 1842``` 1843 1844## ServiceExtensionContext.startAbilityByCall 1845 1846startAbilityByCall(want: Want): Promise<Caller> 1847 1848Starts an ability in the foreground or background and obtains the caller object for communicating with the ability. This API can be called only by the main thread. It uses a promise to return the result. 1849 1850This API cannot be used to start the UIAbility with the launch type set to [specified](../../application-models/uiability-launch-type.md#specified). 1851 1852Observe the following when using this API: 1853 - 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. 1854 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the ohos.permission.START_INVISIBLE_ABILITY permission. 1855 - 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). 1856 1857**Required permissions**: ohos.permission.ABILITY_BACKGROUND_COMMUNICATION 1858 1859**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1860 1861**System API**: This is a system API. 1862 1863**Parameters** 1864 1865| Name| Type| Mandatory| Description| 1866| -------- | -------- | -------- | -------- | 1867| 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.| 1868 1869**Return value** 1870 1871| Type| Description| 1872| -------- | -------- | 1873| Promise<Caller> | Promise used to return the caller object to communicate with.| 1874 1875**Error codes** 1876 1877For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1878 1879| ID| Error Message| 1880| ------- | -------- | 1881| 201 | The application does not have permission to call the interface. | 1882| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1883| 16000001 | The specified ability does not exist. | 1884| 16000002 | Incorrect ability type. | 1885| 16000004 | Cannot start an invisible component. | 1886| 16000006 | Cross-user operations are not allowed. | 1887| 16000008 | The crowdtesting application expires. | 1888| 16000011 | The context does not exist. | 1889| 16000050 | Internal error. | 1890| 16200001 | The caller has been released. | 1891 1892**Example** 1893 1894Start an ability in the background. 1895 1896```ts 1897import { ServiceExtensionAbility, Caller, Want } from '@kit.AbilityKit'; 1898import { BusinessError } from '@kit.BasicServicesKit'; 1899 1900class EntryAbility extends ServiceExtensionAbility { 1901 onCreate() { 1902 let caller: Caller; 1903 // Start an ability in the background by not passing parameters. 1904 let wantBackground: Want = { 1905 bundleName: 'com.example.myservice', 1906 moduleName: 'entry', 1907 abilityName: 'EntryAbility', 1908 deviceId: '' 1909 }; 1910 1911 try { 1912 this.context.startAbilityByCall(wantBackground) 1913 .then((obj: Caller) => { 1914 // Carry out normal service processing. 1915 caller = obj; 1916 console.info('startAbilityByCall succeed'); 1917 }).catch((error: BusinessError) => { 1918 // Process service logic errors. 1919 console.error(`startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}`); 1920 }); 1921 } catch (paramError) { 1922 // Process input parameter errors. 1923 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1924 } 1925 } 1926} 1927``` 1928 1929Start an ability in the foreground. 1930 1931```ts 1932import { ServiceExtensionAbility, Caller, Want } from '@kit.AbilityKit'; 1933import { BusinessError } from '@kit.BasicServicesKit'; 1934 1935class EntryAbility extends ServiceExtensionAbility { 1936 onCreate() { 1937 let caller: Caller; 1938 // Start an ability in the foreground with 'ohos.aafwk.param.callAbilityToForeground' in parameters set to true. 1939 let wantForeground: Want = { 1940 bundleName: 'com.example.myservice', 1941 moduleName: 'entry', 1942 abilityName: 'EntryAbility', 1943 deviceId: '', 1944 parameters: { 1945 'ohos.aafwk.param.callAbilityToForeground': true 1946 } 1947 }; 1948 1949 try { 1950 this.context.startAbilityByCall(wantForeground) 1951 .then((obj: Caller) => { 1952 // Carry out normal service processing. 1953 caller = obj; 1954 console.info('startAbilityByCall succeed'); 1955 }).catch((error: BusinessError) => { 1956 // Process service logic errors. 1957 console.error(`startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}`); 1958 }); 1959 } catch (paramError) { 1960 // Process input parameter errors. 1961 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1962 } 1963 } 1964} 1965``` 1966## ServiceExtensionContext.startRecentAbility 1967 1968startRecentAbility(want: Want, callback: AsyncCallback\<void>): void 1969 1970Starts an ability. If the ability has multiple instances, the latest instance is started. This API can be called only by the main thread. It uses an asynchronous callback to return the result. 1971 1972> **NOTE** 1973> 1974> 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). 1975 1976**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1977 1978**System API**: This is a system API. 1979 1980**Parameters** 1981 1982| Name| Type| Mandatory| Description| 1983| -------- | -------- | -------- | -------- | 1984| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 1985| callback | AsyncCallback\<void> | Yes| Callback used to return the result. If the ability is started, **err** is **undefined**; otherwise, **err** is an error object.| 1986 1987**Error codes** 1988 1989For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1990 1991| ID| Error Message| 1992| ------- | -------- | 1993| 201 | The application does not have permission to call the interface. | 1994| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1995| 16000001 | The specified ability does not exist. | 1996| 16000002 | Incorrect ability type. | 1997| 16000004 | Cannot start an invisible component. | 1998| 16000005 | The specified process does not have the permission. | 1999| 16000006 | Cross-user operations are not allowed. | 2000| 16000008 | The crowdtesting application expires. | 2001| 16000009 | An ability cannot be started or stopped in Wukong mode. | 2002| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 2003| 16000011 | The context does not exist. | 2004| 16000050 | Internal error. | 2005| 16000053 | The ability is not on the top of the UI. | 2006| 16000055 | Installation-free timed out. | 2007| 16000071 | App clone is not supported. | 2008| 16000072 | App clone or multi-instance is not supported. | 2009| 16000073 | The app clone index is invalid. | 2010| 16000076 | The app instance key is invalid. | 2011| 16000077 | The number of app instances reaches the limit. | 2012| 16000078 | The multi-instance is not supported. | 2013| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 2014| 16000080 | Creating a new instance is not supported. | 2015| 16200001 | The caller has been released. | 2016 2017**Example** 2018 2019```ts 2020import { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 2021import { BusinessError } from '@kit.BasicServicesKit'; 2022 2023class EntryAbility extends ServiceExtensionAbility { 2024 onCreate() { 2025 let want: Want = { 2026 bundleName: 'com.example.myapplication', 2027 abilityName: 'EntryAbility' 2028 }; 2029 2030 try { 2031 this.context.startRecentAbility(want, (err: BusinessError) => { 2032 if (err.code) { 2033 // Process service logic errors. 2034 console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); 2035 return; 2036 } 2037 // Carry out normal service processing. 2038 console.info('startRecentAbility succeed'); 2039 }); 2040 } catch (err) { 2041 // Process input parameter errors. 2042 let code = (err as BusinessError).code; 2043 let message = (err as BusinessError).message; 2044 console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); 2045 } 2046 } 2047} 2048``` 2049## ServiceExtensionContext.startRecentAbility 2050 2051startRecentAbility(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void 2052 2053Starts an ability. If the ability has multiple instances, the latest instance is started. This API can be called only by the main thread. It uses an asynchronous callback to return the result. 2054 2055You can use this API to carry start options. 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. 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. If the ability is started, **err** is **undefined**; otherwise, **err** is an error object.| 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| 16000004 | Cannot start an invisible component. | 2083| 16000005 | The specified process does not have the permission. | 2084| 16000006 | Cross-user operations are not allowed. | 2085| 16000008 | The crowdtesting application expires. | 2086| 16000009 | An ability cannot be started or stopped in Wukong mode. | 2087| 16000011 | The context does not exist. | 2088| 16000050 | Internal error. | 2089| 16000053 | The ability is not on the top of the UI. | 2090| 16000055 | Installation-free timed out. | 2091| 16000071 | App clone is not supported. | 2092| 16000072 | App clone or multi-instance is not supported. | 2093| 16000073 | The app clone index is invalid. | 2094| 16000076 | The app instance key is invalid. | 2095| 16000077 | The number of app instances reaches the limit. | 2096| 16000078 | The multi-instance is not supported. | 2097| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 2098| 16000080 | Creating a new instance is not supported. | 2099| 16200001 | The caller has been released. | 2100 2101**Example** 2102 2103```ts 2104import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 2105import { BusinessError } from '@kit.BasicServicesKit'; 2106 2107class EntryAbility extends ServiceExtensionAbility { 2108 onCreate() { 2109 let want: Want = { 2110 deviceId: '', 2111 bundleName: 'com.example.myapplication', 2112 abilityName: 'EntryAbility' 2113 }; 2114 let options: StartOptions = { 2115 windowMode: 0 2116 }; 2117 2118 try { 2119 this.context.startRecentAbility(want, options, (err: BusinessError) => { 2120 if (err.code) { 2121 // Process service logic errors. 2122 console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); 2123 return; 2124 } 2125 // Carry out normal service processing. 2126 console.info('startRecentAbility succeed'); 2127 }); 2128 } catch (err) { 2129 // Process input parameter errors. 2130 let code = (err as BusinessError).code; 2131 let message = (err as BusinessError).message; 2132 console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); 2133 } 2134 } 2135} 2136``` 2137## ServiceExtensionContext.startRecentAbility 2138 2139startRecentAbility(want: Want, options?: StartOptions): Promise\<void> 2140 2141Starts an ability. If the ability has multiple instances, the latest instance is started. This API uses a promise to return the result. It can be called only by the main thread. 2142 2143> **NOTE** 2144> 2145> 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). 2146 2147**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2148 2149**System API**: This is a system API. 2150 2151**Parameters** 2152 2153| Name| Type| Mandatory| Description| 2154| -------- | -------- | -------- | -------- | 2155| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 2156| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 2157 2158**Error codes** 2159 2160For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2161 2162| ID| Error Message| 2163| ------- | -------- | 2164| 201 | The application does not have permission to call the interface. | 2165| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2166| 16000001 | The specified ability does not exist. | 2167| 16000002 | Incorrect ability type. | 2168| 16000004 | Cannot start an invisible component. | 2169| 16000005 | The specified process does not have the permission. | 2170| 16000006 | Cross-user operations are not allowed. | 2171| 16000008 | The crowdtesting application expires. | 2172| 16000009 | An ability cannot be started or stopped in Wukong mode. | 2173| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 2174| 16000011 | The context does not exist. | 2175| 16000050 | Internal error. | 2176| 16000053 | The ability is not on the top of the UI. | 2177| 16000055 | Installation-free timed out. | 2178| 16000071 | App clone is not supported. | 2179| 16000072 | App clone or multi-instance is not supported. | 2180| 16000073 | The app clone index is invalid. | 2181| 16000076 | The app instance key is invalid. | 2182| 16000077 | The number of app instances reaches the limit. | 2183| 16000078 | The multi-instance is not supported. | 2184| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 2185| 16000080 | Creating a new instance is not supported. | 2186| 16200001 | The caller has been released. | 2187 2188**Example** 2189 2190```ts 2191import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 2192import { BusinessError } from '@kit.BasicServicesKit'; 2193 2194class EntryAbility extends ServiceExtensionAbility { 2195 onCreate() { 2196 let want: Want = { 2197 bundleName: 'com.example.myapplication', 2198 abilityName: 'EntryAbility' 2199 }; 2200 let options: StartOptions = { 2201 windowMode: 0, 2202 }; 2203 2204 try { 2205 this.context.startRecentAbility(want, options) 2206 .then(() => { 2207 // Carry out normal service processing. 2208 console.info('startRecentAbility succeed'); 2209 }) 2210 .catch((err: BusinessError) => { 2211 // Process service logic errors. 2212 console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); 2213 }); 2214 } catch (err) { 2215 // Process input parameter errors. 2216 let code = (err as BusinessError).code; 2217 let message = (err as BusinessError).message; 2218 console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); 2219 } 2220 } 2221} 2222``` 2223 2224## ServiceExtensionContext.startAbilityByCallWithAccount<sup>10+</sup> 2225 2226startAbilityByCallWithAccount(want: Want, accountId: number): Promise<Caller> 2227 2228Starts an ability with the account ID specified and obtains the caller object for communicating with the ability. This API can be called only by the main thread. It uses a promise to return the result. 2229 2230This API cannot be used to start the UIAbility with the launch type set to [specified](../../application-models/uiability-launch-type.md#specified). 2231 2232Observe the following when using this API: 2233 - 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. 2234 - 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. 2235 - If **exported** of the target ability is **false** in cross-application scenarios, the caller must have the ohos.permission.START_INVISIBLE_ABILITY permission. 2236 - 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). 2237 2238**Required permissions**: ohos.permission.ABILITY_BACKGROUND_COMMUNICATION and ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 2239 2240**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2241 2242**System API**: This is a system API. 2243 2244**Parameters** 2245 2246| Name| Type| Mandatory| Description| 2247| -------- | -------- | -------- | -------- | 2248| 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.| 2249| 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).| 2250 2251**Return value** 2252 2253| Type| Description| 2254| -------- | -------- | 2255| Promise<Caller> | Promise used to return the caller object to communicate with.| 2256 2257**Error codes** 2258 2259For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2260 2261| ID| Error Message| 2262| ------- | -------- | 2263| 201 | The application does not have permission to call the interface. | 2264| 202 | The application is not system-app, can not use system-api. | 2265| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2266| 16000001 | The specified ability does not exist. | 2267| 16000002 | Incorrect ability type. | 2268| 16000004 | Cannot start an invisible component. | 2269| 16000005 | Static permission denied. The specified process does not have the permission. | 2270| 16000006 | Cross-user operations are not allowed. | 2271| 16000008 | The crowdtesting application expires. | 2272| 16000011 | The context does not exist. | 2273| 16000012 | The application is controlled. | 2274| 16000013 | The application is controlled by EDM. | 2275| 16000050 | Internal error. | 2276| 16200001 | The caller has been released. | 2277 2278**Example** 2279 2280```ts 2281import { ServiceExtensionAbility, Want, Caller } from '@kit.AbilityKit'; 2282import { BusinessError } from '@kit.BasicServicesKit'; 2283 2284class EntryAbility extends ServiceExtensionAbility { 2285 onCreate() { 2286 let caller: Caller; 2287 // ID of a system account. The value -1 indicates the current user. 2288 let accountId = -1; 2289 // Specify the ability to start. 2290 let want: Want = { 2291 bundleName: 'com.acts.actscalleeabilityrely', 2292 moduleName: 'entry', 2293 abilityName: 'EntryAbility', 2294 deviceId: '', 2295 parameters: { 2296 // 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. 2297 'ohos.aafwk.param.callAbilityToForeground': true 2298 } 2299 }; 2300 2301 try { 2302 this.context.startAbilityByCallWithAccount(want, accountId) 2303 .then((obj: Caller) => { 2304 // Carry out normal service processing. 2305 caller = obj; 2306 console.info('startAbilityByCallWithAccount succeed'); 2307 }).catch((error: BusinessError) => { 2308 // Process service logic errors. 2309 console.error(`startAbilityByCallWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`); 2310 }); 2311 } catch (paramError) { 2312 // Process input parameter errors. 2313 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 2314 } 2315 } 2316} 2317``` 2318 2319## ServiceExtensionContext.requestModalUIExtension<sup>11+<sup> 2320 2321requestModalUIExtension(pickerWant: Want): Promise\<void> 2322 2323Requests 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 can be called only by the main thread. It uses a promise to return the result. 2324 2325Before 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. 2326 2327> **NOTE** 2328> 2329> 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). 2330 2331**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2332 2333**System API**: This is a system API. 2334 2335**Parameters** 2336 2337| Name| Type| Mandatory| Description| 2338| -------- | -------- | -------- | -------- | 2339| pickerWant | [Want](js-apis-app-ability-want.md) | Yes| Want information used to start the UIExtensionAbility.| 2340 2341**Return value** 2342 2343| Type| Description| 2344| -------- | -------- | 2345| Promise<void> | Promise that returns no value.| 2346 2347**Error codes** 2348 2349For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2350 2351| ID| Error Message| 2352| ------- | -------- | 2353| 202 | The application is not system-app, can not use system-api. | 2354| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2355| 16000050 | Internal error. | 2356 2357**Example** 2358 2359```ts 2360import { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 2361import { BusinessError } from '@kit.BasicServicesKit'; 2362 2363class ServiceExtension extends ServiceExtensionAbility { 2364 onCreate() { 2365 let pickerWant: Want = { 2366 bundleName: 'com.example.myapplication', 2367 abilityName: 'UIExtAbility', 2368 moduleName: 'entry_test', 2369 parameters: { 2370 'bundleName': 'com.example.myapplication', 2371 // The value is the same as the value of type configured for com.example.myapplication.UIExtAbility. 2372 'ability.want.params.uiExtensionType': 'sys/commonUI' 2373 } 2374 }; 2375 2376 try { 2377 this.context.requestModalUIExtension(pickerWant) 2378 .then(() => { 2379 // Carry out normal service processing. 2380 console.info('requestModalUIExtension succeed'); 2381 }) 2382 .catch((err: BusinessError) => { 2383 // Process service logic errors. 2384 console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`); 2385 }); 2386 } catch (err) { 2387 // Process input parameter errors. 2388 let code = (err as BusinessError).code; 2389 let message = (err as BusinessError).message; 2390 console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`); 2391 } 2392 } 2393} 2394``` 2395 2396## ServiceExtensionContext.requestModalUIExtension<sup>11+<sup> 2397 2398requestModalUIExtension(pickerWant: Want, callback: AsyncCallback\<void>): void 2399 2400Requests 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 can be called only by the main thread. It uses an asynchronous callback to return the result. 2401 2402Before 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. 2403 2404> **NOTE** 2405> 2406> 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). 2407 2408**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2409 2410**System API**: This is a system API. 2411 2412**Parameters** 2413 2414| Name| Type| Mandatory| Description| 2415| -------- | -------- | -------- | -------- | 2416| pickerWant | [Want](js-apis-app-ability-want.md) | Yes| Want information used to start the UIExtensionAbility.| 2417| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the UIExtensionAbility is started, **err** is **undefined**; otherwise, **err** is an error object.| 2418 2419**Error codes** 2420 2421For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2422 2423| ID| Error Message| 2424| ------- | -------- | 2425| 202 | The application is not system-app, can not use system-api. | 2426| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2427| 16000050 | Internal error. | 2428 2429**Example** 2430 2431```ts 2432import { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 2433import { BusinessError } from '@kit.BasicServicesKit'; 2434 2435class ServiceExtension extends ServiceExtensionAbility { 2436 onCreate() { 2437 let pickerWant: Want = { 2438 bundleName: 'com.example.myapplication', 2439 abilityName: 'com.example.myapplication.UIExtAbility', 2440 moduleName: 'entry_test', 2441 parameters: { 2442 'bundleName': 'com.example.myapplication', 2443 // The value is the same as the value of type configured for com.example.myapplication.UIExtAbility. 2444 'ability.want.params.uiExtensionType': 'sys/commonUI' 2445 } 2446 }; 2447 2448 try { 2449 this.context.requestModalUIExtension(pickerWant, (err: BusinessError) => { 2450 if (err.code) { 2451 // Process service logic errors. 2452 console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`); 2453 return; 2454 } 2455 // Carry out normal service processing. 2456 console.info('requestModalUIExtension succeed'); 2457 }); 2458 } catch (err) { 2459 // Process input parameter errors. 2460 let code = (err as BusinessError).code; 2461 let message = (err as BusinessError).message; 2462 console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`); 2463 } 2464 } 2465} 2466``` 2467 2468## ServiceExtensionContext.openLink<sup>12+<sup> 2469openLink(link:string, options?: OpenLinkOptions): Promise<void> 2470 2471Starts a UIAbility through App Linking. This API can be called only by the main thread. It uses a promise to return the result. 2472 2473A 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: 2474- The **actions** field contains **ohos.want.action.viewData**. 2475- The **entities** field contains **entity.system.browsable**. 2476- The **uris** field contains elements whose **scheme** is **https** and **domainVerify** is **true**. 2477 2478If 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. 2479 2480> **NOTE** 2481> 2482> 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). 2483 2484**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2485 2486**System API**: This is a system API. 2487 2488**Parameters** 2489 2490| Name| Type| Mandatory| Description| 2491| -------- | -------- | -------- | -------- | 2492| link | string | Yes| URL to open, which must be in the standard format.| 2493| options | [OpenLinkOptions](js-apis-app-ability-openLinkOptions.md) | No| Options of the URL.| 2494 2495**Return value** 2496 2497| Type| Description| 2498| -------- | -------- | 2499| Promise<void> | Promise that returns no value.| 2500 2501**Error codes** 2502 2503For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2504 2505| ID| Error Message| 2506| ------- | -------- | 2507| 201 | The application does not have permission to call the interface. | 2508| 202 | The application is not system-app, can not use system-api. | 2509| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2510| 16000001 | The specified ability does not exist. | 2511| 16000002 | Incorrect ability type. | 2512| 16000004 | Cannot start an invisible component. | 2513| 16000005 | The specified process does not have the permission. | 2514| 16000006 | Cross-user operations are not allowed. | 2515| 16000008 | The crowdtesting application expires. | 2516| 16000009 | An ability cannot be started or stopped in Wukong mode. | 2517| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 2518| 16000011 | The context does not exist. | 2519| 16000012 | The application is controlled. | 2520| 16000013 | The application is controlled by EDM. | 2521| 16000019 | No matching ability is found. | 2522| 16200001 | The caller has been released. | 2523 2524**Example** 2525 2526```ts 2527import { ServiceExtensionAbility, Want, OpenLinkOptions } from '@kit.AbilityKit'; 2528import { BusinessError } from '@kit.BasicServicesKit'; 2529 2530function log(info: string) { 2531 console.error(`[ServiceExtApp]:: ${JSON.stringify(info)}`); 2532} 2533 2534export default class ServiceExtAbility extends ServiceExtensionAbility { 2535 onCreate(want: Want) { 2536 log(`ServiceExtAbility OnCreate`); 2537 } 2538 2539 onRequest(want: Want, startId: number) { 2540 log(`ServiceExtAbility onRequest`); 2541 let link: string = 'https://www.example.com'; 2542 let openLinkOptions: OpenLinkOptions = { 2543 appLinkingOnly: false 2544 }; 2545 try { 2546 this.context.openLink( 2547 link, 2548 openLinkOptions 2549 ).then(() => { 2550 log(`open link success.`); 2551 }).catch((err: BusinessError) => { 2552 log(`open link failed, errCode ${JSON.stringify(err.code)}`); 2553 }); 2554 } 2555 catch (e) { 2556 log(`exception occured, errCode ${JSON.stringify(e.code)}`); 2557 } 2558 } 2559 2560 onDestroy() { 2561 log(`ServiceExtAbility onDestroy`); 2562 } 2563} 2564``` 2565 2566## ServiceExtensionContext.preStartMission<sup>12+<sup> 2567 2568preStartMission(bundleName:string, moduleName: string, abilityName: string, startTime: string): Promise<void> 2569 2570Starts an atomic service and pre-opens the window, with the loading box skipped. This API uses a promise to return the result. 2571 2572If parameter verification is successful but the atomic service fails to start, you need to implement an exception mechanism to capture the error. 2573 2574**Required permissions**: ohos.permission.PRE_START_ATOMIC_SERVICE 2575 2576**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2577 2578**System API**: This is a system API. 2579 2580**Parameters** 2581 2582| Name| Type| Mandatory| Description| 2583| -------- | -------- | -------- | -------- | 2584| bundleName | string | Yes| Bundle name of the atomic service.| 2585| moduleName | string | Yes| Module name of the atomic service.| 2586| abilityName | string | Yes| Ability name of the atomic service.| 2587| startTime | string | Yes| Start time to open the atomic service, in milliseconds.| 2588 2589**Return value** 2590 2591| Type| Description| 2592| -------- | -------- | 2593| Promise<void> | Promise that returns no value.| 2594 2595**Error codes** 2596 2597For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2598 2599| ID| Error Message| 2600| ------- | -------- | 2601| 201 | The application does not have permission to call the interface. | 2602| 202 | The application is not system-app, can not use system-api. | 2603| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2604| 16300007 | The target free-installation task does not exist. | 2605| 16000011 | The context does not exist. | 2606 2607**Example** 2608 2609```ts 2610import { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 2611import { BusinessError } from '@kit.BasicServicesKit'; 2612 2613function log(info: string) { 2614 console.error(`[ServiceExtApp]:: ${JSON.stringify(info)}`); 2615} 2616 2617export default class ServiceExtAbility extends ServiceExtensionAbility { 2618 onCreate(want: Want) { 2619 log(`ServiceExtAbility OnCreate`); 2620 } 2621 2622 onRequest(want: Want, startId: number) { 2623 log(`ServiceExtAbility onRequest`); 2624 try { 2625 this.context.preStartMission( 2626 want.bundleName, 2627 want.moduleName, 2628 want.abilityName, 2629 want.parameters["ohos.aafwk.param.startTime"] 2630 ).then(() => { 2631 log(`pre-start mission success.`); 2632 }).catch((err: BusinessError) => { 2633 log(`pre-start mission failed, errCode ${JSON.stringify(err.code)}`); 2634 }); 2635 } 2636 catch (e) { 2637 log(`exception occured, errCode ${JSON.stringify(e.code)}`); 2638 } 2639 } 2640 2641 onDestroy() { 2642 log(`ServiceExtAbility onDestroy`); 2643 } 2644} 2645``` 2646 2647## ServiceExtensionContext.startUIServiceExtensionAbility<sup>14+<sup> 2648 2649startUIServiceExtensionAbility(want: Want): Promise<void> 2650 2651Starts a new [UIServiceExtensionAbility](js-apis-app-ability-uiServiceExtensionAbility-sys.md). This API uses a promise to return the result. 2652 2653> **NOTE** 2654> 2655> 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). 2656 2657**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2658 2659**System API**: This is a system API. 2660 2661**Parameters** 2662| Name| Type| Read Only| Optional| Description | 2663| ------ | ---- | ---- | -------------------- | -------------------- | 2664| want | [Want](js-apis-app-ability-want.md) | Yes | No| Want information about the target ability, such as the ability name and bundle name.| 2665 2666**Return value** 2667 2668| Type | Description | 2669| ------------------- | -------------------------------------- | 2670| Promise<void> | Promise that returns no value.| 2671 2672**Error codes** 2673 2674For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2675| ID| Error Message | 2676| -------- | ---------------------------------------------------------------------| 2677| 201 | The application does not have permission to call the interface. | 2678| 202 | The application is not system-app, can not use system-api. | 2679| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2680| 801 | The Ability is not supported. | 2681| 16000001 | The specified ability does not exist. | 2682| 16000002 | Incorrect ability type. | 2683| 16000004 | Cannot start an invisible component. | 2684| 16000005 | The specified process does not have the permission. | 2685| 16000006 | Cross-user operations are not allowed. | 2686| 16000008 | The crowdtesting application expires. | 2687| 16000011 | The context does not exist. | 2688| 16000012 | The application is controlled. | 2689| 16000013 | The application is controlled by EDM. | 2690| 16000019 | No matching ability is found. | 2691| 16000050 | Internal error. | 2692| 16200001 | The caller has been released. | 2693 2694**Example** 2695 2696```ts 2697import { BusinessError } from '@ohos.base'; 2698import { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 2699 2700export default class MyServiceExtensionAbility extends ServiceExtensionAbility { 2701 onRequest(want: Want, startId: number) { 2702 const startWant: Want = { 2703 bundleName: 'com.example.myapplication', 2704 abilityName: 'UIServiceExtensionAbility' 2705 } 2706 // Start a UIServiceExtensionAbility. 2707 this.context.startUIServiceExtensionAbility(startWant).then(() => { 2708 console.info('succeeded'); 2709 }).catch((error: BusinessError) => { 2710 console.error(`error code: ${error.code}, error message : ${error.message}`); 2711 }) 2712 } 2713} 2714``` 2715 2716## ServiceExtensionContext.openAtomicService<sup>18+<sup> 2717openAtomicService(appId: string, options?: AtomicServiceOptions): Promise<void> 2718 2719Starts an atomic service based on an application ID. This API uses a promise to return the result. 2720 2721> **NOTE** 2722> 2723> 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). 2724 2725**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2726 2727**System API**: This is a system API. 2728 2729**Parameters** 2730 2731| Name| Type| Mandatory| Description| 2732| -------- | -------- | -------- | -------- | 2733| appId | string | Yes| Unique ID of the application, which is allocated by the cloud.| 2734| options | [AtomicServiceOptions](js-apis-app-ability-atomicServiceOptions.md) | No| Parameter carried in the request for starting the atomic service in jump-out mode.| 2735 2736**Return value** 2737 2738| Type| Description| 2739| -------- | -------- | 2740| Promise<void> | Promise that returns no value.| 2741 2742**Error codes** 2743 2744For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2745 2746| ID| Error Message | 2747| -------- | ------------------------------------------------------------ | 2748| 201 | The application does not have permission to call the interface. | 2749| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2750| 16000002 | Incorrect ability type. | 2751| 16000004 | Cannot start an invisible component. | 2752| 16000011 | The context does not exist. | 2753| 16000012 | The application is controlled. | 2754| 16000050 | Internal error. | 2755| 16200001 | The caller has been released. | 2756 2757**Example** 2758 2759```ts 2760import { ServiceExtensionAbility, AtomicServiceOptions } from '@kit.AbilityKit'; 2761import { BusinessError } from '@kit.BasicServicesKit'; 2762 2763export default class ServiceExtension extends ServiceExtensionAbility { 2764 onRequest(want: Want, startId: number) { 2765 let appId: string = '6918661953712445909'; 2766 let options: AtomicServiceOptions = { 2767 displayId: 0, 2768 }; 2769 try { 2770 this.context.openAtomicService(appId, options) 2771 .then(() => { 2772 console.info('openAtomicService succeed'); 2773 }) 2774 .catch((err: BusinessError) => { 2775 console.error(`openAtomicService failed, code is ${err.code}, message is ${err.message}`); 2776 }); 2777 } catch (err) { 2778 let code = (err as BusinessError).code; 2779 let message = (err as BusinessError).message; 2780 console.error(`openAtomicService failed, code is ${code}, message is ${message}`); 2781 } 2782 } 2783} 2784``` 2785 2786## ServiceExtensionContext.startUIAbilities<sup>20+</sup> 2787 2788startUIAbilities(wantList: Array\<Want>): Promise\<void> 2789 2790Starts multiple UIAbilities simultaneously. This API uses a promise to return the result. 2791 2792You can pass the Want information of multiple UIAbility instances, which can point to one or more applications. If all the UIAbility instances can be started successfully, the system displays these UIAbility instances in multiple windows simultaneously. Depending on the window handling, different devices may have varying display effects (including window shape, quantity, and layout). 2793 2794> **NOTE** 2795> 2796> This API takes effect only on phones and tablets. 2797> 2798> 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). 2799 2800**System API**: This is a system API. 2801 2802**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2803 2804**Parameters** 2805 2806| Name| Type| Mandatory| Description| 2807| ------ | ------ | ------ | ------ | 2808| wantList | Array\<[Want](js-apis-app-ability-want.md)> | Yes| List of launch parameters for multiple UIAbility components to be started simultaneously. A maximum of four Want objects can be passed. The **Want** parameter does not support implicit launch, cross-user launch, distributed launch, instant installation, or on-demand loading. By default, the main application is launched unless specified otherwise.| 2809 2810**Return value** 2811 2812| Type| Description| 2813| -------- | -------- | 2814| Promise<void> | Promise that returns no value.| 2815 2816**Error codes** 2817 2818For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2819 2820| ID| Error Message| 2821| ------ | ------ | 2822| 201 | The application does not have permission to call the interface. | 2823| 202 | Not system application. | 2824| 801 | Capability not supported. | 2825| 16000001 | The specified ability does not exist. | 2826| 16000004 | Failed to start the invisible ability. | 2827| 16000005 | The specified process does not have the permission. | 2828| 16000006 | Cross-user operations are not allowed. | 2829| 16000008 | The crowdtesting application expires. | 2830| 16000009 | An ability cannot be started or stopped in Wukong mode. | 2831| 16000011 | The context does not exist. | 2832| 16000050 | Internal error. | 2833| 16200001 | The caller has been released. | 2834| 16000073 | The app clone index is invalid. | 2835| 16000076 | The app instance key is invalid. | 2836| 16000080 | Creating a new instance is not supported. | 2837| 16000120 | A maximum of four UIAbility instances can be started simultaneously. The current parameter exceeds the maximum number or is less than 1.| 2838| 16000121 | The target component type is not a UIAbility. | 2839| 16000122 | The target component is blocked by the system module and does not support startup. | 2840| 16000123 | Implicit startup is not supported. | 2841| 16000124 | Starting a remote UIAbility is not supported. | 2842| 16000125 | Starting a plugin UIAbility is not supported. | 2843| 16000126 | Starting DLP files is not supported. | 2844 2845**Example** 2846 2847```ts 2848import { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 2849import { BusinessError } from '@kit.BasicServicesKit'; 2850 2851export default class EntryServiceExtAbility extends ServiceExtensionAbility { 2852 onRequest() { 2853 let want1: Want = { 2854 bundleName: 'com.example.myapplication1', 2855 abilityName: 'EntryAbility' 2856 }; 2857 let want2: Want = { 2858 bundleName: 'com.example.myapplication2', 2859 abilityName: 'EntryAbility' 2860 }; 2861 let wantList: Array<Want> = [want1, want2]; 2862 try { 2863 this.context.startUIAbilities(wantList).then(() => { 2864 console.info(`TestTag:: start succeeded.`); 2865 }).catch((error: BusinessError) => { 2866 console.info(`TestTag:: startUIAbilities failed: ${JSON.stringify(error)}`); 2867 }); 2868 } catch (paramError) { 2869 // Process input parameter errors. 2870 console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`); 2871 } 2872 } 2873} 2874``` 2875