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。仅支持在主线程调用。 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> | 是 | 回调函数,返回接口调用是否成功的结果。 | 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,包含启动的结果。 | 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> | 是 | callback形式返回启动结果。 | 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根据account启动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\> | 是 | 启动Ability的回调函数。 | 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根据account启动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\> | 是 | 启动Ability的回调函数。 | 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根据account启动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,包含接口的结果。 | 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\> | 是 | 启动Ability的回调函数。 | 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,包含接口的结果。 | 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\> | 是 | 启动Ability的回调函数。 | 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,包含接口的结果。 | 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\> | 是 | 停止Ability的回调函数。 | 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,包含接口的结果。 | 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\> | 是 | 停止Ability的回调函数。 | 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,包含接口的结果。 | 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自身。仅支持在主线程调用。 1454 1455**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1456 1457**系统接口**:此接口为系统接口。 1458 1459**参数:** 1460 1461| 参数名 | 类型 | 必填 | 说明 | 1462| -------- | -------- | -------- | -------- | 1463| callback | AsyncCallback<void> | 是 | 回调函数,返回接口调用是否成功的结果。 | 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,包含接口的结果。 | 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> **说明:** 1640> 1641> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1642> 当accountId为当前用户时,无需进行权限校验。 1643 1644**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1645 1646**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1647 1648**系统接口**:此接口为系统接口。 1649 1650**参数:** 1651 1652| 参数名 | 类型 | 必填 | 说明 | 1653| -------- | -------- | -------- | -------- | 1654| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 1655| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated)。 | 1656| options | ConnectOptions | 是 | 远端对象实例。 | 1657 1658**返回值:** 1659 1660| 类型 | 说明 | 1661| -------- | -------- | 1662| number | 返回Ability连接的结果code。 | 1663 1664**错误码:** 1665 1666以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1667 1668| 错误码ID | 错误信息 | 1669| ------- | -------- | 1670| 201 | The application does not have permission to call the interface. | 1671| 202 | The application is not system-app, can not use system-api. | 1672| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1673| 16000001 | The specified ability does not exist. | 1674| 16000002 | Incorrect ability type. | 1675| 16000004 | Failed to start the invisible ability. | 1676| 16000005 | The specified process does not have the permission. | 1677| 16000006 | Cross-user operations are not allowed. | 1678| 16000008 | The crowdtesting application expires. | 1679| 16000053 | The ability is not on the top of the UI. | 1680| 16000055 | Installation-free timed out. | 1681| 16000011 | The context does not exist. | 1682| 16000050 | Internal error. | 1683 1684**示例:** 1685 1686```ts 1687import { ServiceExtensionAbility, Want, common } from '@kit.AbilityKit'; 1688import { rpc } from '@kit.IPCKit'; 1689import { BusinessError } from '@kit.BasicServicesKit'; 1690 1691let commRemote: rpc.IRemoteObject; // 断开连接时需要释放 1692 1693class EntryAbility extends ServiceExtensionAbility { 1694 onCreate() { 1695 let want: Want = { 1696 deviceId: '', 1697 bundleName: 'com.example.myapplication', 1698 abilityName: 'EntryAbility' 1699 }; 1700 let accountId = 100; 1701 let options: common.ConnectOptions = { 1702 onConnect(elementName, remote) { 1703 commRemote = remote; 1704 console.log('----------- onConnect -----------'); 1705 }, 1706 onDisconnect(elementName) { 1707 console.log('----------- onDisconnect -----------'); 1708 }, 1709 onFailed(code) { 1710 console.log('----------- onFailed -----------'); 1711 } 1712 }; 1713 let connection: number; 1714 1715 try { 1716 connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options); 1717 } catch (paramError) { 1718 // 处理入参错误异常 1719 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1720 } 1721 } 1722} 1723``` 1724 1725## ServiceExtensionContext.disconnectServiceExtensionAbility 1726 1727disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback<void>): void; 1728 1729将一个Ability与绑定的服务类型的Ability解绑,断开连接之后需要将连接成功时返回的remote对象置空。仅支持在主线程调用。 1730 1731**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1732 1733**系统接口**:此接口为系统接口。 1734 1735**参数:** 1736 1737| 参数名 | 类型 | 必填 | 说明 | 1738| -------- | -------- | -------- | -------- | 1739| connection | number | 是 | 在connectServiceExtensionAbility中返回的number。 | 1740| callback | AsyncCallback<void> | 是 | 回调函数,返回接口调用是否成功的结果。 | 1741 1742**错误码:** 1743 1744以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1745 1746| 错误码ID | 错误信息 | 1747| ------- | -------- | 1748| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1749| 16000011 | The context does not exist. | 1750| 16000050 | Internal error. | 1751 1752**示例:** 1753 1754```ts 1755import { ServiceExtensionAbility } from '@kit.AbilityKit'; 1756import { rpc } from '@kit.IPCKit'; 1757import { BusinessError } from '@kit.BasicServicesKit'; 1758 1759let commRemote: rpc.IRemoteObject | null; // 断开连接时需要释放 1760 1761class EntryAbility extends ServiceExtensionAbility { 1762 onCreate() { 1763 // connection为connectServiceExtensionAbility中的返回值 1764 let connection = 1; 1765 try { 1766 this.context.disconnectServiceExtensionAbility(connection, (error: BusinessError) => { 1767 commRemote = null; 1768 if (error.code) { 1769 // 处理业务逻辑错误 1770 console.error(`disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 1771 return; 1772 } 1773 // 执行正常业务 1774 console.log('disconnectServiceExtensionAbility succeed'); 1775 }); 1776 } catch (paramError) { 1777 commRemote = null; 1778 // 处理入参错误异常 1779 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1780 } 1781 } 1782} 1783``` 1784 1785## ServiceExtensionContext.disconnectServiceExtensionAbility 1786 1787disconnectServiceExtensionAbility(connection: number): Promise<void>; 1788 1789将一个Ability与绑定的服务类型的Ability解绑,断开连接之后需要将连接成功时返回的remote对象置空(Promise形式返回结果)。仅支持在主线程调用。 1790 1791**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1792 1793**系统接口**:此接口为系统接口。 1794 1795**参数:** 1796 1797| 参数名 | 类型 | 必填 | 说明 | 1798| -------- | -------- | -------- | -------- | 1799| connection | number | 是 | 在connectServiceExtensionAbility中返回的number。 | 1800 1801**返回值:** 1802 1803| 类型 | 说明 | 1804| -------- | -------- | 1805| Promise<void> | 返回一个Promise,包含接口的结果。 | 1806 1807**错误码:** 1808 1809以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1810 1811| 错误码ID | 错误信息 | 1812| ------- | -------- | 1813| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1814| 16000011 | The context does not exist. | 1815| 16000050 | Internal error. | 1816 1817**示例:** 1818 1819```ts 1820import { ServiceExtensionAbility } from '@kit.AbilityKit'; 1821import { rpc } from '@kit.IPCKit'; 1822import { BusinessError } from '@kit.BasicServicesKit'; 1823 1824let commRemote: rpc.IRemoteObject | null; // 断开连接时需要释放 1825 1826class EntryAbility extends ServiceExtensionAbility { 1827 onCreate() { 1828 // connection为connectServiceExtensionAbility中的返回值 1829 let connection = 1; 1830 try { 1831 this.context.disconnectServiceExtensionAbility(connection) 1832 .then(() => { 1833 commRemote = null; 1834 // 执行正常业务 1835 console.log('disconnectServiceExtensionAbility succeed'); 1836 }) 1837 .catch((error: BusinessError) => { 1838 commRemote = null; 1839 // 处理业务逻辑错误 1840 console.error(`disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 1841 }); 1842 } catch (paramError) { 1843 commRemote = null; 1844 // 处理入参错误异常 1845 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1846 } 1847 } 1848} 1849``` 1850 1851## ServiceExtensionContext.startAbilityByCall 1852 1853startAbilityByCall(want: Want): Promise<Caller>; 1854 1855启动指定Ability至前台或后台,同时获取其Caller通信接口,调用方可使用Caller与被启动的Ability进行通信。仅支持在主线程调用。 1856该接口不支持拉起启动模式为[specified模式](../../application-models/uiability-launch-type.md#specified启动模式)的UIAbility。 1857 1858使用规则: 1859 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限 1860 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限 1861 - 同设备与跨设备场景下,该接口的使用规则存在差异,详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md) 1862 1863**需要权限**:ohos.permission.ABILITY_BACKGROUND_COMMUNICATION 1864 1865**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1866 1867**系统接口**:此接口为系统接口。 1868 1869**参数:** 1870 1871| 参数名 | 类型 | 必填 | 说明 | 1872| -------- | -------- | -------- | -------- | 1873| want | [Want](js-apis-app-ability-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId、parameters(可选),parameters缺省或为空表示后台启动Ability。 | 1874 1875**返回值:** 1876 1877| 类型 | 说明 | 1878| -------- | -------- | 1879| Promise<Caller> | 获取要通讯的caller对象。 | 1880 1881**错误码:** 1882 1883以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1884 1885| 错误码ID | 错误信息 | 1886| ------- | -------- | 1887| 201 | The application does not have permission to call the interface. | 1888| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1889| 16000001 | The specified ability does not exist. | 1890| 16000002 | Incorrect ability type. | 1891| 16000004 | Failed to start the invisible ability. | 1892| 16000005 | Static permission denied. The specified process does not have the permission. | 1893| 16000006 | Cross-user operations are not allowed. | 1894| 16000008 | The crowdtesting application expires. | 1895| 16000011 | The context does not exist. | 1896| 16000050 | Internal error. | 1897| 16200001 | The caller has been released. | 1898 1899**示例:** 1900 1901后台启动: 1902 1903```ts 1904import { ServiceExtensionAbility, Caller, Want } from '@kit.AbilityKit'; 1905import { BusinessError } from '@kit.BasicServicesKit'; 1906 1907class EntryAbility extends ServiceExtensionAbility { 1908 onCreate() { 1909 let caller: Caller; 1910 // 后台启动Ability,不配置parameters 1911 let wantBackground: Want = { 1912 bundleName: 'com.example.myservice', 1913 moduleName: 'entry', 1914 abilityName: 'EntryAbility', 1915 deviceId: '' 1916 }; 1917 1918 try { 1919 this.context.startAbilityByCall(wantBackground) 1920 .then((obj: Caller) => { 1921 // 执行正常业务 1922 caller = obj; 1923 console.log('startAbilityByCall succeed'); 1924 }).catch((error: BusinessError) => { 1925 // 处理业务逻辑错误 1926 console.error(`startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}`); 1927 }); 1928 } catch (paramError) { 1929 // 处理入参错误异常 1930 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1931 } 1932 } 1933} 1934``` 1935 1936前台启动: 1937 1938```ts 1939import { ServiceExtensionAbility, Caller, Want } from '@kit.AbilityKit'; 1940import { BusinessError } from '@kit.BasicServicesKit'; 1941 1942class EntryAbility extends ServiceExtensionAbility { 1943 onCreate() { 1944 let caller: Caller; 1945 // 前台启动Ability,将parameters中的'ohos.aafwk.param.callAbilityToForeground'配置为true 1946 let wantForeground: Want = { 1947 bundleName: 'com.example.myservice', 1948 moduleName: 'entry', 1949 abilityName: 'EntryAbility', 1950 deviceId: '', 1951 parameters: { 1952 'ohos.aafwk.param.callAbilityToForeground': true 1953 } 1954 }; 1955 1956 try { 1957 this.context.startAbilityByCall(wantForeground) 1958 .then((obj: Caller) => { 1959 // 执行正常业务 1960 caller = obj; 1961 console.log('startAbilityByCall succeed'); 1962 }).catch((error: BusinessError) => { 1963 // 处理业务逻辑错误 1964 console.error(`startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}`); 1965 }); 1966 } catch (paramError) { 1967 // 处理入参错误异常 1968 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1969 } 1970 } 1971} 1972``` 1973## ServiceExtensionContext.startRecentAbility 1974 1975startRecentAbility(want: Want, callback: AsyncCallback\<void>): void; 1976 1977启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。启动结果以callback的形式返回开发者。仅支持在主线程调用。 1978 1979> **说明:** 1980> 1981> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1982 1983**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1984 1985**系统接口**:此接口为系统接口。 1986 1987**参数:** 1988 1989| 参数名 | 类型 | 必填 | 说明 | 1990| -------- | -------- | -------- | -------- | 1991| want | [Want](js-apis-app-ability-want.md) | 是 | 需要启动Ability的want信息。 | 1992| callback | AsyncCallback\<void> | 是 | 指定的回调函数的结果。 | 1993 1994**错误码:** 1995 1996以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1997 1998| 错误码ID | 错误信息 | 1999| ------- | -------- | 2000| 201 | The application does not have permission to call the interface. | 2001| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2002| 16000001 | The specified ability does not exist. | 2003| 16000002 | Incorrect ability type. | 2004| 16000004 | Failed to start the invisible ability. | 2005| 16000005 | The specified process does not have the permission. | 2006| 16000006 | Cross-user operations are not allowed. | 2007| 16000008 | The crowdtesting application expires. | 2008| 16000009 | An ability cannot be started or stopped in Wukong mode. | 2009| 16000010 | The call with the continuation flag is forbidden. | 2010| 16000011 | The context does not exist. | 2011| 16000050 | Internal error. | 2012| 16000053 | The ability is not on the top of the UI. | 2013| 16000055 | Installation-free timed out. | 2014| 16000082 | The UIAbility is being started. | 2015| 16200001 | The caller has been released. | 2016 2017**示例:** 2018 2019```ts 2020import { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 2021import { BusinessError } from '@kit.BasicServicesKit'; 2022 2023class EntryAbility extends ServiceExtensionAbility { 2024 onCreate() { 2025 let want: Want = { 2026 bundleName: 'com.example.myapplication', 2027 abilityName: 'EntryAbility' 2028 }; 2029 2030 try { 2031 this.context.startRecentAbility(want, (err: BusinessError) => { 2032 if (err.code) { 2033 // 处理业务逻辑错误 2034 console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); 2035 return; 2036 } 2037 // 执行正常业务 2038 console.info('startRecentAbility succeed'); 2039 }); 2040 } catch (err) { 2041 // 处理入参错误异常 2042 let code = (err as BusinessError).code; 2043 let message = (err as BusinessError).message; 2044 console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); 2045 } 2046 } 2047} 2048``` 2049## ServiceExtensionContext.startRecentAbility 2050 2051startRecentAbility(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void; 2052 2053启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。启动结果以callback的形式返回开发者。 2054当开发者需要携带启动参数时可以选择此API。仅支持在主线程调用。 2055 2056> **说明:** 2057> 2058> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 2059 2060**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2061 2062**系统接口**:此接口为系统接口。 2063 2064**参数:** 2065 2066| 参数名 | 类型 | 必填 | 说明 | 2067| -------- | -------- | -------- | -------- | 2068| want | [Want](js-apis-app-ability-want.md) | 是 | 需要启动Ability的want信息。 | 2069| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 | 2070| callback | AsyncCallback\<void> | 是 | 指定的回调函数的结果。 | 2071 2072**错误码:** 2073 2074以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2075 2076| 错误码ID | 错误信息 | 2077| ------- | -------- | 2078| 201 | The application does not have permission to call the interface. | 2079| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2080| 16000001 | The specified ability does not exist. | 2081| 16000002 | Incorrect ability type. | 2082| 16000004 | Failed to start the invisible ability. | 2083| 16000005 | The specified process does not have the permission. | 2084| 16000006 | Cross-user operations are not allowed. | 2085| 16000008 | The crowdtesting application expires. | 2086| 16000009 | An ability cannot be started or stopped in Wukong mode. | 2087| 16000010 | The call with the continuation flag is forbidden. | 2088| 16000011 | The context does not exist. | 2089| 16000050 | Internal error. | 2090| 16000053 | The ability is not on the top of the UI. | 2091| 16000055 | Installation-free timed out. | 2092| 16000082 | The UIAbility is being started. | 2093| 16200001 | The caller has been released. | 2094 2095**示例:** 2096 2097```ts 2098import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 2099import { BusinessError } from '@kit.BasicServicesKit'; 2100 2101class EntryAbility extends ServiceExtensionAbility { 2102 onCreate() { 2103 let want: Want = { 2104 deviceId: '', 2105 bundleName: 'com.example.myapplication', 2106 abilityName: 'EntryAbility' 2107 }; 2108 let options: StartOptions = { 2109 windowMode: 0 2110 }; 2111 2112 try { 2113 this.context.startRecentAbility(want, options, (err: BusinessError) => { 2114 if (err.code) { 2115 // 处理业务逻辑错误 2116 console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); 2117 return; 2118 } 2119 // 执行正常业务 2120 console.info('startRecentAbility succeed'); 2121 }); 2122 } catch (err) { 2123 // 处理入参错误异常 2124 let code = (err as BusinessError).code; 2125 let message = (err as BusinessError).message; 2126 console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); 2127 } 2128 } 2129} 2130``` 2131## ServiceExtensionContext.startRecentAbility 2132 2133startRecentAbility(want: Want, options?: StartOptions): Promise\<void>; 2134 2135启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。 2136当开发者期望启动结果以Promise形式返回时可以选择此API。仅支持在主线程调用。 2137 2138> **说明:** 2139> 2140> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 2141 2142**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2143 2144**系统接口**:此接口为系统接口。 2145 2146**参数:** 2147 2148| 参数名 | 类型 | 必填 | 说明 | 2149| -------- | -------- | -------- | -------- | 2150| want | [Want](js-apis-app-ability-want.md) | 是 | 需要启动Ability的want信息。 | 2151| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 | 2152 2153**错误码:** 2154 2155以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2156 2157| 错误码ID | 错误信息 | 2158| ------- | -------- | 2159| 201 | The application does not have permission to call the interface. | 2160| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2161| 16000001 | The specified ability does not exist. | 2162| 16000002 | Incorrect ability type. | 2163| 16000004 | Failed to start the invisible ability. | 2164| 16000005 | The specified process does not have the permission. | 2165| 16000006 | Cross-user operations are not allowed. | 2166| 16000008 | The crowdtesting application expires. | 2167| 16000009 | An ability cannot be started or stopped in Wukong mode. | 2168| 16000010 | The call with the continuation flag is forbidden. | 2169| 16000011 | The context does not exist. | 2170| 16000050 | Internal error. | 2171| 16000053 | The ability is not on the top of the UI. | 2172| 16000055 | Installation-free timed out. | 2173| 16000082 | The UIAbility is being started. | 2174| 16200001 | The caller has been released. | 2175 2176**示例:** 2177 2178```ts 2179import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 2180import { BusinessError } from '@kit.BasicServicesKit'; 2181 2182class EntryAbility extends ServiceExtensionAbility { 2183 onCreate() { 2184 let want: Want = { 2185 bundleName: 'com.example.myapplication', 2186 abilityName: 'EntryAbility' 2187 }; 2188 let options: StartOptions = { 2189 windowMode: 0, 2190 }; 2191 2192 try { 2193 this.context.startRecentAbility(want, options) 2194 .then(() => { 2195 // 执行正常业务 2196 console.info('startRecentAbility succeed'); 2197 }) 2198 .catch((err: BusinessError) => { 2199 // 处理业务逻辑错误 2200 console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); 2201 }); 2202 } catch (err) { 2203 // 处理入参错误异常 2204 let code = (err as BusinessError).code; 2205 let message = (err as BusinessError).message; 2206 console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); 2207 } 2208 } 2209} 2210``` 2211 2212## ServiceExtensionContext.startAbilityByCallWithAccount<sup>10+</sup> 2213 2214startAbilityByCallWithAccount(want: Want, accountId: number): Promise<Caller>; 2215 2216根据accountId对指定的Ability进行call调用,并且可以使用返回的Caller通信接口与被调用方进行通信。仅支持在主线程调用。 2217该接口不支持拉起启动模式为[specified模式](../../application-models/uiability-launch-type.md#specified启动模式)的UIAbility。 2218 2219使用规则: 2220 - 跨用户场景下,Call调用目标Ability时,调用方应用需同时申请`ohos.permission.ABILITY_BACKGROUND_COMMUNICATION`与`ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS`权限 2221 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限 2222 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限 2223 - 同设备与跨设备场景下,该接口的使用规则存在差异,详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md) 2224 2225**需要权限**:ohos.permission.ABILITY_BACKGROUND_COMMUNICATION, ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 2226 2227**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2228 2229**系统接口**:此接口为系统接口。 2230 2231**参数:** 2232 2233| 参数名 | 类型 | 必填 | 说明 | 2234| -------- | -------- | -------- | -------- | 2235| want | [Want](js-apis-app-ability-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId(可选)、parameters(可选),其中deviceId缺省或为空表示启动本地Ability,parameters缺省或为空表示后台启动Ability。 | 2236| accountId | number | 是 | 系统账号的账号ID,-1表示当前活动用户,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1)。 | 2237 2238**返回值:** 2239 2240| 类型 | 说明 | 2241| -------- | -------- | 2242| Promise<Caller> | 获取要通讯的caller对象。 | 2243 2244**错误码:** 2245 2246以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2247 2248| 错误码ID | 错误信息 | 2249| ------- | -------- | 2250| 201 | The application does not have permission to call the interface. | 2251| 202 | The application is not system-app, can not use system-api. | 2252| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2253| 16000001 | The specified ability does not exist. | 2254| 16000002 | Incorrect ability type. | 2255| 16000004 | Failed to start the invisible ability. | 2256| 16000005 | Static permission denied. The specified process does not have the permission. | 2257| 16000006 | Cross-user operations are not allowed. | 2258| 16000008 | The crowdtesting application expires. | 2259| 16000011 | The context does not exist. | 2260| 16000012 | The application is controlled. | 2261| 16000013 | The application is controlled by EDM. | 2262| 16000050 | Internal error. | 2263| 16200001 | The caller has been released. | 2264 2265**示例:** 2266 2267```ts 2268import { ServiceExtensionAbility, Want, Caller } from '@kit.AbilityKit'; 2269import { BusinessError } from '@kit.BasicServicesKit'; 2270 2271class EntryAbility extends ServiceExtensionAbility { 2272 onCreate() { 2273 let caller: Caller; 2274 // 系统账号的账号ID, -1表示当前激活用户 2275 let accountId = -1; 2276 // 指定启动的Ability 2277 let want: Want = { 2278 bundleName: 'com.acts.actscalleeabilityrely', 2279 moduleName: 'entry', 2280 abilityName: 'EntryAbility', 2281 deviceId: '', 2282 parameters: { 2283 // 'ohos.aafwk.param.callAbilityToForeground' 值设置为true时为前台启动, 设置false或不设置为后台启动 2284 'ohos.aafwk.param.callAbilityToForeground': true 2285 } 2286 }; 2287 2288 try { 2289 this.context.startAbilityByCallWithAccount(want, accountId) 2290 .then((obj: Caller) => { 2291 // 执行正常业务 2292 caller = obj; 2293 console.log('startAbilityByCallWithAccount succeed'); 2294 }).catch((error: BusinessError) => { 2295 // 处理业务逻辑错误 2296 console.error(`startAbilityByCallWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`); 2297 }); 2298 } catch (paramError) { 2299 // 处理入参错误异常 2300 console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 2301 } 2302 } 2303} 2304``` 2305 2306## ServiceExtensionContext.requestModalUIExtension<sup>11+<sup> 2307 2308requestModalUIExtension(pickerWant: Want): Promise\<void> 2309 2310请求在指定的前台应用上拉起对应类型的UIExtensionAbility。其中,前台应用通过want.parameters中bundleName来指定,如果未指定前台应用、bundleName指定的应用未在前台或指定的前台应用的bundleName不正确,则在系统界面上直接拉起UIExtensionAbility;被拉起的UIExtensionAbility通过want中bundleName、abilityName、moduleName字段共同确定,同时需要通过want.parameters中的ability.want.params.uiExtensionType字段配置UIExtensionAbility的类型。使用promise形式异步回调。仅支持在主线程调用。 2311 2312在前台应用上拉起UIExtensionAility之前,必须确保该应用已完成页面初始化,否则将导致拉起失败、并出现"uiContent is nullptr"的报错信息。应用可通过监听页面加载状态来判断拉起UIExtensionAbility的时机,页面初始化成功后会出现关键日志信息"UIContentImpl: focus again"。 2313 2314> **说明:** 2315> 2316> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 2317 2318**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2319 2320**系统接口**:此接口为系统接口。 2321 2322**参数:** 2323 2324| 参数名 | 类型 | 必填 | 说明 | 2325| -------- | -------- | -------- | -------- | 2326| pickerWant | [Want](js-apis-app-ability-want.md) | 是 | 拉起UIExtension的want信息。 | 2327 2328**返回值:** 2329 2330| 类型 | 说明 | 2331| -------- | -------- | 2332| Promise<void> | Promise对象。无返回结果的Promise对象。 | 2333 2334**错误码:** 2335 2336以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2337 2338| 错误码ID | 错误信息 | 2339| ------- | -------- | 2340| 202 | The application is not system-app, can not use system-api. | 2341| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2342| 16000050 | Internal error. | 2343 2344**示例:** 2345 2346```ts 2347import { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 2348import { BusinessError } from '@kit.BasicServicesKit'; 2349 2350class ServiceExtension extends ServiceExtensionAbility { 2351 onCreate() { 2352 let pickerWant: Want = { 2353 bundleName: 'com.example.myapplication', 2354 abilityName: 'UIExtAbility', 2355 moduleName: 'entry_test', 2356 parameters: { 2357 'bundleName': 'com.example.myapplication', 2358 //与com.example.myapplication.UIExtAbility配置的type相同 2359 'ability.want.params.uiExtensionType': 'sys/commonUI' 2360 } 2361 }; 2362 2363 try { 2364 this.context.requestModalUIExtension(pickerWant) 2365 .then(() => { 2366 // 执行正常业务 2367 console.info('requestModalUIExtension succeed'); 2368 }) 2369 .catch((err: BusinessError) => { 2370 // 处理业务逻辑错误 2371 console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`); 2372 }); 2373 } catch (err) { 2374 // 处理入参错误异常 2375 let code = (err as BusinessError).code; 2376 let message = (err as BusinessError).message; 2377 console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`); 2378 } 2379 } 2380} 2381``` 2382 2383## ServiceExtensionContext.requestModalUIExtension<sup>11+<sup> 2384 2385requestModalUIExtension(pickerWant: Want, callback: AsyncCallback\<void>): void 2386 2387请求在指定的前台应用上拉起对应类型的UIExtensionAbility。其中,前台应用通过want.parameters中bundleName来指定,如果未指定前台应用、bundleName指定的应用未在前台或指定的前台应用的bundleName不正确,则在系统界面上直接拉起UIExtensionAbility;被拉起的UIExtensionAbility通过want中bundleName、abilityName、moduleName字段共同确定,同时需要通过want.parameters中的ability.want.params.uiExtensionType字段配置UIExtensionAbility的类型。使用callback形式异步回调。仅支持在主线程调用。 2388 2389在前台应用上拉起UIExtensionAility之前,必须确保该应用已完成页面初始化,否则将导致拉起失败、并出现"uiContent is nullptr"的报错信息。应用可通过监听页面加载状态来判断拉起UIExtensionAbility的时机,页面初始化成功后会出现关键日志信息"UIContentImpl: focus again"。 2390 2391> **说明:** 2392> 2393> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 2394 2395**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2396 2397**系统接口**:此接口为系统接口。 2398 2399**参数:** 2400 2401| 参数名 | 类型 | 必填 | 说明 | 2402| -------- | -------- | -------- | -------- | 2403| pickerWant | [Want](js-apis-app-ability-want.md) | 是 | 拉起UIExtension的want信息。 | 2404| callback | AsyncCallback<void> | 是 | 回调函数。当拉起UIExtension成功,err为undefined,否则为错误对象。 | 2405 2406**错误码:** 2407 2408以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2409 2410| 错误码ID | 错误信息 | 2411| ------- | -------- | 2412| 202 | The application is not system-app, can not use system-api. | 2413| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2414| 16000050 | Internal error. | 2415 2416**示例:** 2417 2418```ts 2419import { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 2420import { BusinessError } from '@kit.BasicServicesKit'; 2421 2422class ServiceExtension extends ServiceExtensionAbility { 2423 onCreate() { 2424 let pickerWant: Want = { 2425 bundleName: 'com.example.myapplication', 2426 abilityName: 'com.example.myapplication.UIExtAbility', 2427 moduleName: 'entry_test', 2428 parameters: { 2429 'bundleName': 'com.example.myapplication', 2430 //与com.example.myapplication.UIExtAbility配置的type相同 2431 'ability.want.params.uiExtensionType': 'sys/commonUI' 2432 } 2433 }; 2434 2435 try { 2436 this.context.requestModalUIExtension(pickerWant, (err: BusinessError) => { 2437 if (err.code) { 2438 // 处理业务逻辑错误 2439 console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`); 2440 return; 2441 } 2442 // 执行正常业务 2443 console.info('requestModalUIExtension succeed'); 2444 }); 2445 } catch (err) { 2446 // 处理入参错误异常 2447 let code = (err as BusinessError).code; 2448 let message = (err as BusinessError).message; 2449 console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`); 2450 } 2451 } 2452} 2453``` 2454 2455## ServiceExtensionContext.openLink<sup>12+<sup> 2456openLink(link:string, options?: OpenLinkOptions): Promise<void> 2457 2458通过AppLinking启动UIAbility,使用Promise异步回调。仅支持在主线程调用。 2459 2460通过在link字段中传入标准格式的URL,基于隐式want匹配规则拉起目标UIAbility。目标方必须具备以下过滤器特征,才能处理AppLinking链接: 2461- "actions"列表中包含"ohos.want.action.viewData"。 2462- "entities"列表中包含"entity.system.browsable"。 2463- "uris"列表中包含"scheme"为"https"且"domainVerify"为true的元素。 2464 2465传入的参数不合法时,如未设置必选参数或link字符串不是标准格式的URL,接口会直接抛出异常。参数校验通过,拉起目标方时出现的错误通过promise返回错误信息。 2466 2467> **说明:** 2468> 2469> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 2470 2471**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2472 2473**系统接口**:此接口为系统接口。 2474 2475**参数:** 2476 2477| 参数名 | 类型 | 必填 | 说明 | 2478| -------- | -------- | -------- | -------- | 2479| link | string | 是 | 指示要打开的标准格式URL。 | 2480| options | [OpenLinkOptions](js-apis-app-ability-openLinkOptions.md) | 否 | 打开URL的选项参数。 | 2481 2482**返回值:** 2483 2484| 类型 | 说明 | 2485| -------- | -------- | 2486| Promise<void> | Promise对象。无返回结果的Promise对象。 | 2487 2488**错误码:** 2489 2490以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2491 2492| 错误码ID | 错误信息 | 2493| ------- | -------- | 2494| 201 | The application does not have permission to call the interface. | 2495| 202 | The application is not system-app, can not use system-api. | 2496| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2497| 16000001 | The specified ability does not exist. | 2498| 16000002 | Incorrect ability type. | 2499| 16000004 | Failed to start the invisible ability. | 2500| 16000005 | The specified process does not have the permission. | 2501| 16000006 | Cross-user operations are not allowed. | 2502| 16000008 | The crowdtesting application expires. | 2503| 16000009 | An ability cannot be started or stopped in Wukong mode. | 2504| 16000010 | The call with the continuation flag is forbidden. | 2505| 16000011 | The context does not exist. | 2506| 16000012 | The application is controlled. | 2507| 16000013 | The application is controlled by EDM. | 2508| 16000019 | No matching ability is found. | 2509| 16200001 | The caller has been released. | 2510| 16000082 | The UIAbility is being started. | 2511 2512**示例:** 2513 2514```ts 2515import { ServiceExtensionAbility, Want, OpenLinkOptions } from '@kit.AbilityKit'; 2516import { BusinessError } from '@kit.BasicServicesKit'; 2517 2518function log(info: string) { 2519 console.error(`[ServiceExtApp]:: ${JSON.stringify(info)}`); 2520} 2521 2522export default class ServiceExtAbility extends ServiceExtensionAbility { 2523 onCreate(want: Want) { 2524 log(`ServiceExtAbility OnCreate`); 2525 } 2526 2527 onRequest(want: Want, startId: number) { 2528 log(`ServiceExtAbility onRequest`); 2529 let link: string = 'https://www.example.com'; 2530 let openLinkOptions: OpenLinkOptions = { 2531 appLinkingOnly: false 2532 }; 2533 try { 2534 this.context.openLink( 2535 link, 2536 openLinkOptions 2537 ).then(() => { 2538 log(`open link success.`); 2539 }).catch((err: BusinessError) => { 2540 log(`open link failed, errCode ${JSON.stringify(err.code)}`); 2541 }); 2542 } 2543 catch (e) { 2544 log(`exception occured, errCode ${JSON.stringify(e.code)}`); 2545 } 2546 } 2547 2548 onDestroy() { 2549 log(`ServiceExtAbility onDestroy`); 2550 } 2551} 2552``` 2553 2554## ServiceExtensionContext.preStartMission<sup>12+<sup> 2555preStartMission(bundleName:string, moduleName: string, abilitName: string, startTime: string): Promise<void> 2556 2557打开原子化服务跳过loading框并预打开窗口,使用Promise异步回调。 2558 2559参数校验通过,拉起目标方时出现的错误需要通过异常机制捕获。 2560 2561**需要权限**:ohos.permission.PRE_START_ATOMIC_SERVICE 2562 2563**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2564 2565**系统接口**:此接口为系统接口。 2566 2567**参数:** 2568 2569| 参数名 | 类型 | 必填 | 说明 | 2570| -------- | -------- | -------- | -------- | 2571| bundleName | string | 是 | 打开原子化服务对应的包名。 | 2572| moduleName | string | 是 | 打开原子化服务对应的模块名。 | 2573| abilityName | string | 是 | 打开原子化服务对应的能力名。 | 2574| startTime | string | 是 | 打开原子化服务对应的开始时间,单位为毫秒级的时间戳。 | 2575 2576 2577**返回值:** 2578 2579| 类型 | 说明 | 2580| -------- | -------- | 2581| Promise<void> | Promise对象。无返回结果的Promise对象。 | 2582 2583**错误码:** 2584 2585以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2586 2587| 错误码ID | 错误信息 | 2588| ------- | -------- | 2589| 201 | The application does not have permission to call the interface. | 2590| 202 | The application is not system-app, can not use system-api. | 2591| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2592| 16300007 | The target free install task does not exist. | 2593| 16000011 | The context does not exist. | 2594 2595**示例:** 2596 2597```ts 2598import { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 2599import { BusinessError } from '@kit.BasicServicesKit'; 2600 2601function log(info: string) { 2602 console.error(`[ServiceExtApp]:: ${JSON.stringify(info)}`); 2603} 2604 2605export default class ServiceExtAbility extends ServiceExtensionAbility { 2606 onCreate(want: Want) { 2607 log(`ServiceExtAbility OnCreate`); 2608 } 2609 2610 onRequest(want: Want, startId: number) { 2611 log(`ServiceExtAbility onRequest`); 2612 try { 2613 this.context.preStartMission( 2614 want.bundleName, 2615 want.moduleName, 2616 want.abilityName, 2617 want.parameters["ohos.aafwk.param.startTime"] 2618 ).then(() => { 2619 log(`pre-start mission success.`); 2620 }).catch((err: BusinessError) => { 2621 log(`pre-start mission failed, errCode ${JSON.stringify(err.code)}`); 2622 }); 2623 } 2624 catch (e) { 2625 log(`exception occured, errCode ${JSON.stringify(e.code)}`); 2626 } 2627 } 2628 2629 onDestroy() { 2630 log(`ServiceExtAbility onDestroy`); 2631 } 2632} 2633``` 2634 2635## ServiceExtensionContext.startUIServiceExtensionAbility<sup>13+<sup> 2636startUIServiceExtensionAbility(want: Want): Promise<void> 2637 2638启动一个新的[UIServiceExtensionAbility](js-apis-app-ability-uiServiceExtensionAbility-sys.md)(Promise形式)。 2639 2640 2641> **说明:** 2642> 2643> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 2644> 2645 2646**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2647 2648**系统接口**:此接口为系统接口。 2649 2650**参数:** 2651| 参数名 | 类型 | 只读 | 可选 | 说明 | 2652| ------ | ---- | ---- | -------------------- | -------------------- | 2653| want | [Want](js-apis-app-ability-want.md) | 是 | 否 | [Want](js-apis-app-ability-want.md)类型参数,传入需要启动的Ability的信息,如Ability名称,Bundle名称等。 | 2654 2655**返回值:** 2656 2657| 类型 | 说明 | 2658| ------------------- | -------------------------------------- | 2659| Promise<void> | Promise对象。无返回结果的Promise对象。 | 2660 2661**错误码:** 2662 2663以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2664| 错误码ID | 错误信息 | 2665| -------- | ---------------------------------------------------------------------| 2666| 201 | The application does not have permission to call the interface. | 2667| 202 | The application is not system-app, can not use system-api. | 2668| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2669| 801 | The Ability is not supported. | 2670| 16000001 | The specified ability does not exist. | 2671| 16000002 | Incorrect ability type. | 2672| 16000004 | Failed to start the invisible ability. | 2673| 16000005 | The specified process does not have the permission. | 2674| 16000006 | Cross-user operations are not allowed. | 2675| 16000008 | The crowdtesting application expires. | 2676| 16000011 | The context does not exist. | 2677| 16000012 | The application is controlled. | 2678| 16000013 | The application is controlled by EDM. | 2679| 16000019 | No matching ability is found. | 2680| 16000050 | Internal error. | 2681| 16200001 | The caller has been released. | 2682 2683**示例:** 2684 2685```ts 2686import { BusinessError } from '@ohos.base'; 2687import { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 2688 2689export default class MyServiceExtensionAbility extends ServiceExtensionAbility { 2690 onRequest(want: Want, startId: number) { 2691 const startWant: Want = { 2692 bundleName: 'com.example.myapplication', 2693 abilityName: 'UIServiceExtensionAbility' 2694 } 2695 // 启动一个UIServiceExtensionAbility 2696 this.context.startUIServiceExtensionAbility(startWant).then(() => { 2697 console.info('succeeded'); 2698 }).catch((error: BusinessError) => { 2699 console.error(`error code: ${error.code}, error essage : ${error.message}`); 2700 }) 2701 } 2702} 2703```