1# ServiceExtensionContext 2 3ServiceExtensionContext模块是ServiceExtensionAbility的上下文环境,继承自ExtensionContext。 4 5ServiceExtensionContext模块提供ServiceExtensionAbility具有的能力和接口,包括启动、停止、绑定、解绑Ability。 6 7> **说明:** 8> 9> - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 10> - 本模块接口仅可在Stage模型下使用。 11 12## 导入模块 13 14```ts 15import common from '@ohos.app.ability.common'; 16``` 17 18## 使用说明 19 20在使用ServiceExtensionContext的功能前,需要通过ServiceExtensionAbility子类实例获取。 21 22```ts 23 import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 24 25 let context = undefined; 26 class MainAbility extends ServiceExtensionAbility { 27 onCreate() { 28 context = this.context; // 获取ServiceExtensionContext 29 } 30 } 31``` 32 33## ServiceExtensionContext.startAbility 34 35startAbility(want: Want, callback: AsyncCallback<void>): void; 36 37启动Ability。 38 39**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 40 41**系统API**: 此接口为系统接口,三方应用不支持调用。 42 43**参数:** 44 45| 参数名 | 类型 | 必填 | 说明 | 46| -------- | -------- | -------- | -------- | 47| want | [Want](js-apis-application-want.md) | 是 | Want类型参数,传入需要启动的ability的信息,如ability名称,包名等。 | 48| callback | AsyncCallback<void> | 否 | 回调函数,返回接口调用是否成功的结果。 | 49 50**错误码:** 51 52| 错误码ID | 错误信息 | 53| ------- | -------------------------------- | 54| 16000001 | The specified ability does not exist. | 55| 16000002 | Incorrect ability type. | 56| 16000004 | Can not start invisible component. | 57| 16000005 | The specified process does not have the permission. | 58| 16000006 | Cross-user operations are not allowed. | 59| 16000008 | The crowdtesting application expires. | 60| 16000009 | An ability cannot be started or stopped in Wukong mode. | 61| 16000010 | The call with the continuation flag is forbidden. | 62| 16000011 | The context does not exist. | 63| 16000050 | Internal error. | 64| 16000053 | The ability is not on the top of the UI. | 65| 16000055 | Installation-free timed out. | 66| 16200001 | The caller has been released. | 67 68以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 69 70**示例:** 71 72 ```ts 73 let want = { 74 bundleName: 'com.example.myapp', 75 abilityName: 'MyAbility' 76 }; 77 78 try { 79 this.context.startAbility(want, (error) => { 80 if (error.code) { 81 // 处理业务逻辑错误 82 console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) + 83 ' error.message: ' + JSON.stringify(error.message)); 84 return; 85 } 86 // 执行正常业务 87 console.log('startAbility succeed'); 88 }); 89 } catch (paramError) { 90 // 处理入参错误异常 91 console.log('error.code: ' + JSON.stringify(paramError.code) + 92 ' error.message: ' + JSON.stringify(paramError.message)); 93 } 94 ``` 95 96## ServiceExtensionContext.startAbility 97 98startAbility(want: Want, options?: StartOptions): Promise\<void>; 99 100启动Ability。通过Promise返回结果。 101 102**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 103 104**系统API**: 此接口为系统接口,三方应用不支持调用。 105 106**参数:** 107 108| 参数名 | 类型 | 必填 | 说明 | 109| -------- | -------- | -------- | -------- | 110| want | [Want](js-apis-application-want.md) | 是 | Want类型参数,传入需要启动的ability的信息,如ability名称,包名等。 | 111| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 | 112 113**返回值:** 114 115| 类型 | 说明 | 116| -------- | -------- | 117| Promise<void> | 返回一个Promise,包含接口的结果。 | 118 119**错误码:** 120 121| 错误码ID | 错误信息 | 122| ------- | -------------------------------- | 123| 16000001 | The specified ability does not exist. | 124| 16000002 | Incorrect ability type. | 125| 16000004 | Can not start invisible component. | 126| 16000005 | The specified process does not have the permission. | 127| 16000006 | Cross-user operations are not allowed. | 128| 16000008 | The crowdtesting application expires. | 129| 16000009 | An ability cannot be started or stopped in Wukong mode. | 130| 16000010 | The call with the continuation flag is forbidden. | 131| 16000011 | The context does not exist. | 132| 16000050 | Internal error. | 133| 16000053 | The ability is not on the top of the UI. | 134| 16000055 | Installation-free timed out. | 135| 16200001 | The caller has been released. | 136 137以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 138 139**示例:** 140 141 ```ts 142 let want = { 143 bundleName: 'com.example.myapp', 144 abilityName: 'MyAbility' 145 }; 146 let options = { 147 windowMode: 0, 148 }; 149 150 try { 151 this.context.startAbility(want, options) 152 .then((data) => { 153 // 执行正常业务 154 console.log('startAbility succeed'); 155 }) 156 .catch((error) => { 157 // 处理业务逻辑错误 158 console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) + 159 ' error.message: ' + JSON.stringify(error.message)); 160 }); 161 } catch (paramError) { 162 // 处理入参错误异常 163 console.log('error.code: ' + JSON.stringify(paramError.code) + 164 ' error.message: ' + JSON.stringify(paramError.message)); 165 } 166 ``` 167 168## ServiceExtensionContext.startAbility 169 170startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void 171 172启动Ability。 173 174**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 175 176**系统API**: 此接口为系统接口,三方应用不支持调用。 177 178**参数:** 179 180| 参数名 | 类型 | 必填 | 说明 | 181| -------- | -------- | -------- | -------- | 182| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 | 183| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 | 184| callback | AsyncCallback<void> | 是 | callback形式返回启动结果。 | 185 186**错误码:** 187 188| 错误码ID | 错误信息 | 189| ------- | -------------------------------- | 190| 16000001 | The specified ability does not exist. | 191| 16000002 | Incorrect ability type. | 192| 16000004 | Can not start invisible component. | 193| 16000005 | The specified process does not have the permission. | 194| 16000006 | Cross-user operations are not allowed. | 195| 16000008 | The crowdtesting application expires. | 196| 16000009 | An ability cannot be started or stopped in Wukong mode. | 197| 16000010 | The call with the continuation flag is forbidden. | 198| 16000011 | The context does not exist. | 199| 16000050 | Internal error. | 200| 16000053 | The ability is not on the top of the UI. | 201| 16000055 | Installation-free timed out. | 202| 16200001 | The caller has been released. | 203 204以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 205 206**示例:** 207 208 ```ts 209 let want = { 210 deviceId: '', 211 bundleName: 'com.extreme.test', 212 abilityName: 'MainAbility' 213 }; 214 let options = { 215 windowMode: 0 216 }; 217 218 try { 219 this.context.startAbility(want, options, (error) => { 220 if (error.code) { 221 // 处理业务逻辑错误 222 console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) + 223 ' error.message: ' + JSON.stringify(error.message)); 224 return; 225 } 226 // 执行正常业务 227 console.log('startAbility succeed'); 228 }); 229 } catch (paramError) { 230 // 处理入参错误异常 231 console.log('error.code: ' + JSON.stringify(paramError.code) + 232 ' error.message: ' + JSON.stringify(paramError.message)); 233 } 234 ``` 235 236## ServiceExtensionContext.startAbilityWithAccount 237 238startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void; 239 240根据account启动Ability(callback形式)。 241 242使用规则: 243 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限 244 - 目标Ability的visible属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限 245 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md) 246 247**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 248 249**系统API**: 此接口为系统接口,三方应用不支持调用。 250 251**参数:** 252 253| 参数名 | 类型 | 必填 | 说明 | 254| -------- | -------- | -------- | -------- | 255| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 | 256| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 | 257| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 | 258 259**错误码:** 260 261| 错误码ID | 错误信息 | 262| ------- | -------------------------------- | 263| 16000001 | The specified ability does not exist. | 264| 16000002 | Incorrect ability type. | 265| 16000004 | Can not start invisible component. | 266| 16000005 | The specified process does not have the permission. | 267| 16000006 | Cross-user operations are not allowed. | 268| 16000008 | The crowdtesting application expires. | 269| 16000009 | An ability cannot be started or stopped in Wukong mode. | 270| 16000010 | The call with the continuation flag is forbidden. | 271| 16000011 | The context does not exist. | 272| 16000050 | Internal error. | 273| 16000053 | The ability is not on the top of the UI. | 274| 16000055 | Installation-free timed out. | 275| 16200001 | The caller has been released. | 276 277以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 278 279**示例:** 280 281 ```ts 282 let want = { 283 deviceId: '', 284 bundleName: 'com.extreme.test', 285 abilityName: 'MainAbility' 286 }; 287 let accountId = 100; 288 289 try { 290 this.context.startAbilityWithAccount(want, accountId, (error) => { 291 if (error.code) { 292 // 处理业务逻辑错误 293 console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + 294 ' error.message: ' + JSON.stringify(error.message)); 295 return; 296 } 297 // 执行正常业务 298 console.log('startAbilityWithAccount succeed'); 299 }); 300 } catch (paramError) { 301 // 处理入参错误异常 302 console.log('error.code: ' + JSON.stringify(paramError.code) + 303 ' error.message: ' + JSON.stringify(paramError.message)); 304 } 305 ``` 306 307## ServiceExtensionContext.startAbilityWithAccount 308 309startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void; 310 311根据account启动Ability(callback形式)。 312 313使用规则: 314 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限 315 - 目标Ability的visible属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限 316 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md) 317 318**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 319 320**系统API**: 此接口为系统接口,三方应用不支持调用。 321 322**参数:** 323 324| 参数名 | 类型 | 必填 | 说明 | 325| -------- | -------- | -------- | -------- | 326| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 | 327| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 | 328| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 | 329| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 | 330 331**错误码:** 332 333| 错误码ID | 错误信息 | 334| ------- | -------------------------------- | 335| 16000001 | The specified ability does not exist. | 336| 16000002 | Incorrect ability type. | 337| 16000004 | Can not start invisible component. | 338| 16000005 | The specified process does not have the permission. | 339| 16000006 | Cross-user operations are not allowed. | 340| 16000008 | The crowdtesting application expires. | 341| 16000009 | An ability cannot be started or stopped in Wukong mode. | 342| 16000010 | The call with the continuation flag is forbidden. | 343| 16000011 | The context does not exist. | 344| 16000050 | Internal error. | 345| 16000053 | The ability is not on the top of the UI. | 346| 16000055 | Installation-free timed out. | 347| 16200001 | The caller has been released. | 348 349以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 350 351**示例:** 352 353 ```ts 354 let want = { 355 deviceId: '', 356 bundleName: 'com.extreme.test', 357 abilityName: 'MainAbility' 358 }; 359 let accountId = 100; 360 let options = { 361 windowMode: 0 362 }; 363 364 try { 365 this.context.startAbilityWithAccount(want, accountId, options, (error) => { 366 if (error.code) { 367 // 处理业务逻辑错误 368 console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + 369 ' error.message: ' + JSON.stringify(error.message)); 370 return; 371 } 372 // 执行正常业务 373 console.log('startAbilityWithAccount succeed'); 374 }); 375 } catch (paramError) { 376 // 处理入参错误异常 377 console.log('error.code: ' + JSON.stringify(paramError.code) + 378 ' error.message: ' + JSON.stringify(paramError.message)); 379 } 380 ``` 381 382 383## ServiceExtensionContext.startAbilityWithAccount 384 385startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void>; 386 387根据account启动Ability(Promise形式)。 388 389使用规则: 390 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限 391 - 目标Ability的visible属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限 392 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md) 393 394**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 395 396**系统API**: 此接口为系统接口,三方应用不支持调用。 397 398**参数:** 399 400| 参数名 | 类型 | 必填 | 说明 | 401| -------- | -------- | -------- | -------- | 402| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 | 403| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。。 | 404| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 | 405 406**返回值:** 407 408| 类型 | 说明 | 409| -------- | -------- | 410| Promise<void> | 返回一个Promise,包含接口的结果。 | 411 412**错误码:** 413 414| 错误码ID | 错误信息 | 415| ------- | -------------------------------- | 416| 16000001 | The specified ability does not exist. | 417| 16000002 | Incorrect ability type. | 418| 16000004 | Can not start invisible component. | 419| 16000005 | The specified process does not have the permission. | 420| 16000006 | Cross-user operations are not allowed. | 421| 16000008 | The crowdtesting application expires. | 422| 16000009 | An ability cannot be started or stopped in Wukong mode. | 423| 16000010 | The call with the continuation flag is forbidden. | 424| 16000011 | The context does not exist. | 425| 16000050 | Internal error. | 426| 16000053 | The ability is not on the top of the UI. | 427| 16000055 | Installation-free timed out. | 428| 16200001 | The caller has been released. | 429 430以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 431 432**示例:** 433 434 ```ts 435 let want = { 436 deviceId: '', 437 bundleName: 'com.extreme.test', 438 abilityName: 'MainAbility' 439 }; 440 let accountId = 100; 441 let options = { 442 windowMode: 0 443 }; 444 445 try { 446 this.context.startAbilityWithAccount(want, accountId, options) 447 .then((data) => { 448 // 执行正常业务 449 console.log('startAbilityWithAccount succeed'); 450 }) 451 .catch((error) => { 452 // 处理业务逻辑错误 453 console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + 454 ' error.message: ' + JSON.stringify(error.message)); 455 }); 456 } catch (paramError) { 457 // 处理入参错误异常 458 console.log('error.code: ' + JSON.stringify(paramError.code) + 459 ' error.message: ' + JSON.stringify(paramError.message)); 460 } 461 ``` 462 463## ServiceExtensionContext.startServiceExtensionAbility 464 465startServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void; 466 467启动一个新的ServiceExtensionAbility(callback形式)。 468 469**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 470 471**系统API**: 此接口为系统接口,三方应用不支持调用。 472 473**参数:** 474 475| 参数名 | 类型 | 必填 | 说明 | 476| -------- | -------- | -------- | -------- | 477| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 | 478| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 | 479 480**错误码:** 481 482| 错误码ID | 错误信息 | 483| ------- | -------------------------------- | 484| 16000001 | The specified ability does not exist. | 485| 16000002 | Incorrect ability type. | 486| 16000005 | The specified process does not have the permission. | 487| 16000006 | Cross-user operations are not allowed. | 488| 16000008 | The crowdtesting application expires. | 489| 16000011 | The context does not exist. | 490| 16000050 | Internal error. | 491| 16200001 | The caller has been released. | 492 493以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 494 495**示例:** 496 497 ```ts 498 let want = { 499 deviceId: '', 500 bundleName: 'com.extreme.test', 501 abilityName: 'MainAbility' 502 }; 503 504 try { 505 this.context.startServiceExtensionAbility(want, (error) => { 506 if (error.code) { 507 // 处理业务逻辑错误 508 console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + 509 ' error.message: ' + JSON.stringify(error.message)); 510 return; 511 } 512 // 执行正常业务 513 console.log('startServiceExtensionAbility succeed'); 514 }); 515 } catch (paramError) { 516 // 处理入参错误异常 517 console.log('error.code: ' + JSON.stringify(paramError.code) + 518 ' error.message: ' + JSON.stringify(paramError.message)); 519 } 520 ``` 521 522## ServiceExtensionContext.startServiceExtensionAbility 523 524startServiceExtensionAbility(want: Want): Promise\<void>; 525 526启动一个新的ServiceExtensionAbility(Promise形式)。 527 528**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 529 530**系统API**: 此接口为系统接口,三方应用不支持调用。 531 532**参数:** 533 534| 参数名 | 类型 | 必填 | 说明 | 535| -------- | -------- | -------- | -------- | 536| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 | 537 538**返回值:** 539 540| 类型 | 说明 | 541| -------- | -------- | 542| Promise<void> | 返回一个Promise,包含接口的结果。 | 543 544**错误码:** 545 546| 错误码ID | 错误信息 | 547| ------- | -------------------------------- | 548| 16000001 | The specified ability does not exist. | 549| 16000002 | Incorrect ability type. | 550| 16000005 | The specified process does not have the permission. | 551| 16000006 | Cross-user operations are not allowed. | 552| 16000008 | The crowdtesting application expires. | 553| 16000011 | The context does not exist. | 554| 16000050 | Internal error. | 555| 16200001 | The caller has been released. | 556 557以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 558 559**示例:** 560 561 ```ts 562 let want = { 563 deviceId: '', 564 bundleName: 'com.extreme.test', 565 abilityName: 'MainAbility' 566 }; 567 568 try { 569 this.context.startServiceExtensionAbility(want) 570 .then((data) => { 571 // 执行正常业务 572 console.log('startServiceExtensionAbility succeed'); 573 }) 574 .catch((error) => { 575 // 处理业务逻辑错误 576 console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + 577 ' error.message: ' + JSON.stringify(error.message)); 578 }); 579 } catch (paramError) { 580 // 处理入参错误异常 581 console.log('error.code: ' + JSON.stringify(paramError.code) + 582 ' error.message: ' + JSON.stringify(paramError.message)); 583 } 584 ``` 585 586## ServiceExtensionContext.startServiceExtensionAbilityWithAccount 587 588startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void; 589 590启动一个新的ServiceExtensionAbility(callback形式)。 591 592**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 593 594**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 595 596**系统API**: 此接口为系统接口,三方应用不支持调用。 597 598**参数:** 599 600| 参数名 | 类型 | 必填 | 说明 | 601| -------- | -------- | -------- | -------- | 602| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 | 603| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 | 604| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 | 605 606**错误码:** 607 608| 错误码ID | 错误信息 | 609| ------- | -------------------------------- | 610| 16000001 | The specified ability does not exist. | 611| 16000002 | Incorrect ability type. | 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| 16000050 | Internal error. | 617| 16200001 | The caller has been released. | 618 619以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 620 621**示例:** 622 623 ```ts 624 let want = { 625 deviceId: '', 626 bundleName: 'com.extreme.test', 627 abilityName: 'MainAbility' 628 }; 629 let accountId = 100; 630 631 try { 632 this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error) => { 633 if (error.code) { 634 // 处理业务逻辑错误 635 console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + 636 ' error.message: ' + JSON.stringify(error.message)); 637 return; 638 } 639 // 执行正常业务 640 console.log('startServiceExtensionAbilityWithAccount succeed'); 641 }); 642 } catch (paramError) { 643 // 处理入参错误异常 644 console.log('error.code: ' + JSON.stringify(paramError.code) + 645 ' error.message: ' + JSON.stringify(paramError.message)); 646 } 647 ``` 648 649## ServiceExtensionContext.startServiceExtensionAbilityWithAccount 650 651startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>; 652 653启动一个新的ServiceExtensionAbility(Promise形式)。 654 655**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 656 657**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 658 659**系统API**: 此接口为系统接口,三方应用不支持调用。 660 661**参数:** 662 663| 参数名 | 类型 | 必填 | 说明 | 664| -------- | -------- | -------- | -------- | 665| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 | 666| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 | 667 668**返回值:** 669 670| 类型 | 说明 | 671| -------- | -------- | 672| Promise<void> | 返回一个Promise,包含接口的结果。 | 673 674**错误码:** 675 676| 错误码ID | 错误信息 | 677| ------- | -------------------------------- | 678| 16000001 | The specified ability does not exist. | 679| 16000002 | Incorrect ability type. | 680| 16000005 | The specified process does not have the permission. | 681| 16000006 | Cross-user operations are not allowed. | 682| 16000008 | The crowdtesting application expires. | 683| 16000011 | The context does not exist. | 684| 16000050 | Internal error. | 685| 16200001 | The caller has been released. | 686 687以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 688 689**示例:** 690 691 ```ts 692 let want = { 693 deviceId: '', 694 bundleName: 'com.extreme.test', 695 abilityName: 'MainAbility' 696 }; 697 let accountId = 100; 698 699 try { 700 this.context.startServiceExtensionAbilityWithAccount(want, accountId) 701 .then((data) => { 702 // 执行正常业务 703 console.log('startServiceExtensionAbilityWithAccount succeed'); 704 }) 705 .catch((error) => { 706 // 处理业务逻辑错误 707 console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + 708 ' error.message: ' + JSON.stringify(error.message)); 709 }); 710 } catch (paramError) { 711 // 处理入参错误异常 712 console.log('error.code: ' + JSON.stringify(paramError.code) + 713 ' error.message: ' + JSON.stringify(paramError.message)); 714 } 715 ``` 716 717## ServiceExtensionContext.stopServiceExtensionAbility 718 719stopServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void; 720 721停止同一应用程序内的服务(callback形式)。 722 723**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 724 725**系统API**: 此接口为系统接口,三方应用不支持调用。 726 727**参数:** 728 729| 参数名 | 类型 | 必填 | 说明 | 730| -------- | -------- | -------- | -------- | 731| want | [Want](js-apis-application-want.md) | 是 | 停止Ability的want信息。 | 732| callback | AsyncCallback\<void\> | 是 | 停止Ability的回调函数。 | 733 734**错误码:** 735 736| 错误码ID | 错误信息 | 737| ------- | -------------------------------- | 738| 16000001 | The specified ability does not exist. | 739| 16000002 | Incorrect ability type. | 740| 16000005 | The specified process does not have the permission. | 741| 16000006 | Cross-user operations are not allowed. | 742| 16000011 | The context does not exist. | 743| 16000050 | Internal error. | 744| 16200001 | The caller has been released. | 745 746以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 747 748**示例:** 749 750 ```ts 751 let want = { 752 deviceId: '', 753 bundleName: 'com.extreme.test', 754 abilityName: 'MainAbility' 755 }; 756 757 try { 758 this.context.stopServiceExtensionAbility(want, (error) => { 759 if (error.code) { 760 // 处理业务逻辑错误 761 console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + 762 ' error.message: ' + JSON.stringify(error.message)); 763 return; 764 } 765 // 执行正常业务 766 console.log('stopServiceExtensionAbility succeed'); 767 }); 768 } catch (paramError) { 769 // 处理入参错误异常 770 console.log('error.code: ' + JSON.stringify(paramError.code) + 771 ' error.message: ' + JSON.stringify(paramError.message)); 772 } 773 ``` 774 775## ServiceExtensionContext.stopServiceExtensionAbility 776 777stopServiceExtensionAbility(want: Want): Promise\<void>; 778 779停止同一应用程序内的服务(Promise形式)。 780 781**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 782 783**系统API**: 此接口为系统接口,三方应用不支持调用。 784 785**参数:** 786 787| 参数名 | 类型 | 必填 | 说明 | 788| -------- | -------- | -------- | -------- | 789| want | [Want](js-apis-application-want.md) | 是 | 停止Ability的want信息。 | 790 791**返回值:** 792 793| 类型 | 说明 | 794| -------- | -------- | 795| Promise<void> | 返回一个Promise,包含接口的结果。 | 796 797**错误码:** 798 799| 错误码ID | 错误信息 | 800| ------- | -------------------------------- | 801| 16000001 | The specified ability does not exist. | 802| 16000002 | Incorrect ability type. | 803| 16000005 | The specified process does not have the permission. | 804| 16000006 | Cross-user operations are not allowed. | 805| 16000011 | The context does not exist. | 806| 16000050 | Internal error. | 807| 16200001 | The caller has been released. | 808 809以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 810 811**示例:** 812 813 ```ts 814 let want = { 815 deviceId: '', 816 bundleName: 'com.extreme.test', 817 abilityName: 'MainAbility' 818 }; 819 820 try { 821 this.context.stopServiceExtensionAbility(want) 822 .then((data) => { 823 // 执行正常业务 824 console.log('stopServiceExtensionAbility succeed'); 825 }) 826 .catch((error) => { 827 // 处理业务逻辑错误 828 console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + 829 ' error.message: ' + JSON.stringify(error.message)); 830 }); 831 } catch (paramError) { 832 // 处理入参错误异常 833 console.log('error.code: ' + JSON.stringify(paramError.code) + 834 ' error.message: ' + JSON.stringify(paramError.message)); 835 } 836 ``` 837 838## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount 839 840stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void; 841 842使用帐户停止同一应用程序内的服务(callback形式)。 843 844**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 845 846**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 847 848**系统API**: 此接口为系统接口,三方应用不支持调用。 849 850**参数:** 851 852| 参数名 | 类型 | 必填 | 说明 | 853| -------- | -------- | -------- | -------- | 854| want | [Want](js-apis-application-want.md) | 是 | 停止Ability的want信息。 | 855| accountId | number | 是 | 需要停止的系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 | 856| callback | AsyncCallback\<void\> | 是 | 停止Ability的回调函数。 | 857 858**错误码:** 859 860| 错误码ID | 错误信息 | 861| ------- | -------------------------------- | 862| 16000001 | The specified ability does not exist. | 863| 16000002 | Incorrect ability type. | 864| 16000005 | The specified process does not have the permission. | 865| 16000006 | Cross-user operations are not allowed. | 866| 16000011 | The context does not exist. | 867| 16000050 | Internal error. | 868| 16200001 | The caller has been released. | 869 870以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 871 872**示例:** 873 874 ```ts 875 let want = { 876 deviceId: '', 877 bundleName: 'com.extreme.test', 878 abilityName: 'MainAbility' 879 }; 880 let accountId = 100; 881 882 try { 883 this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error) => { 884 if (error.code) { 885 // 处理业务逻辑错误 886 console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + 887 ' error.message: ' + JSON.stringify(error.message)); 888 return; 889 } 890 // 执行正常业务 891 console.log('stopServiceExtensionAbilityWithAccount succeed'); 892 }); 893 } catch (paramError) { 894 // 处理入参错误异常 895 console.log('error.code: ' + JSON.stringify(paramError.code) + 896 ' error.message: ' + JSON.stringify(paramError.message)); 897 } 898 ``` 899 900## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount 901 902stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>; 903 904使用帐户停止同一应用程序内的服务(Promise形式)。 905 906**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 907 908**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 909 910**系统API**: 此接口为系统接口,三方应用不支持调用。 911 912**参数:** 913 914| 参数名 | 类型 | 必填 | 说明 | 915| -------- | -------- | -------- | -------- | 916| want | [Want](js-apis-application-want.md) | 是 | 停止Ability的want信息。 | 917| accountId | number | 是 | 需要停止的系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 | 918 919**返回值:** 920 921| 类型 | 说明 | 922| -------- | -------- | 923| Promise<void> | 返回一个Promise,包含接口的结果。 | 924 925**错误码:** 926 927| 错误码ID | 错误信息 | 928| ------- | -------------------------------- | 929| 16000001 | The specified ability does not exist. | 930| 16000002 | Incorrect ability type. | 931| 16000005 | The specified process does not have the permission. | 932| 16000006 | Cross-user operations are not allowed. | 933| 16000011 | The context does not exist. | 934| 16000050 | Internal error. | 935| 16200001 | The caller has been released. | 936 937以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 938 939**示例:** 940 941 ```ts 942 let want = { 943 deviceId: '', 944 bundleName: 'com.extreme.test', 945 abilityName: 'MainAbility' 946 }; 947 let accountId = 100; 948 949 try { 950 this.context.stopServiceExtensionAbilityWithAccount(want, accountId) 951 .then((data) => { 952 // 执行正常业务 953 console.log('stopServiceExtensionAbilityWithAccount succeed'); 954 }) 955 .catch((error) => { 956 // 处理业务逻辑错误 957 console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + 958 ' error.message: ' + JSON.stringify(error.message)); 959 }); 960 } catch (paramError) { 961 // 处理入参错误异常 962 console.log('error.code: ' + JSON.stringify(paramError.code) + 963 ' error.message: ' + JSON.stringify(paramError.message)); 964 } 965 ``` 966 967## ServiceExtensionContext.terminateSelf 968 969terminateSelf(callback: AsyncCallback<void>): void; 970 971停止Ability自身。 972 973**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 974 975**系统API**: 此接口为系统接口,三方应用不支持调用。 976 977**参数:** 978 979| 参数名 | 类型 | 必填 | 说明 | 980| -------- | -------- | -------- | -------- | 981| callback | AsyncCallback<void> | 否 | 回调函数,返回接口调用是否成功的结果。 | 982 983**错误码:** 984 985| 错误码ID | 错误信息 | 986| ------- | -------------------------------- | 987| 16000001 | The specified ability does not exist. | 988| 16000004 | Can not start invisible component. | 989| 16000005 | The specified process does not have the permission. | 990| 16000009 | An ability cannot be started or stopped in Wukong mode. | 991| 16000011 | The context does not exist. | 992| 16000050 | Internal error. | 993 994以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 995 996**示例:** 997 998 ```ts 999 this.context.terminateSelf((error) => { 1000 if (error.code) { 1001 // 处理业务逻辑错误 1002 console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) + 1003 ' error.message: ' + JSON.stringify(error.message)); 1004 return; 1005 } 1006 // 执行正常业务 1007 console.log('terminateSelf succeed'); 1008 }); 1009 ``` 1010 1011## ServiceExtensionContext.terminateSelf 1012 1013terminateSelf(): Promise<void>; 1014 1015停止自身。通过Promise返回结果。 1016 1017**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1018 1019**系统API**: 此接口为系统接口,三方应用不支持调用。 1020 1021**返回值:** 1022 1023| 类型 | 说明 | 1024| -------- | -------- | 1025| Promise<void> | 返回一个Promise,包含接口的结果。 | 1026 1027**错误码:** 1028 1029| 错误码ID | 错误信息 | 1030| ------- | -------------------------------- | 1031| 16000001 | The specified ability does not exist. | 1032| 16000004 | Can not start invisible component. | 1033| 16000005 | The specified process does not have the permission. | 1034| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1035| 16000011 | The context does not exist. | 1036| 16000050 | Internal error. | 1037 1038以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 1039 1040**示例:** 1041 1042 ```ts 1043 this.context.terminateSelf().then((data) => { 1044 // 执行正常业务 1045 console.log('terminateSelf succeed'); 1046 }).catch((error) => { 1047 // 处理业务逻辑错误 1048 console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) + 1049 ' error.message: ' + JSON.stringify(error.message)); 1050 }); 1051 ``` 1052 1053## ServiceExtensionContext.connectServiceExtensionAbility 1054 1055connectServiceExtensionAbility(want: Want, options: ConnectOptions): number; 1056 1057将一个Ability与服务类型的Ability绑定。 1058 1059**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1060 1061**系统API**: 此接口为系统接口,三方应用不支持调用。 1062 1063**参数:** 1064 1065| 参数名 | 类型 | 必填 | 说明 | 1066| -------- | -------- | -------- | -------- | 1067| want | [Want](js-apis-application-want.md) | 是 | Want类型参数,传入需要启动的ability的信息,如ability名称,包名等。 | 1068| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | ConnectOptions类型的回调函数,返回服务连接成功、断开或连接失败后的信息。 | 1069 1070**返回值:** 1071 1072| 类型 | 说明 | 1073| -------- | -------- | 1074| number | 返回一个number,后续根据这个number去断开连接。 | 1075 1076**错误码:** 1077 1078| 错误码ID | 错误信息 | 1079| ------- | -------------------------------- | 1080| 16000001 | Input error. The specified ability name does not exist. | 1081| 16000005 | The specified process does not have the permission. | 1082| 16000011 | The context does not exist. | 1083| 16000050 | Internal Error. | 1084 1085以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 1086 1087**示例:** 1088 1089 ```ts 1090 let want = { 1091 bundleName: 'com.example.myapp', 1092 abilityName: 'MyAbility' 1093 }; 1094 let options = { 1095 onConnect(elementName, remote) { console.log('----------- onConnect -----------') }, 1096 onDisconnect(elementName) { console.log('----------- onDisconnect -----------') }, 1097 onFailed(code) { console.log('----------- onFailed -----------') } 1098 }; 1099 1100 let connection = null; 1101 try { 1102 connection = this.context.connectServiceExtensionAbility(want, options); 1103 } catch (paramError) { 1104 // 处理入参错误异常 1105 console.log('error.code: ' + JSON.stringify(paramError.code) + 1106 ' error.message: ' + JSON.stringify(paramError.message)); 1107 } 1108 ``` 1109 1110## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount 1111 1112connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number; 1113 1114使用AbilityInfo.AbilityType.SERVICE模板和account将当前能力连接到一个能力。 1115 1116**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1117 1118**系统API**: 此接口为系统接口,三方应用不支持调用。 1119 1120**参数:** 1121 1122| 参数名 | 类型 | 必填 | 说明 | 1123| -------- | -------- | -------- | -------- | 1124| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 | 1125| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 | 1126| options | ConnectOptions | 否 | 远端对象实例。 | 1127 1128**返回值:** 1129 1130| 类型 | 说明 | 1131| -------- | -------- | 1132| number | 返回Ability连接的结果code。 | 1133 1134**错误码:** 1135 1136| 错误码ID | 错误信息 | 1137| ------- | -------------------------------- | 1138| 16000001 | Input error. The specified ability name does not exist. | 1139| 16000005 | The specified process does not have the permission. | 1140| 16000011 | The context does not exist. | 1141| 16000050 | Internal Error. | 1142 1143以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 1144 1145**示例:** 1146 1147 ```ts 1148 let want = { 1149 deviceId: '', 1150 bundleName: 'com.extreme.test', 1151 abilityName: 'MainAbility' 1152 }; 1153 let accountId = 100; 1154 let options = { 1155 onConnect(elementName, remote) { console.log('----------- onConnect -----------') }, 1156 onDisconnect(elementName) { console.log('----------- onDisconnect -----------') }, 1157 onFailed(code) { console.log('----------- onFailed -----------') } 1158 } 1159 1160 let connection = null; 1161 try { 1162 connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options); 1163 } catch (paramError) { 1164 // 处理入参错误异常 1165 console.log('error.code: ' + JSON.stringify(paramError.code) + 1166 ' error.message: ' + JSON.stringify(paramError.message)); 1167 } 1168 ``` 1169 1170## ServiceExtensionContext.disconnectServiceExtensionAbility 1171 1172disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback<void>): void; 1173 1174将一个Ability与绑定的服务类型的Ability解绑。 1175 1176**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1177 1178**系统API**: 此接口为系统接口,三方应用不支持调用。 1179 1180**参数:** 1181 1182| 参数名 | 类型 | 必填 | 说明 | 1183| -------- | -------- | -------- | -------- | 1184| connection | number | 是 | 在connectServiceExtensionAbility中返回的number。 | 1185| callback | AsyncCallback<void> | 否 | 回调函数,返回接口调用是否成功的结果。 | 1186 1187**错误码:** 1188 1189| 错误码ID | 错误信息 | 1190| ------- | -------------------------------- | 1191| 16000011 | The context does not exist. | 1192| 16000050 | Internal Error. | 1193 1194以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 1195 1196**示例:** 1197 1198 ```ts 1199 // connection为connectServiceExtensionAbility中的返回值 1200 let connection = 1; 1201 1202 try { 1203 this.context.disconnectServiceExtensionAbility(connection, (error) => { 1204 if (error.code) { 1205 // 处理业务逻辑错误 1206 console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + 1207 ' error.message: ' + JSON.stringify(error.message)); 1208 return; 1209 } 1210 // 执行正常业务 1211 console.log('disconnectServiceExtensionAbility succeed'); 1212 }); 1213 } catch (paramError) { 1214 // 处理入参错误异常 1215 console.log('error.code: ' + JSON.stringify(paramError.code) + 1216 ' error.message: ' + JSON.stringify(paramError.message)); 1217 } 1218 ``` 1219 1220## ServiceExtensionContext.disconnectServiceExtensionAbility 1221 1222disconnectServiceExtensionAbility(connection: number): Promise<void>; 1223 1224将一个Ability与绑定的服务类型的Ability解绑。通过Promise返回结果。 1225 1226**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1227 1228**系统API**: 此接口为系统接口,三方应用不支持调用。 1229 1230**参数:** 1231 1232| 参数名 | 类型 | 必填 | 说明 | 1233| -------- | -------- | -------- | -------- | 1234| connection | number | 是 | 在connectServiceExtensionAbility中返回的number。 | 1235 1236**返回值:** 1237 1238| 类型 | 说明 | 1239| -------- | -------- | 1240| Promise<void> | 返回一个Promise,包含接口的结果。 | 1241 1242**错误码:** 1243 1244| 错误码ID | 错误信息 | 1245| ------- | -------------------------------- | 1246| 16000011 | The context does not exist. | 1247| 16000050 | Internal Error. | 1248 1249以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 1250 1251**示例:** 1252 1253 ```ts 1254 // connection为connectServiceExtensionAbility中的返回值 1255 let connection = 1; 1256 1257 try { 1258 this.context.disconnectServiceExtensionAbility(connection) 1259 .then((data) => { 1260 // 执行正常业务 1261 console.log('disconnectServiceExtensionAbility succeed'); 1262 }) 1263 .catch((error) => { 1264 // 处理业务逻辑错误 1265 console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + 1266 ' error.message: ' + JSON.stringify(error.message)); 1267 }); 1268 } catch (paramError) { 1269 // 处理入参错误异常 1270 console.log('error.code: ' + JSON.stringify(paramError.code) + 1271 ' error.message: ' + JSON.stringify(paramError.message)); 1272 } 1273 ``` 1274 1275## ServiceExtensionContext.startAbilityByCall 1276 1277startAbilityByCall(want: Want): Promise<Caller>; 1278 1279启动指定Ability至前台或后台,同时获取其Caller通信接口,调用方可使用Caller与被启动的Ability进行通信。 1280 1281使用规则: 1282 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限 1283 - 目标Ability的visible属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限 1284 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md) 1285 1286**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1287 1288**系统API**:此接口为系统接口,三方应用不支持调用。 1289 1290**参数:** 1291 1292| 参数名 | 类型 | 必填 | 说明 | 1293| -------- | -------- | -------- | -------- | 1294| want | [Want](js-apis-application-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId(可选)、parameters(可选),其中deviceId缺省或为空表示启动本地Ability,parameters缺省或为空表示后台启动Ability。 | 1295 1296**返回值:** 1297 1298| 类型 | 说明 | 1299| -------- | -------- | 1300| Promise<Caller> | 获取要通讯的caller对象。 | 1301 1302**错误码:** 1303 1304| 错误码ID | 错误信息 | 1305| ------- | -------------------------------- | 1306| 16000001 | Input error. The specified ability name does not exist. | 1307| 16000002 | Incorrect ability type. | 1308| 16000004 | Visibility verification failed. | 1309| 16000005 | Static permission denied. The specified process does not have the permission. | 1310| 16000006 | Cross-user operations are not allowed. | 1311| 16000008 | Crowdtest App Expiration. | 1312| 16000011 | The context does not exist. | 1313| 16000050 | Internal Error. | 1314| 16200001 | The caller has been released. | 1315 1316以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 1317 1318**示例:** 1319 1320 后台启动: 1321 1322 ```ts 1323 let caller = undefined; 1324 1325 // 后台启动Ability,不配置parameters 1326 let wantBackground = { 1327 bundleName: 'com.example.myservice', 1328 moduleName: 'entry', 1329 abilityName: 'MainAbility', 1330 deviceId: '' 1331 }; 1332 1333 try { 1334 this.context.startAbilityByCall(wantBackground) 1335 .then((obj) => { 1336 // 执行正常业务 1337 caller = obj; 1338 console.log('startAbilityByCall succeed'); 1339 }).catch((error) => { 1340 // 处理业务逻辑错误 1341 console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) + 1342 ' error.message: ' + JSON.stringify(error.message)); 1343 }); 1344 } catch (paramError) { 1345 // 处理入参错误异常 1346 console.log('error.code: ' + JSON.stringify(paramError.code) + 1347 ' error.message: ' + JSON.stringify(paramError.message)); 1348 } 1349 ``` 1350 1351 前台启动: 1352 1353 ```ts 1354 let caller = undefined; 1355 1356 // 前台启动Ability,将parameters中的'ohos.aafwk.param.callAbilityToForeground'配置为true 1357 let wantForeground = { 1358 bundleName: 'com.example.myservice', 1359 moduleName: 'entry', 1360 abilityName: 'MainAbility', 1361 deviceId: '', 1362 parameters: { 1363 'ohos.aafwk.param.callAbilityToForeground': true 1364 } 1365 }; 1366 1367 try { 1368 this.context.startAbilityByCall(wantForeground) 1369 .then((obj) => { 1370 // 执行正常业务 1371 caller = obj; 1372 console.log('startAbilityByCall succeed'); 1373 }).catch((error) => { 1374 // 处理业务逻辑错误 1375 console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) + 1376 ' error.message: ' + JSON.stringify(error.message)); 1377 }); 1378 } catch (paramError) { 1379 // 处理入参错误异常 1380 console.log('error.code: ' + JSON.stringify(paramError.code) + 1381 ' error.message: ' + JSON.stringify(paramError.message)); 1382 } 1383 ```