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