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