1# UIAbilityContext (系统接口) 2 3UIAbilityContext是需要保存状态的[UIAbility](js-apis-app-ability-uiAbility.md)所对应的context,继承自[Context](js-apis-inner-application-context.md),提供UIAbility的相关配置信息以及操作UIAbility和ServiceExtensionAbility的方法,如启动UIAbility,停止当前UIAbilityContext所属的UIAbility,启动、停止、连接、断开连接ServiceExtensionAbility等。 4 5> **说明:** 6> 7> - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> - 本模块接口仅可在Stage模型下使用。 9> - 本模块接口为系统接口。 10 11## 导入模块 12 13```ts 14import { common } from '@kit.AbilityKit'; 15``` 16 17> **关于示例代码的说明:** 18> 19> 在本文档的示例中,通过`this.context`来获取`UIAbilityContext`,其中`this`代表继承自`UIAbility`的`UIAbility`实例。如需要在页面中使用`UIAbilityContext`提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。 20 21## UIAbilityContext.startAbilityForResultWithAccount 22 23startAbilityForResultWithAccount(want: Want, accountId: number, callback: AsyncCallback\<AbilityResult>): void 24 25启动一个Ability并在该Ability销毁时返回执行结果。使用callback异步回调。仅支持在主线程调用。 26 27> **说明:** 28> 29> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 30> 当accountId为当前用户时,无需进行权限校验。 31 32**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 33 34**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 35 36**系统接口**:此接口为系统接口。 37 38**参数:** 39 40| 参数名 | 类型 | 必填 | 说明 | 41| -------- | -------- | -------- | -------- | 42| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 43| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | 44| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | 是 | 启动Ability的回调函数,返回Ability结果。 | 45 46**错误码:** 47 48以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 49 50| 错误码ID | 错误信息 | 51| ------- | -------------------------------- | 52| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 53| 16000001 | The specified ability does not exist. | 54| 16000002 | Incorrect ability type. | 55| 16000004 | Failed to start the invisible ability. | 56| 16000005 | The specified process does not have the permission. | 57| 16000006 | Cross-user operations are not allowed. | 58| 16000008 | The crowdtesting application expires. | 59| 16000009 | An ability cannot be started or stopped in Wukong mode. | 60| 16000010 | The call with the continuation flag is forbidden. | 61| 16000011 | The context does not exist. | 62| 16000012 | The application is controlled. | 63| 16000013 | The application is controlled by EDM. | 64| 16000019 | No matching ability is found. | 65| 16000050 | Internal error. | 66| 16000053 | The ability is not on the top of the UI. | 67| 16000055 | Installation-free timed out. | 68| 16000071 | App clone is not supported. | 69| 16000072 | App clone or multi-instance is not supported. | 70| 16000073 | The app clone index is invalid. | 71| 16000076 | The app instance key is invalid. | 72| 16000077 | The number of app instances reaches the limit. | 73| 16000078 | The multi-instance is not supported. | 74| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 75| 16000080 | Creating an instance is not supported. | 76| 16000082 | The UIAbility is being started. | 77| 16200001 | The caller has been released. | 78 79**示例:** 80 81```ts 82import { UIAbility, common, Want } from '@kit.AbilityKit'; 83import { BusinessError } from '@kit.BasicServicesKit'; 84 85export default class EntryAbility extends UIAbility { 86 onForeground() { 87 let want: Want = { 88 deviceId: '', 89 bundleName: 'com.example.myapplication', 90 abilityName: 'EntryAbility' 91 }; 92 let accountId = 100; 93 94 try { 95 this.context.startAbilityForResultWithAccount(want, accountId, (err: BusinessError, result: common.AbilityResult) => { 96 if (err.code) { 97 // 处理业务逻辑错误 98 console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`); 99 return; 100 } 101 // 执行正常业务 102 console.info('startAbilityForResultWithAccount succeed'); 103 }); 104 } catch (err) { 105 // 处理入参错误异常 106 let code = (err as BusinessError).code; 107 let message = (err as BusinessError).message; 108 console.error(`startAbilityForResultWithAccount failed, code is ${code}, message is ${message}`); 109 } 110 } 111} 112``` 113 114 115## UIAbilityContext.startAbilityForResultWithAccount 116 117startAbilityForResultWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void 118 119启动一个Ability并在该Ability销毁时返回执行结果。使用callback异步回调。仅支持在主线程调用。 120 121> **说明:** 122> 123> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 124> 当accountId为当前用户时,无需进行权限校验。 125 126**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 127 128**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 129 130**系统接口**:此接口为系统接口。 131 132**参数:** 133 134| 参数名 | 类型 | 必填 | 说明 | 135| -------- | -------- | -------- | -------- | 136| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 137| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | 138| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 | 139| callback | AsyncCallback\<void\> | 是 | 启动Ability后,Ability被销毁时的回调函数。 | 140 141**错误码:** 142 143以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 144 145| 错误码ID | 错误信息 | 146| ------- | -------------------------------- | 147| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 148| 16000001 | The specified ability does not exist. | 149| 16000002 | Incorrect ability type. | 150| 16000004 | Failed to start the invisible ability. | 151| 16000005 | The specified process does not have the permission. | 152| 16000006 | Cross-user operations are not allowed. | 153| 16000008 | The crowdtesting application expires. | 154| 16000009 | An ability cannot be started or stopped in Wukong mode. | 155| 16000010 | The call with the continuation flag is forbidden. | 156| 16000011 | The context does not exist. | 157| 16000012 | The application is controlled. | 158| 16000013 | The application is controlled by EDM. | 159| 16000019 | No matching ability is found. | 160| 16000050 | Internal error. | 161| 16000053 | The ability is not on the top of the UI. | 162| 16000055 | Installation-free timed out. | 163| 16000071 | App clone is not supported. | 164| 16000072 | App clone or multi-instance is not supported. | 165| 16000073 | The app clone index is invalid. | 166| 16000076 | The app instance key is invalid. | 167| 16000077 | The number of app instances reaches the limit. | 168| 16000078 | The multi-instance is not supported. | 169| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 170| 16000080 | Creating an instance is not supported. | 171| 16000082 | The UIAbility is being started. | 172| 16200001 | The caller has been released. | 173 174**示例:** 175 176```ts 177import { UIAbility, StartOptions, Want } from '@kit.AbilityKit'; 178import { BusinessError } from '@kit.BasicServicesKit'; 179 180export default class EntryAbility extends UIAbility { 181 onForeground() { 182 let want: Want = { 183 deviceId: '', 184 bundleName: 'com.example.myapplication', 185 abilityName: 'EntryAbility' 186 }; 187 let accountId = 100; 188 let options: StartOptions = { 189 displayId: 0 190 }; 191 192 try { 193 this.context.startAbilityForResultWithAccount(want, accountId, options, (err: BusinessError) => { 194 if (err.code) { 195 // 处理业务逻辑错误 196 console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`); 197 return; 198 } 199 // 执行正常业务 200 console.info('startAbilityForResultWithAccount succeed'); 201 }); 202 } catch (err) { 203 // 处理入参错误异常 204 let code = (err as BusinessError).code; 205 let message = (err as BusinessError).message; 206 console.error(`startAbilityForResultWithAccount failed, code is ${code}, message is ${message}`); 207 } 208 } 209} 210``` 211 212 213## UIAbilityContext.startAbilityForResultWithAccount 214 215startAbilityForResultWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<AbilityResult\> 216 217启动一个Ability并在该Ability销毁时返回执行结果。使用Promise异步回调。仅支持在主线程调用。 218 219> **说明:** 220> 221> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 222> 当accountId为当前用户时,无需进行权限校验。 223 224**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 225 226**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 227 228**系统接口**:此接口为系统接口。 229 230**参数:** 231 232| 参数名 | 类型 | 必填 | 说明 | 233| -------- | -------- | -------- | -------- | 234| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 235| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | 236| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 | 237 238**返回值:** 239 240| 类型 | 说明 | 241| -------- | -------- | 242| Promise<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Ability被销毁时的回调函数,包含AbilityResult参数。 | 243 244**错误码:** 245 246以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 247 248| 错误码ID | 错误信息 | 249| ------- | -------------------------------- | 250| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 251| 16000001 | The specified ability does not exist. | 252| 16000002 | Incorrect ability type. | 253| 16000004 | Failed to start the invisible ability. | 254| 16000005 | The specified process does not have the permission. | 255| 16000006 | Cross-user operations are not allowed. | 256| 16000008 | The crowdtesting application expires. | 257| 16000009 | An ability cannot be started or stopped in Wukong mode. | 258| 16000010 | The call with the continuation flag is forbidden. | 259| 16000011 | The context does not exist. | 260| 16000012 | The application is controlled. | 261| 16000013 | The application is controlled by EDM. | 262| 16000019 | No matching ability is found. | 263| 16000050 | Internal error. | 264| 16000053 | The ability is not on the top of the UI. | 265| 16000055 | Installation-free timed out. | 266| 16000071 | App clone is not supported. | 267| 16000072 | App clone or multi-instance is not supported. | 268| 16000073 | The app clone index is invalid. | 269| 16000076 | The app instance key is invalid. | 270| 16000077 | The number of app instances reaches the limit. | 271| 16000078 | The multi-instance is not supported. | 272| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 273| 16000080 | Creating an instance is not supported. | 274| 16000082 | The UIAbility is being started. | 275| 16200001 | The caller has been released. | 276 277**示例:** 278 279```ts 280import { UIAbility, StartOptions, Want, common } from '@kit.AbilityKit'; 281import { BusinessError } from '@kit.BasicServicesKit'; 282 283export default class EntryAbility extends UIAbility { 284 onForeground() { 285 let want: Want = { 286 deviceId: '', 287 bundleName: 'com.example.myapplication', 288 abilityName: 'EntryAbility' 289 }; 290 let accountId = 100; 291 let options: StartOptions = { 292 displayId: 0 293 }; 294 295 try { 296 this.context.startAbilityForResultWithAccount(want, accountId, options) 297 .then((result: common.AbilityResult) => { 298 // 执行正常业务 299 console.info('startAbilityForResultWithAccount succeed'); 300 }) 301 .catch((err: BusinessError) => { 302 // 处理业务逻辑错误 303 console.error(`startAbilityForResultWithAccount failed, code is ${err.code}, message is ${err.message}`); 304 }); 305 } catch (err) { 306 // 处理入参错误异常 307 let code = (err as BusinessError).code; 308 let message = (err as BusinessError).message; 309 console.error(`startAbilityForResultWithAccount failed, code is ${code}, message is ${message}`); 310 } 311 } 312} 313``` 314## UIAbilityContext.startServiceExtensionAbility 315 316startServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void 317 318启动一个新的ServiceExtensionAbility。使用callback异步回调。 319 320**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 321 322**系统接口**:此接口为系统接口。 323 324**参数:** 325 326| 参数名 | 类型 | 必填 | 说明 | 327| -------- | -------- | -------- | -------- | 328| want | [Want](js-apis-app-ability-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 | 329| callback | AsyncCallback\<void\> | 是 | 启动ServiceExtensionAbility的回调函数。 | 330 331**错误码:** 332 333以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 334 335| 错误码ID | 错误信息 | 336| ------- | -------------------------------- | 337| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 338| 16000001 | The specified ability does not exist. | 339| 16000002 | Incorrect ability type. | 340| 16000004 | Failed to start the invisible ability. | 341| 16000005 | The specified process does not have the permission. | 342| 16000006 | Cross-user operations are not allowed. | 343| 16000008 | The crowdtesting application expires. | 344| 16000011 | The context does not exist. | 345| 16000012 | The application is controlled. | 346| 16000013 | The application is controlled by EDM. | 347| 16000019 | No matching ability is found. | 348| 16000050 | Internal error. | 349| 16200001 | The caller has been released. | 350 351**示例:** 352 353```ts 354import { UIAbility, Want } from '@kit.AbilityKit'; 355import { BusinessError } from '@kit.BasicServicesKit'; 356 357export default class EntryAbility extends UIAbility { 358 onForeground() { 359 let want: Want = { 360 deviceId: '', 361 bundleName: 'com.example.myapplication', 362 abilityName: 'ServiceExtensionAbility' 363 }; 364 365 try { 366 this.context.startServiceExtensionAbility(want, (error: BusinessError) => { 367 if (error.code) { 368 // 处理业务逻辑错误 369 console.error(`startServiceExtensionAbility failed, code is ${error.code}, message is ${error.message}`); 370 return; 371 } 372 // 执行正常业务 373 console.info('startServiceExtensionAbility succeed'); 374 }); 375 } catch (err) { 376 // 处理入参错误异常 377 let code = (err as BusinessError).code; 378 let message = (err as BusinessError).message; 379 console.error(`startServiceExtensionAbility failed, code is ${code}, message is ${message}`); 380 } 381 } 382} 383``` 384 385## UIAbilityContext.startServiceExtensionAbility 386 387startServiceExtensionAbility(want: Want): Promise\<void> 388 389启动一个新的ServiceExtensionAbility。使用Promise异步回调。 390 391**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 392 393**系统接口**:此接口为系统接口。 394 395**参数:** 396 397| 参数名 | 类型 | 必填 | 说明 | 398| -------- | -------- | -------- | -------- | 399| want | [Want](js-apis-app-ability-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 | 400 401**错误码:** 402 403以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 404 405| 错误码ID | 错误信息 | 406| ------- | -------------------------------- | 407| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 408| 16000001 | The specified ability does not exist. | 409| 16000002 | Incorrect ability type. | 410| 16000004 | Failed to start the invisible ability. | 411| 16000005 | The specified process does not have the permission. | 412| 16000006 | Cross-user operations are not allowed. | 413| 16000008 | The crowdtesting application expires. | 414| 16000011 | The context does not exist. | 415| 16000012 | The application is controlled. | 416| 16000013 | The application is controlled by EDM. | 417| 16000019 | No matching ability is found. | 418| 16000050 | Internal error. | 419| 16200001 | The caller has been released. | 420 421**示例:** 422 423```ts 424import { UIAbility, Want } from '@kit.AbilityKit'; 425import { BusinessError } from '@kit.BasicServicesKit'; 426 427export default class EntryAbility extends UIAbility { 428 onForeground() { 429 let want: Want = { 430 deviceId: '', 431 bundleName: 'com.example.myapplication', 432 abilityName: 'ServiceExtensionAbility' 433 }; 434 435 try { 436 this.context.startServiceExtensionAbility(want) 437 .then(() => { 438 // 执行正常业务 439 console.info('startServiceExtensionAbility succeed'); 440 }) 441 .catch((err: BusinessError) => { 442 // 处理业务逻辑错误 443 console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`); 444 }); 445 } catch (err) { 446 // 处理入参错误异常 447 let code = (err as BusinessError).code; 448 let message = (err as BusinessError).message; 449 console.error(`startServiceExtensionAbility failed, code is ${code}, message is ${message}`); 450 } 451 } 452} 453``` 454 455## UIAbilityContext.startServiceExtensionAbilityWithAccount 456 457startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void 458 459启动一个新的ServiceExtensionAbility。使用callback异步回调。 460 461> **说明:** 462> 463> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 464> 当accountId为当前用户时,无需进行权限校验。 465 466**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 467 468**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 469 470**系统接口**:此接口为系统接口。 471 472**参数:** 473 474| 参数名 | 类型 | 必填 | 说明 | 475| -------- | -------- | -------- | -------- | 476| want | [Want](js-apis-app-ability-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 | 477| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | 478| callback | AsyncCallback\<void\> | 是 | 启动ServiceExtensionAbility的回调函数。 | 479 480**错误码:** 481 482以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 483 484| 错误码ID | 错误信息 | 485| ------- | -------------------------------- | 486| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 487| 16000001 | The specified ability does not exist. | 488| 16000002 | Incorrect ability type. | 489| 16000004 | Failed to start the invisible ability. | 490| 16000005 | The specified process does not have the permission. | 491| 16000006 | Cross-user operations are not allowed. | 492| 16000008 | The crowdtesting application expires. | 493| 16000011 | The context does not exist. | 494| 16000012 | The application is controlled. | 495| 16000013 | The application is controlled by EDM. | 496| 16000019 | No matching ability is found. | 497| 16000050 | Internal error. | 498| 16200001 | The caller has been released. | 499 500**示例:** 501 502```ts 503import { UIAbility, Want } from '@kit.AbilityKit'; 504import { BusinessError } from '@kit.BasicServicesKit'; 505 506export default class EntryAbility extends UIAbility { 507 onForeground() { 508 let want: Want = { 509 deviceId: '', 510 bundleName: 'com.example.myapplication', 511 abilityName: 'ServiceExtensionAbility' 512 }; 513 let accountId = 100; 514 515 try { 516 this.context.startServiceExtensionAbilityWithAccount(want, accountId, (err: BusinessError) => { 517 if (err.code) { 518 // 处理业务逻辑错误 519 console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); 520 return; 521 } 522 // 执行正常业务 523 console.info('startServiceExtensionAbilityWithAccount succeed'); 524 }); 525 } catch (err) { 526 // 处理入参错误异常 527 let code = (err as BusinessError).code; 528 let message = (err as BusinessError).message; 529 console.error(`startServiceExtensionAbilityWithAccount failed, code is ${code}, message is ${message}`); 530 } 531 } 532} 533``` 534 535## UIAbilityContext.startServiceExtensionAbilityWithAccount 536 537startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void> 538 539启动一个新的ServiceExtensionAbility。使用Promise异步回调。 540 541> **说明:** 542> 543> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 544> 当accountId为当前用户时,无需进行权限校验。 545 546**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 547 548**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 549 550**系统接口**:此接口为系统接口。 551 552**参数:** 553 554| 参数名 | 类型 | 必填 | 说明 | 555| -------- | -------- | -------- | -------- | 556| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 557| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | 558 559**错误码:** 560 561以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 562 563| 错误码ID | 错误信息 | 564| ------- | -------------------------------- | 565| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 566| 16000001 | The specified ability does not exist. | 567| 16000002 | Incorrect ability type. | 568| 16000004 | Failed to start the invisible ability. | 569| 16000005 | The specified process does not have the permission. | 570| 16000006 | Cross-user operations are not allowed. | 571| 16000008 | The crowdtesting application expires. | 572| 16000011 | The context does not exist. | 573| 16000012 | The application is controlled. | 574| 16000013 | The application is controlled by EDM. | 575| 16000019 | No matching ability is found. | 576| 16000050 | Internal error. | 577| 16200001 | The caller has been released. | 578 579**示例:** 580 581```ts 582import { UIAbility, Want } from '@kit.AbilityKit'; 583import { BusinessError } from '@kit.BasicServicesKit'; 584 585export default class EntryAbility extends UIAbility { 586 onForeground() { 587 let want: Want = { 588 deviceId: '', 589 bundleName: 'com.example.myapplication', 590 abilityName: 'ServiceExtensionAbility' 591 }; 592 let accountId = 100; 593 594 try { 595 this.context.startServiceExtensionAbilityWithAccount(want, accountId) 596 .then(() => { 597 // 执行正常业务 598 console.info('startServiceExtensionAbilityWithAccount succeed'); 599 }) 600 .catch((err: BusinessError) => { 601 // 处理业务逻辑错误 602 console.error(`startServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); 603 }); 604 } catch (err) { 605 // 处理入参错误异常 606 let code = (err as BusinessError).code; 607 let message = (err as BusinessError).message; 608 console.error(`startServiceExtensionAbilityWithAccount failed, code is ${code}, message is ${message}`); 609 } 610 } 611} 612``` 613## UIAbilityContext.stopServiceExtensionAbility 614 615stopServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void 616 617停止同一应用程序内的服务。使用callback异步回调。 618 619**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 620 621**系统接口**:此接口为系统接口。 622 623**参数:** 624 625| 参数名 | 类型 | 必填 | 说明 | 626| -------- | -------- | -------- | -------- | 627| want | [Want](js-apis-app-ability-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 | 628| callback | AsyncCallback\<void\> | 是 | 停止ServiceExtensionAbility的回调函数。 | 629 630**错误码:** 631 632以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 633 634| 错误码ID | 错误信息 | 635| ------- | -------------------------------- | 636| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 637| 16000001 | The specified ability does not exist. | 638| 16000002 | Incorrect ability type. | 639| 16000004 | Failed to start the invisible ability. | 640| 16000005 | The specified process does not have the permission. | 641| 16000006 | Cross-user operations are not allowed. | 642| 16000011 | The context does not exist. | 643| 16000012 | The application is controlled. | 644| 16000013 | The application is controlled by EDM. | 645| 16000050 | Internal error. | 646| 16200001 | The caller has been released. | 647 648**示例:** 649 650```ts 651import { UIAbility, Want } from '@kit.AbilityKit'; 652import { BusinessError } from '@kit.BasicServicesKit'; 653 654export default class EntryAbility extends UIAbility { 655 onForeground() { 656 let want: Want = { 657 deviceId: '', 658 bundleName: 'com.example.myapplication', 659 abilityName: 'ServiceExtensionAbility' 660 }; 661 662 try { 663 this.context.stopServiceExtensionAbility(want, (err: BusinessError) => { 664 if (err.code) { 665 // 处理业务逻辑错误 666 console.error(`stopServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`); 667 return; 668 } 669 // 执行正常业务 670 console.info('stopServiceExtensionAbility succeed'); 671 }); 672 } catch (err) { 673 // 处理入参错误异常 674 let code = (err as BusinessError).code; 675 let message = (err as BusinessError).message; 676 console.error(`stopServiceExtensionAbility failed, code is ${code}, message is ${message}`); 677 } 678 } 679} 680``` 681 682## UIAbilityContext.stopServiceExtensionAbility 683 684stopServiceExtensionAbility(want: Want): Promise\<void> 685 686停止同一应用程序内的服务。使用Promise异步回调。 687 688**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 689 690**系统接口**:此接口为系统接口。 691 692**参数:** 693 694| 参数名 | 类型 | 必填 | 说明 | 695| -------- | -------- | -------- | -------- | 696| want | [Want](js-apis-app-ability-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 | 697 698**错误码:** 699 700以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 701 702| 错误码ID | 错误信息 | 703| ------- | -------------------------------- | 704| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 705| 16000001 | The specified ability does not exist. | 706| 16000002 | Incorrect ability type. | 707| 16000004 | Failed to start the invisible ability. | 708| 16000005 | The specified process does not have the permission. | 709| 16000006 | Cross-user operations are not allowed. | 710| 16000011 | The context does not exist. | 711| 16000050 | Internal error. | 712| 16200001 | The caller has been released. | 713 714**示例:** 715 716```ts 717import { UIAbility, Want } from '@kit.AbilityKit'; 718import { BusinessError } from '@kit.BasicServicesKit'; 719 720export default class EntryAbility extends UIAbility { 721 onForeground() { 722 let want: Want = { 723 deviceId: '', 724 bundleName: 'com.example.myapplication', 725 abilityName: 'ServiceExtensionAbility' 726 }; 727 728 try { 729 this.context.stopServiceExtensionAbility(want) 730 .then(() => { 731 // 执行正常业务 732 console.info('stopServiceExtensionAbility succeed'); 733 }) 734 .catch((err: BusinessError) => { 735 // 处理业务逻辑错误 736 console.error(`stopServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`); 737 }); 738 } catch (err) { 739 // 处理入参错误异常 740 let code = (err as BusinessError).code; 741 let message = (err as BusinessError).message; 742 console.error(`stopServiceExtensionAbility failed, code is ${code}, message is ${message}`); 743 } 744 } 745} 746``` 747 748## UIAbilityContext.stopServiceExtensionAbilityWithAccount 749 750stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void 751 752停止同一应用程序内指定账户的服务。使用callback异步回调。 753 754> **说明:** 755> 756> 当accountId为当前用户时,无需进行权限校验。 757 758**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 759 760**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 761 762**系统接口**:此接口为系统接口。 763 764**参数:** 765 766| 参数名 | 类型 | 必填 | 说明 | 767| -------- | -------- | -------- | -------- | 768| want | [Want](js-apis-app-ability-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 | 769| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | 770| callback | AsyncCallback\<void\> | 是 | 停止ServiceExtensionAbility的回调函数。 | 771 772**错误码:** 773 774以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 775 776| 错误码ID | 错误信息 | 777| ------- | -------------------------------- | 778| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 779| 16000001 | The specified ability does not exist. | 780| 16000002 | Incorrect ability type. | 781| 16000004 | Failed to start the invisible ability. | 782| 16000005 | The specified process does not have the permission. | 783| 16000006 | Cross-user operations are not allowed. | 784| 16000011 | The context does not exist. | 785| 16000050 | Internal error. | 786| 16200001 | The caller has been released. | 787 788**示例:** 789 790```ts 791import { UIAbility, Want } from '@kit.AbilityKit'; 792import { BusinessError } from '@kit.BasicServicesKit'; 793 794export default class EntryAbility extends UIAbility { 795 onForeground() { 796 let want: Want = { 797 deviceId: '', 798 bundleName: 'com.example.myapplication', 799 abilityName: 'ServiceExtensionAbility' 800 }; 801 let accountId = 100; 802 803 try { 804 this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (err: BusinessError) => { 805 if (err.code) { 806 // 处理业务逻辑错误 807 console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); 808 return; 809 } 810 // 执行正常业务 811 console.info('stopServiceExtensionAbilityWithAccount succeed'); 812 }); 813 } catch (err) { 814 // 处理入参错误异常 815 let code = (err as BusinessError).code; 816 let message = (err as BusinessError).message; 817 console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${code}, message is ${message}`); 818 } 819 } 820} 821``` 822 823## UIAbilityContext.stopServiceExtensionAbilityWithAccount 824 825stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void> 826 827停止同一应用程序内指定账户的服务。使用Promise异步回调。 828 829> **说明:** 830> 831> 当accountId为当前用户时,无需进行权限校验。 832 833**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 834 835**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 836 837**系统接口**:此接口为系统接口。 838 839**参数:** 840 841| 参数名 | 类型 | 必填 | 说明 | 842| -------- | -------- | -------- | -------- | 843| want | [Want](js-apis-app-ability-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 | 844| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | 845 846**错误码:** 847 848以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 849 850| 错误码ID | 错误信息 | 851| ------- | -------------------------------- | 852| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 853| 16000001 | The specified ability does not exist. | 854| 16000002 | Incorrect ability type. | 855| 16000004 | Failed to start the invisible ability. | 856| 16000005 | The specified process does not have the permission. | 857| 16000006 | Cross-user operations are not allowed. | 858| 16000011 | The context does not exist. | 859| 16000050 | Internal error. | 860| 16200001 | The caller has been released. | 861 862**示例:** 863 864```ts 865import { UIAbility, Want } from '@kit.AbilityKit'; 866import { BusinessError } from '@kit.BasicServicesKit'; 867 868export default class EntryAbility extends UIAbility { 869 onForeground() { 870 let want: Want = { 871 deviceId: '', 872 bundleName: 'com.example.myapplication', 873 abilityName: 'ServiceExtensionAbility' 874 }; 875 let accountId = 100; 876 877 try { 878 this.context.stopServiceExtensionAbilityWithAccount(want, accountId) 879 .then(() => { 880 // 执行正常业务 881 console.info('stopServiceExtensionAbilityWithAccount succeed'); 882 }) 883 .catch((err: BusinessError) => { 884 // 处理业务逻辑错误 885 console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); 886 }); 887 } catch (err) { 888 // 处理入参错误异常 889 let code = (err as BusinessError).code; 890 let message = (err as BusinessError).message; 891 console.error(`stopServiceExtensionAbilityWithAccount failed, code is ${code}, message is ${message}`); 892 } 893 } 894} 895``` 896 897## UIAbilityContext.connectServiceExtensionAbilityWithAccount 898 899connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number 900 901将当前Ability连接到一个指定account的ServiceExtensionAbility。仅支持在主线程调用。 902 903当前仅在phone、tablet设备上生效。 904 905> **说明:** 906> 907> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 908> 当accountId为当前用户时,无需进行权限校验。 909 910**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 911 912**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 913 914**系统接口**:此接口为系统接口。 915 916**参数:** 917 918| 参数名 | 类型 | 必填 | 说明 | 919| -------- | -------- | -------- | -------- | 920| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 921| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | 922| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | 与ServiceExtensionAbility建立连接后回调函数的实例。 | 923 924**返回值:** 925 926| 类型 | 说明 | 927| -------- | -------- | 928| number | 返回Ability连接的结果code。 | 929 930**错误码:** 931 932以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 933 934| 错误码ID | 错误信息 | 935| ------- | -------------------------------- | 936| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 937| 16000001 | The specified ability does not exist. | 938| 16000002 | Incorrect ability type. | 939| 16000004 | Failed to start the invisible ability. | 940| 16000005 | The specified process does not have the permission. | 941| 16000006 | Cross-user operations are not allowed. | 942| 16000008 | The crowdtesting application expires. | 943| 16000053 | The ability is not on the top of the UI. | 944| 16000055 | Installation-free timed out. | 945| 16000011 | The context does not exist. | 946| 16000050 | Internal error. | 947 948**示例:** 949 950```ts 951import { UIAbility, Want, common } from '@kit.AbilityKit'; 952import { rpc } from '@kit.IPCKit'; 953import { BusinessError } from '@kit.BasicServicesKit'; 954 955export default class EntryAbility extends UIAbility { 956 onForeground() { 957 let want: Want = { 958 deviceId: '', 959 bundleName: 'com.example.myapplication', 960 abilityName: 'ServiceExtensionAbility' 961 }; 962 let accountId = 100; 963 let commRemote: rpc.IRemoteObject; 964 let options: common.ConnectOptions = { 965 onConnect(elementName, remote) { 966 commRemote = remote; 967 console.info('onConnect...'); 968 }, 969 onDisconnect(elementName) { 970 console.info('onDisconnect...'); 971 }, 972 onFailed(code) { 973 console.info('onFailed...'); 974 } 975 }; 976 let connection: number; 977 978 try { 979 connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options); 980 } catch (err) { 981 // 处理入参错误异常 982 let code = (err as BusinessError).code; 983 let message = (err as BusinessError).message; 984 console.error(`connectServiceExtensionAbility failed, code is ${code}, message is ${message}`); 985 } 986 } 987} 988``` 989 990## UIAbilityContext.startAbilityWithAccount 991 992startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void\>): void 993 994根据want和accountId启动Ability。使用callback异步回调。仅支持在主线程调用。 995 996> **说明:** 997> 998> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 999> 当accountId为当前用户时,无需进行权限校验。 1000 1001**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1002 1003**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1004 1005**系统接口**:此接口为系统接口。 1006 1007**参数:** 1008 1009| 参数名 | 类型 | 必填 | 说明 | 1010| -------- | -------- | -------- | -------- | 1011| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 1012| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | 1013| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 | 1014 1015**错误码:** 1016 1017以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1018 1019| 错误码ID | 错误信息 | 1020| ------- | -------------------------------- | 1021| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1022| 16000001 | The specified ability does not exist. | 1023| 16000002 | Incorrect ability type. | 1024| 16000004 | Failed to start the invisible ability. | 1025| 16000005 | The specified process does not have the permission. | 1026| 16000006 | Cross-user operations are not allowed. | 1027| 16000008 | The crowdtesting application expires. | 1028| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1029| 16000010 | The call with the continuation flag is forbidden. | 1030| 16000011 | The context does not exist. | 1031| 16000012 | The application is controlled. | 1032| 16000013 | The application is controlled by EDM. | 1033| 16000019 | No matching ability is found. | 1034| 16000050 | Internal error. | 1035| 16000053 | The ability is not on the top of the UI. | 1036| 16000055 | Installation-free timed out. | 1037| 16000071 | App clone is not supported. | 1038| 16000072 | App clone or multi-instance is not supported. | 1039| 16000073 | The app clone index is invalid. | 1040| 16000076 | The app instance key is invalid. | 1041| 16000077 | The number of app instances reaches the limit. | 1042| 16000078 | The multi-instance is not supported. | 1043| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 1044| 16000080 | Creating an instance is not supported. | 1045| 16000082 | The UIAbility is being started. | 1046| 16200001 | The caller has been released. | 1047 1048**示例:** 1049 1050```ts 1051import { UIAbility, Want } from '@kit.AbilityKit'; 1052import { BusinessError } from '@kit.BasicServicesKit'; 1053 1054export default class EntryAbility extends UIAbility { 1055 onForeground() { 1056 let want: Want = { 1057 deviceId: '', 1058 bundleName: 'com.example.myapplication', 1059 abilityName: 'EntryAbility' 1060 }; 1061 let accountId = 100; 1062 1063 try { 1064 this.context.startAbilityWithAccount(want, accountId, (err: BusinessError) => { 1065 if (err.code) { 1066 // 处理业务逻辑错误 1067 console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); 1068 return; 1069 } 1070 // 执行正常业务 1071 console.info('startAbilityWithAccount succeed'); 1072 }); 1073 } catch (err) { 1074 // 处理入参错误异常 1075 let code = (err as BusinessError).code; 1076 let message = (err as BusinessError).message; 1077 console.error(`startAbilityWithAccount failed, code is ${code}, message is ${message}`); 1078 } 1079 } 1080} 1081``` 1082 1083 1084## UIAbilityContext.startAbilityWithAccount 1085 1086startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void 1087 1088根据want、accountId及startOptions启动Ability。使用callback异步回调。仅支持在主线程调用。 1089 1090> **说明:** 1091> 1092> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1093> 当accountId为当前用户时,无需进行权限校验。 1094 1095**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1096 1097**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1098 1099**系统接口**:此接口为系统接口。 1100 1101**参数:** 1102 1103| 参数名 | 类型 | 必填 | 说明 | 1104| -------- | -------- | -------- | -------- | 1105| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 1106| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。| 1107| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 | 1108| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 | 1109 1110**错误码:** 1111 1112以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1113 1114| 错误码ID | 错误信息 | 1115| ------- | -------------------------------- | 1116| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1117| 16000001 | The specified ability does not exist. | 1118| 16000002 | Incorrect ability type. | 1119| 16000004 | Failed to start the invisible ability. | 1120| 16000005 | The specified process does not have the permission. | 1121| 16000006 | Cross-user operations are not allowed. | 1122| 16000008 | The crowdtesting application expires. | 1123| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1124| 16000010 | The call with the continuation flag is forbidden. | 1125| 16000011 | The context does not exist. | 1126| 16000012 | The application is controlled. | 1127| 16000013 | The application is controlled by EDM. | 1128| 16000019 | No matching ability is found. | 1129| 16000050 | Internal error. | 1130| 16000053 | The ability is not on the top of the UI. | 1131| 16000055 | Installation-free timed out. | 1132| 16000071 | App clone is not supported. | 1133| 16000072 | App clone or multi-instance is not supported. | 1134| 16000073 | The app clone index is invalid. | 1135| 16000076 | The app instance key is invalid. | 1136| 16000077 | The number of app instances reaches the limit. | 1137| 16000078 | The multi-instance is not supported. | 1138| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 1139| 16000080 | Creating an instance is not supported. | 1140| 16000082 | The UIAbility is being started. | 1141| 16200001 | The caller has been released. | 1142 1143**示例:** 1144 1145```ts 1146import { UIAbility, Want, StartOptions } from '@kit.AbilityKit'; 1147import { BusinessError } from '@kit.BasicServicesKit'; 1148 1149export default class EntryAbility extends UIAbility { 1150 onForeground() { 1151 let want: Want = { 1152 deviceId: '', 1153 bundleName: 'com.example.myapplication', 1154 abilityName: 'EntryAbility' 1155 }; 1156 let accountId = 100; 1157 let options: StartOptions = { 1158 displayId: 0 1159 }; 1160 1161 try { 1162 this.context.startAbilityWithAccount(want, accountId, options, (err: BusinessError) => { 1163 if (err.code) { 1164 // 处理业务逻辑错误 1165 console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); 1166 return; 1167 } 1168 // 执行正常业务 1169 console.info('startAbilityWithAccount succeed'); 1170 }); 1171 } catch (err) { 1172 // 处理入参错误异常 1173 let code = (err as BusinessError).code; 1174 let message = (err as BusinessError).message; 1175 console.error(`startAbilityWithAccount failed, code is ${code}, message is ${message}`); 1176 } 1177 } 1178} 1179``` 1180 1181 1182## UIAbilityContext.startAbilityWithAccount 1183 1184startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void\> 1185 1186根据want、accountId和startOptions启动Ability。使用Promise异步回调。仅支持在主线程调用。 1187 1188> **说明:** 1189> 1190> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1191> 当accountId为当前用户时,无需进行权限校验。 1192 1193**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1194 1195**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1196 1197**系统接口**:此接口为系统接口。 1198 1199**参数:** 1200 1201| 参数名 | 类型 | 必填 | 说明 | 1202| -------- | -------- | -------- | -------- | 1203| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 1204| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | 1205| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 | 1206 1207**错误码:** 1208 1209以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1210 1211| 错误码ID | 错误信息 | 1212| ------- | -------------------------------- | 1213| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1214| 16000001 | The specified ability does not exist. | 1215| 16000002 | Incorrect ability type. | 1216| 16000004 | Failed to start the invisible ability. | 1217| 16000005 | The specified process does not have the permission. | 1218| 16000006 | Cross-user operations are not allowed. | 1219| 16000008 | The crowdtesting application expires. | 1220| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1221| 16000010 | The call with the continuation flag is forbidden. | 1222| 16000011 | The context does not exist. | 1223| 16000012 | The application is controlled. | 1224| 16000013 | The application is controlled by EDM. | 1225| 16000019 | No matching ability is found. | 1226| 16000050 | Internal error. | 1227| 16000053 | The ability is not on the top of the UI. | 1228| 16000055 | Installation-free timed out. | 1229| 16000071 | App clone is not supported. | 1230| 16000072 | App clone or multi-instance is not supported. | 1231| 16000073 | The app clone index is invalid. | 1232| 16000076 | The app instance key is invalid. | 1233| 16000077 | The number of app instances reaches the limit. | 1234| 16000078 | The multi-instance is not supported. | 1235| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 1236| 16000080 | Creating an instance is not supported. | 1237| 16000082 | The UIAbility is being started. | 1238| 16200001 | The caller has been released. | 1239 1240**示例:** 1241 1242```ts 1243import { UIAbility, Want, StartOptions } from '@kit.AbilityKit'; 1244import { BusinessError } from '@kit.BasicServicesKit'; 1245 1246export default class EntryAbility extends UIAbility { 1247 onForeground() { 1248 let want: Want = { 1249 deviceId: '', 1250 bundleName: 'com.example.myapplication', 1251 abilityName: 'EntryAbility' 1252 }; 1253 let accountId = 100; 1254 let options: StartOptions = { 1255 displayId: 0 1256 }; 1257 1258 try { 1259 this.context.startAbilityWithAccount(want, accountId, options) 1260 .then(() => { 1261 // 执行正常业务 1262 console.info('startAbilityWithAccount succeed'); 1263 }) 1264 .catch((err: BusinessError) => { 1265 // 处理业务逻辑错误 1266 console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); 1267 }); 1268 } catch (err) { 1269 // 处理入参错误异常 1270 let code = (err as BusinessError).code; 1271 let message = (err as BusinessError).message; 1272 console.error(`startAbilityWithAccount failed, code is ${code}, message is ${message}`); 1273 } 1274 } 1275} 1276``` 1277 1278## UIAbilityContext.setMissionIcon 1279 1280setMissionIcon(icon: image.PixelMap, callback: AsyncCallback\<void>): void 1281 1282设置当前Ability在任务中显示的图标,图标大小最大为600M。使用callback异步回调。 1283 1284**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1285 1286**系统接口**:此接口为系统接口。 1287 1288**参数:** 1289 1290| 参数名 | 类型 | 必填 | 说明 | 1291| -------- | -------- | -------- | -------- | 1292| icon | image.PixelMap | 是 | 在最近的任务中显示的Ability图标。 | 1293| callback | AsyncCallback\<void> | 是 | 回调函数。当设置当前Ability在任务中显示的图标成功,err为undefined,否则为错误对象。 | 1294 1295**错误码:** 1296 1297以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1298 1299| 错误码ID | 错误信息 | 1300| ------- | -------------------------------- | 1301| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1302| 16000011 | The context does not exist. | 1303| 16000050 | Internal error. | 1304 1305**示例:** 1306 1307```ts 1308import { UIAbility } from '@kit.AbilityKit'; 1309import { image } from '@kit.ImageKit'; 1310import { BusinessError } from '@kit.BasicServicesKit'; 1311 1312export default class EntryAbility extends UIAbility { 1313 onForeground() { 1314 let imagePixelMap: image.PixelMap; 1315 let color = new ArrayBuffer(0); 1316 image.createPixelMap(color, { 1317 size: { 1318 height: 100, 1319 width: 100 1320 } 1321 }).then((data) => { 1322 imagePixelMap = data; 1323 this.context.setMissionIcon(imagePixelMap, (err: BusinessError) => { 1324 console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`); 1325 }) 1326 }).catch((err: BusinessError) => { 1327 console.error(`createPixelMap failed, code is ${err.code}, message is ${err.message}`); 1328 }); 1329 } 1330} 1331``` 1332 1333 1334## UIAbilityContext.setMissionIcon 1335 1336setMissionIcon(icon: image.PixelMap): Promise\<void> 1337 1338设置当前Ability在任务中显示的图标, 图标大小最大为600M。使用Promise异步回调。 1339 1340**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1341 1342**系统接口**:此接口为系统接口。 1343 1344**参数:** 1345 1346| 参数名 | 类型 | 必填 | 说明 | 1347| -------- | -------- | -------- | -------- | 1348| icon | image.PixelMap | 是 | 在最近的任务中显示的Ability图标。 | 1349 1350**返回值:** 1351 1352| 类型 | 说明 | 1353| -------- | -------- | 1354| Promise<void> | Promise对象。无返回结果的Promise对象。 | 1355 1356**错误码:** 1357 1358以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1359 1360| 错误码ID | 错误信息 | 1361| ------- | -------------------------------- | 1362| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1363| 16000011 | The context does not exist. | 1364| 16000050 | Internal error. | 1365 1366**示例:** 1367 1368```ts 1369import { UIAbility } from '@kit.AbilityKit'; 1370import { image } from '@kit.ImageKit'; 1371import { BusinessError } from '@kit.BasicServicesKit'; 1372 1373export default class EntryAbility extends UIAbility { 1374 onForeground() { 1375 let imagePixelMap: image.PixelMap; 1376 let color = new ArrayBuffer(0); 1377 image.createPixelMap(color, { 1378 size: { 1379 height: 100, 1380 width: 100 1381 } 1382 }).then((data) => { 1383 imagePixelMap = data; 1384 this.context.setMissionIcon(imagePixelMap) 1385 .then(() => { 1386 console.info('setMissionIcon succeed'); 1387 }) 1388 .catch((err: BusinessError) => { 1389 console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`); 1390 }); 1391 }).catch((err: BusinessError) => { 1392 console.error(`createPixelMap failed, code is ${err.code}, message is ${err.message}`); 1393 }); 1394 } 1395} 1396``` 1397 1398## UIAbilityContext.startRecentAbility 1399 1400startRecentAbility(want: Want, callback: AsyncCallback<void>): void 1401 1402启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。使用callback异步回调。仅支持在主线程调用。 1403 1404> **说明:** 1405> 1406> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1407 1408**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1409 1410**系统接口**:此接口为系统接口。 1411 1412**参数:** 1413 1414| 参数名 | 类型 | 必填 | 说明 | 1415| -------- | -------- | -------- | -------- | 1416| want | [Want](js-apis-app-ability-want.md) | 是 | 需要启动Ability的want信息。 | 1417| callback | AsyncCallback\<void> | 是 | 指定的回调函数的结果。 | 1418 1419**错误码:** 1420 1421以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1422 1423| 错误码ID | 错误信息 | 1424| ------- | -------------------------------- | 1425| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1426| 16000001 | The specified ability does not exist. | 1427| 16000002 | Incorrect ability type. | 1428| 16000004 | Failed to start the invisible ability. | 1429| 16000005 | The specified process does not have the permission. | 1430| 16000006 | Cross-user operations are not allowed. | 1431| 16000008 | The crowdtesting application expires. | 1432| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1433| 16000010 | The call with the continuation flag is forbidden. | 1434| 16000011 | The context does not exist. | 1435| 16000012 | The application is controlled. | 1436| 16000013 | The application is controlled by EDM. | 1437| 16000050 | Internal error. | 1438| 16000053 | The ability is not on the top of the UI. | 1439| 16000055 | Installation-free timed out. | 1440| 16000082 | The UIAbility is being started. | 1441| 16200001 | The caller has been released. | 1442| 16000073 | The app clone index is invalid. | 1443 1444**示例:** 1445 1446```ts 1447import { UIAbility, Want } from '@kit.AbilityKit'; 1448import { BusinessError } from '@kit.BasicServicesKit'; 1449 1450export default class EntryAbility extends UIAbility { 1451 onForeground() { 1452 let want: Want = { 1453 bundleName: 'com.example.myapplication', 1454 abilityName: 'EntryAbility' 1455 }; 1456 1457 try { 1458 this.context.startRecentAbility(want, (err: BusinessError) => { 1459 if (err.code) { 1460 // 处理业务逻辑错误 1461 console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); 1462 return; 1463 } 1464 // 执行正常业务 1465 console.info('startRecentAbility succeed'); 1466 }); 1467 } catch (err) { 1468 // 处理入参错误异常 1469 let code = (err as BusinessError).code; 1470 let message = (err as BusinessError).message; 1471 console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); 1472 } 1473 } 1474} 1475``` 1476## UIAbilityContext.startRecentAbility 1477 1478startRecentAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void 1479 1480启动一个指定的Ability。如果这个Ability有多个实例,将拉起最近启动的那个实例。当开发者需要携带启动参数时可以选择此API。使用callback异步回调。仅支持在主线程调用。 1481 1482> **说明:** 1483> 1484> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1485 1486**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1487 1488**系统接口**:此接口为系统接口。 1489 1490**参数:** 1491 1492| 参数名 | 类型 | 必填 | 说明 | 1493| -------- | -------- | -------- | -------- | 1494| want | [Want](js-apis-app-ability-want.md) | 是 | 需要启动Ability的want信息。 | 1495| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 | 1496| callback | AsyncCallback\<void> | 是 | 指定的回调函数的结果。 | 1497 1498**错误码:** 1499 1500以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1501 1502| 错误码ID | 错误信息 | 1503| ------- | -------------------------------- | 1504| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1505| 16000001 | The specified ability does not exist. | 1506| 16000002 | Incorrect ability type. | 1507| 16000004 | Failed to start the invisible ability. | 1508| 16000005 | The specified process does not have the permission. | 1509| 16000006 | Cross-user operations are not allowed. | 1510| 16000008 | The crowdtesting application expires. | 1511| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1512| 16000010 | The call with the continuation flag is forbidden. | 1513| 16000011 | The context does not exist. | 1514| 16000012 | The application is controlled. | 1515| 16000013 | The application is controlled by EDM. | 1516| 16000050 | Internal error. | 1517| 16000053 | The ability is not on the top of the UI. | 1518| 16000055 | Installation-free timed out. | 1519| 16000082 | The UIAbility is being started. | 1520| 16200001 | The caller has been released. | 1521| 16000073 | The app clone index is invalid. | 1522 1523**示例:** 1524 1525```ts 1526import { UIAbility, Want, StartOptions } from '@kit.AbilityKit'; 1527import { BusinessError } from '@kit.BasicServicesKit'; 1528 1529export default class EntryAbility extends UIAbility { 1530 onForeground() { 1531 let want: Want = { 1532 deviceId: '', 1533 bundleName: 'com.example.myapplication', 1534 abilityName: 'EntryAbility' 1535 }; 1536 let options: StartOptions = { 1537 displayId: 0 1538 }; 1539 1540 try { 1541 this.context.startRecentAbility(want, options, (err: BusinessError) => { 1542 if (err.code) { 1543 // 处理业务逻辑错误 1544 console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); 1545 return; 1546 } 1547 // 执行正常业务 1548 console.info('startRecentAbility succeed'); 1549 }); 1550 } catch (err) { 1551 // 处理入参错误异常 1552 let code = (err as BusinessError).code; 1553 let message = (err as BusinessError).message; 1554 console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); 1555 } 1556 } 1557} 1558``` 1559## UIAbilityContext.startRecentAbility 1560 1561startRecentAbility(want: Want, options?: StartOptions): Promise<void> 1562 1563启动一个指定的Ability。如果这个Ability有多个实例,将拉起最近启动的那个实例。使用Promise异步回调。仅支持在主线程调用。 1564 1565> **说明:** 1566> 1567> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1568 1569**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1570 1571**系统接口**:此接口为系统接口。 1572 1573**参数:** 1574 1575| 参数名 | 类型 | 必填 | 说明 | 1576| -------- | -------- | -------- | -------- | 1577| want | [Want](js-apis-app-ability-want.md) | 是 | 需要启动Ability的want信息。 | 1578| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 | 1579 1580**错误码:** 1581 1582以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1583 1584| 错误码ID | 错误信息 | 1585| ------- | -------------------------------- | 1586| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1587| 16000001 | The specified ability does not exist. | 1588| 16000002 | Incorrect ability type. | 1589| 16000004 | Failed to start the invisible ability. | 1590| 16000005 | The specified process does not have the permission. | 1591| 16000006 | Cross-user operations are not allowed. | 1592| 16000008 | The crowdtesting application expires. | 1593| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1594| 16000010 | The call with the continuation flag is forbidden. | 1595| 16000011 | The context does not exist. | 1596| 16000012 | The application is controlled. | 1597| 16000013 | The application is controlled by EDM. | 1598| 16000050 | Internal error. | 1599| 16000053 | The ability is not on the top of the UI. | 1600| 16000055 | Installation-free timed out. | 1601| 16000082 | The UIAbility is being started. | 1602| 16200001 | The caller has been released. | 1603| 16000073 | The app clone index is invalid. | 1604 1605**示例:** 1606 1607```ts 1608import { UIAbility, Want, StartOptions } from '@kit.AbilityKit'; 1609import { BusinessError } from '@kit.BasicServicesKit'; 1610 1611export default class EntryAbility extends UIAbility { 1612 onForeground() { 1613 let want: Want = { 1614 bundleName: 'com.example.myapplication', 1615 abilityName: 'EntryAbility' 1616 }; 1617 let options: StartOptions = { 1618 displayId: 0, 1619 }; 1620 1621 try { 1622 this.context.startRecentAbility(want, options) 1623 .then(() => { 1624 // 执行正常业务 1625 console.info('startRecentAbility succeed'); 1626 }) 1627 .catch((err: BusinessError) => { 1628 // 处理业务逻辑错误 1629 console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); 1630 }); 1631 } catch (err) { 1632 // 处理入参错误异常 1633 let code = (err as BusinessError).code; 1634 let message = (err as BusinessError).message; 1635 console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); 1636 } 1637 } 1638} 1639``` 1640 1641## UIAbilityContext.startAbilityByCallWithAccount<sup>10+</sup> 1642 1643startAbilityByCallWithAccount(want: Want, accountId: number): Promise<Caller> 1644 1645根据accountId对指定的Ability进行call调用,并且可以使用返回的Caller通信接口与被调用方进行通信。仅支持在主线程调用。 1646该接口不支持拉起启动模式为[specified模式](../../application-models/uiability-launch-type.md#specified启动模式)的UIAbility。 1647 1648使用规则: 1649 - 跨用户场景下,Call调用目标Ability时,调用方应用需同时申请`ohos.permission.ABILITY_BACKGROUND_COMMUNICATION`与`ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS`权限。 1650 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限。 1651 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限。 1652 - 同设备与跨设备场景下,该接口的使用规则存在差异,详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1653 1654**需要权限**:ohos.permission.ABILITY_BACKGROUND_COMMUNICATION, ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1655 1656**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1657 1658**系统接口**:此接口为系统接口。 1659 1660**参数:** 1661 1662| 参数名 | 类型 | 必填 | 说明 | 1663| -------- | -------- | -------- | -------- | 1664| want | [Want](js-apis-app-ability-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId(可选)、parameters(可选),其中deviceId缺省或为空表示启动本地Ability,parameters缺省或为空表示后台启动Ability。 | 1665| accountId | number | 是 | 系统账号的账号ID,-1表示当前活动用户,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | 1666 1667**返回值:** 1668 1669| 类型 | 说明 | 1670| -------- | -------- | 1671| Promise<[Caller](js-apis-app-ability-uiAbility.md#caller)> | 获取要通讯的caller对象。 | 1672 1673**错误码:** 1674 1675以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1676 1677| 错误码ID | 错误信息 | 1678| ------- | -------------------------------- | 1679| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1680| 16000001 | The specified ability does not exist. | 1681| 16000002 | Incorrect ability type. | 1682| 16000004 | Failed to start the invisible ability. | 1683| 16000005 | Static permission denied. The specified process does not have the permission. | 1684| 16000006 | Cross-user operations are not allowed. | 1685| 16000008 | The crowdtesting application expires. | 1686| 16000011 | The context does not exist. | 1687| 16000012 | The application is controlled. | 1688| 16000013 | The application is controlled by EDM. | 1689| 16000050 | Internal error. | 1690| 16000071 | App clone is not supported. | 1691| 16000072 | App clone or multi-instance is not supported. | 1692| 16000073 | The app clone index is invalid. | 1693| 16000076 | The app instance key is invalid. | 1694| 16000077 | The number of app instances reaches the limit. | 1695| 16000078 | The multi-instance is not supported. | 1696| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 1697| 16000080 | Creating an instance is not supported. | 1698| 16200001 | The caller has been released. | 1699 1700**示例:** 1701 1702```ts 1703import { UIAbility, Want, Caller } from '@kit.AbilityKit'; 1704import { BusinessError } from '@kit.BasicServicesKit'; 1705 1706export default class EntryAbility extends UIAbility { 1707 onForeground() { 1708 let caller: Caller; 1709 // 系统账号的账号ID, -1表示当前激活用户 1710 let accountId = -1; 1711 // 指定启动的Ability 1712 let want: Want = { 1713 bundleName: 'com.acts.actscalleeabilityrely', 1714 moduleName: 'entry', 1715 abilityName: 'EntryAbility', 1716 deviceId: '', 1717 parameters: { 1718 // 'ohos.aafwk.param.callAbilityToForeground' 值设置为true时为前台启动, 设置false或不设置为后台启动 1719 'ohos.aafwk.param.callAbilityToForeground': true 1720 } 1721 }; 1722 1723 try { 1724 this.context.startAbilityByCallWithAccount(want, accountId) 1725 .then((obj: Caller) => { 1726 // 执行正常业务 1727 caller = obj; 1728 console.log('startAbilityByCallWithAccount succeed'); 1729 }).catch((error: BusinessError) => { 1730 // 处理业务逻辑错误 1731 console.error(`startAbilityByCallWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`); 1732 }); 1733 } catch (paramError) { 1734 // 处理入参错误异常 1735 console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`); 1736 } 1737 } 1738} 1739``` 1740 1741## UIAbilityContext.startAbilityAsCaller<sup>10+<sup> 1742 1743startAbilityAsCaller(want: Want, callback: AsyncCallback\<void>): void 1744 1745使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用callback异步回调。仅支持在主线程调用。 1746 1747> **说明:** 1748> 1749> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1750 1751**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1752 1753**系统接口**:此接口为系统接口。 1754 1755**参数:** 1756 1757| 参数名 | 类型 | 必填 | 说明 | 1758| -------- | -------- | -------- | -------- | 1759| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 1760| callback | AsyncCallback<void> | 是 | 回调函数。当启动Ability成功,err为undefined,否则为错误对象。 | 1761 1762**错误码:** 1763 1764以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1765 1766| 错误码ID | 错误信息 | 1767| ------- | -------------------------------- | 1768| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1769| 16000001 | The specified ability does not exist. | 1770| 16000002 | Incorrect ability type. | 1771| 16000004 | Failed to start the invisible ability. | 1772| 16000005 | The specified process does not have the permission. | 1773| 16000006 | Cross-user operations are not allowed. | 1774| 16000008 | The crowdtesting application expires. | 1775| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1776| 16000010 | The call with the continuation flag is forbidden. | 1777| 16000011 | The context does not exist. | 1778| 16000012 | The application is controlled. | 1779| 16000013 | The application is controlled by EDM. | 1780| 16000050 | Internal error. | 1781| 16000053 | The ability is not on the top of the UI. | 1782| 16000055 | Installation-free timed out. | 1783| 16000071 | App clone is not supported. | 1784| 16000072 | App clone or multi-instance is not supported. | 1785| 16000073 | The app clone index is invalid. | 1786| 16000076 | The app instance key is invalid. | 1787| 16000077 | The number of app instances reaches the limit. | 1788| 16000078 | The multi-instance is not supported. | 1789| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 1790| 16000080 | Creating an instance is not supported. | 1791| 16000082 | The UIAbility is being started. | 1792| 16200001 | The caller has been released. | 1793 1794**示例:** 1795 1796```ts 1797import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit'; 1798 1799export default class EntryAbility extends UIAbility { 1800 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 1801 // want包含启动该应用的Caller信息 1802 let localWant: Want = want; 1803 localWant.bundleName = 'com.example.demo'; 1804 localWant.moduleName = 'entry'; 1805 localWant.abilityName = 'TestAbility'; 1806 1807 // 使用启动方的Caller身份信息启动新Ability 1808 this.context.startAbilityAsCaller(localWant, (err) => { 1809 if (err && err.code != 0) { 1810 console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err)); 1811 } else { 1812 console.log('startAbilityAsCaller success.'); 1813 } 1814 }) 1815 } 1816} 1817``` 1818 1819## UIAbilityContext.startAbilityAsCaller<sup>10+<sup> 1820 1821startAbilityAsCaller(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void 1822 1823使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用callback异步回调。仅支持在主线程调用。 1824 1825> **说明:** 1826> 1827> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1828 1829**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1830 1831**系统接口**:此接口为系统接口。 1832 1833**参数:** 1834 1835| 参数名 | 类型 | 必填 | 说明 | 1836| -------- | -------- | -------- | -------- | 1837| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 1838| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 | 1839| callback | AsyncCallback<void> | 是 | 回调函数。当启动Ability成功,err为undefined,否则为错误对象。 | 1840 1841**错误码:** 1842 1843以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1844 1845| 错误码ID | 错误信息 | 1846| ------- | -------------------------------- | 1847| 401 | 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.| 1848| 16000001 | The specified ability does not exist. | 1849| 16000004 | Failed to start the invisible ability. | 1850| 16000005 | The specified process does not have the permission. | 1851| 16000006 | Cross-user operations are not allowed. | 1852| 16000008 | The crowdtesting application expires. | 1853| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1854| 16000011 | The context does not exist. | 1855| 16000012 | The application is controlled. | 1856| 16000013 | The application is controlled by EDM. | 1857| 16000050 | Internal error. | 1858| 16000053 | The ability is not on the top of the UI. | 1859| 16000055 | Installation-free timed out. | 1860| 16000071 | App clone is not supported. | 1861| 16000072 | App clone or multi-instance is not supported. | 1862| 16000073 | The app clone index is invalid. | 1863| 16000076 | The app instance key is invalid. | 1864| 16000077 | The number of app instances reaches the limit. | 1865| 16000078 | The multi-instance is not supported. | 1866| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 1867| 16000080 | Creating an instance is not supported. | 1868| 16000082 | The UIAbility is being started. | 1869| 16200001 | The caller has been released. | 1870 1871**示例:** 1872 1873```ts 1874import { UIAbility, Want, AbilityConstant, StartOptions } from '@kit.AbilityKit'; 1875 1876export default class EntryAbility extends UIAbility { 1877 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 1878 // want包含启动该应用的Caller信息 1879 let localWant: Want = want; 1880 localWant.bundleName = 'com.example.demo'; 1881 localWant.moduleName = 'entry'; 1882 localWant.abilityName = 'TestAbility'; 1883 let option: StartOptions = { 1884 displayId: 0 1885 }; 1886 1887 // 使用启动方的Caller身份信息启动新Ability 1888 this.context.startAbilityAsCaller(localWant, option, (err) => { 1889 if (err && err.code != 0) { 1890 console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err)); 1891 } else { 1892 console.log('startAbilityAsCaller success.'); 1893 } 1894 }) 1895 } 1896} 1897``` 1898 1899## UIAbilityContext.startAbilityAsCaller<sup>10+<sup> 1900 1901startAbilityAsCaller(want: Want, options?: StartOptions): Promise\<void> 1902 1903使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用Promise异步回调。仅支持在主线程调用。 1904 1905> **说明:** 1906> 1907> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1908 1909**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1910 1911**系统接口**:此接口为系统接口。 1912 1913**参数:** 1914 1915| 参数名 | 类型 | 必填 | 说明 | 1916| -------- | -------- | -------- | -------- | 1917| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 1918| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 | 1919 1920**返回值:** 1921 1922| 类型 | 说明 | 1923| -------- | -------- | 1924| Promise<void> | Promise对象。无返回结果的Promise对象。 | 1925 1926**错误码:** 1927 1928以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1929 1930| 错误码ID | 错误信息 | 1931| ------- | -------------------------------- | 1932| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1933| 16000001 | The specified ability does not exist. | 1934| 16000002 | Incorrect ability type. | 1935| 16000004 | Failed to start the invisible ability. | 1936| 16000005 | The specified process does not have the permission. | 1937| 16000006 | Cross-user operations are not allowed. | 1938| 16000008 | The crowdtesting application expires. | 1939| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1940| 16000010 | The call with the continuation flag is forbidden. | 1941| 16000011 | The context does not exist. | 1942| 16000012 | The application is controlled. | 1943| 16000013 | The application is controlled by EDM. | 1944| 16000050 | Internal error. | 1945| 16000053 | The ability is not on the top of the UI. | 1946| 16000055 | Installation-free timed out. | 1947| 16000071 | App clone is not supported. | 1948| 16000072 | App clone or multi-instance is not supported. | 1949| 16000073 | The app clone index is invalid. | 1950| 16000076 | The app instance key is invalid. | 1951| 16000077 | The number of app instances reaches the limit. | 1952| 16000078 | The multi-instance is not supported. | 1953| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 1954| 16000080 | Creating an instance is not supported. | 1955| 16000082 | The UIAbility is being started. | 1956| 16200001 | The caller has been released. | 1957 1958**示例:** 1959 1960```ts 1961import { UIAbility, Want, AbilityConstant, StartOptions } from '@kit.AbilityKit'; 1962import { BusinessError } from '@kit.BasicServicesKit'; 1963 1964export default class EntryAbility extends UIAbility { 1965 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 1966 // want包含启动该应用的Caller信息 1967 let localWant: Want = want; 1968 localWant.bundleName = 'com.example.demo'; 1969 localWant.moduleName = 'entry'; 1970 localWant.abilityName = 'TestAbility'; 1971 let option: StartOptions = { 1972 displayId: 0 1973 }; 1974 1975 // 使用启动方的Caller身份信息启动新Ability 1976 this.context.startAbilityAsCaller(localWant, option) 1977 .then(() => { 1978 console.log('startAbilityAsCaller success.'); 1979 }) 1980 .catch((err: BusinessError) => { 1981 console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err)); 1982 }) 1983 } 1984} 1985``` 1986 1987## UIAbilityContext.requestModalUIExtension<sup>11+<sup> 1988 1989requestModalUIExtension(pickerWant: Want): Promise\<void> 1990 1991请求在指定的前台应用上拉起对应类型的UIExtensionAbility。使用Promise异步回调。仅支持在主线程调用。 1992 1993其中,前台应用通过want.parameters中bundleName来指定,如果未指定前台应用、bundleName指定的应用未在前台或指定的前台应用的bundleName不正确,则在系统界面上直接拉起UIExtensionAbility;被拉起的UIExtensionAbility通过want中bundleName、abilityName、moduleName字段共同确定,同时需要通过want.parameters中的ability.want.params.uiExtensionType字段配置UIExtensionAbility的类型。 1994 1995在前台应用上拉起UIExtensionAility之前,必须确保该应用已完成页面初始化,否则将导致拉起失败、并出现"uiContent is nullptr"的报错信息。应用可通过监听页面加载状态来判断拉起UIExtensionAbility的时机,页面初始化成功后会出现关键日志信息"UIContentImpl: focus again"。 1996 1997> **说明:** 1998> 1999> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 2000 2001**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2002 2003**系统接口**:此接口为系统接口。 2004 2005**参数:** 2006 2007| 参数名 | 类型 | 必填 | 说明 | 2008| -------- | -------- | -------- | -------- | 2009| pickerWant | [Want](js-apis-app-ability-want.md) | 是 | 拉起UIExtension的want信息。 | 2010 2011**返回值:** 2012 2013| 类型 | 说明 | 2014| -------- | -------- | 2015| Promise<void> | Promise对象。无返回结果的Promise对象。 | 2016 2017**错误码:** 2018 2019以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2020 2021| 错误码ID | 错误信息 | 2022| ------- | -------------------------------- | 2023| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2024| 16000050 | Internal error. | 2025 2026**示例:** 2027 2028```ts 2029import { UIAbility, Want } from '@kit.AbilityKit'; 2030import { BusinessError } from '@kit.BasicServicesKit'; 2031 2032export default class EntryAbility extends UIAbility { 2033 onForeground() { 2034 let want: Want = { 2035 bundleName: 'com.example.myapplication', 2036 abilityName: 'com.example.myapplication.UIExtAbility', 2037 moduleName: 'entry_test', 2038 parameters: { 2039 'bundleName': 'com.example.myapplication', 2040 //与com.example.myapplication.UIExtAbility配置的type相同 2041 'ability.want.params.uiExtensionType': 'sys/commonUI' 2042 } 2043 }; 2044 2045 try { 2046 this.context.requestModalUIExtension(want) 2047 .then(() => { 2048 // 执行正常业务 2049 console.info('requestModalUIExtension succeed'); 2050 }) 2051 .catch((err: BusinessError) => { 2052 // 处理业务逻辑错误 2053 console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`); 2054 }); 2055 } catch (err) { 2056 // 处理入参错误异常 2057 let code = (err as BusinessError).code; 2058 let message = (err as BusinessError).message; 2059 console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`); 2060 } 2061 } 2062} 2063``` 2064 2065## UIAbilityContext.requestModalUIExtension<sup>11+<sup> 2066requestModalUIExtension(pickerWant: Want, callback: AsyncCallback\<void>): void 2067 2068请求在指定的前台应用上拉起对应类型的UIExtensionAbility。使用callback异步回调。仅支持在主线程调用。 2069 2070其中,前台应用通过want.parameters中bundleName来指定,如果未指定前台应用、bundleName指定的应用未在前台或指定的前台应用的bundleName不正确,则在系统界面上直接拉起UIExtensionAbility;被拉起的UIExtensionAbility通过want中bundleName、abilityName、moduleName字段共同确定,同时需要通过want.parameters中的ability.want.params.uiExtensionType字段配置UIExtensionAbility的类型。 2071 2072在前台应用上拉起UIExtensionAility之前,必须确保该应用已完成页面初始化,否则将导致拉起失败、并出现"uiContent is nullptr"的报错信息。应用可通过监听页面加载状态来判断拉起UIExtensionAbility的时机,页面初始化成功后会出现关键日志信息"UIContentImpl: focus again"。 2073 2074> **说明:** 2075> 2076> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 2077 2078**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2079 2080**系统接口**:此接口为系统接口。 2081 2082**参数:** 2083 2084| 参数名 | 类型 | 必填 | 说明 | 2085| -------- | -------- | -------- | -------- | 2086| pickerWant | [Want](js-apis-app-ability-want.md) | 是 | 拉起UIExtension的want信息。 | 2087| callback | AsyncCallback<void> | 是 | 回调函数。当拉起UIExtension成功,err为undefined,否则为错误对象。 | 2088 2089**错误码:** 2090 2091以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2092 2093| 错误码ID | 错误信息 | 2094| ------- | -------------------------------- | 2095| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2096| 16000050 | Internal error. | 2097 2098**示例:** 2099 2100```ts 2101import { UIAbility, Want } from '@kit.AbilityKit'; 2102import { BusinessError } from '@kit.BasicServicesKit'; 2103 2104export default class EntryAbility extends UIAbility { 2105 onForeground() { 2106 let want: Want = { 2107 bundleName: 'com.example.myapplication', 2108 abilityName: 'UIExtAbility', 2109 moduleName: 'entry_test', 2110 parameters: { 2111 'bundleName': 'com.example.myapplication', 2112 //与com.example.myapplication.UIExtAbility配置的type相同 2113 'ability.want.params.uiExtensionType': 'sys/commonUI' 2114 } 2115 }; 2116 2117 try { 2118 this.context.requestModalUIExtension(want, (err: BusinessError) => { 2119 if (err.code) { 2120 // 处理业务逻辑错误 2121 console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`); 2122 return; 2123 } 2124 // 执行正常业务 2125 console.info('requestModalUIExtension succeed'); 2126 }); 2127 } catch (err) { 2128 // 处理入参错误异常 2129 let code = (err as BusinessError).code; 2130 let message = (err as BusinessError).message; 2131 console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`); 2132 } 2133 } 2134} 2135``` 2136