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