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> **说明:** 904> 905> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 906> 当accountId为当前用户时,无需进行权限校验。 907 908**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 909 910**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 911 912**系统接口**:此接口为系统接口。 913 914**参数:** 915 916| 参数名 | 类型 | 必填 | 说明 | 917| -------- | -------- | -------- | -------- | 918| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 919| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | 920| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | 与ServiceExtensionAbility建立连接后回调函数的实例。 | 921 922**返回值:** 923 924| 类型 | 说明 | 925| -------- | -------- | 926| number | 返回Ability连接的结果code。 | 927 928**错误码:** 929 930以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 931 932| 错误码ID | 错误信息 | 933| ------- | -------------------------------- | 934| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 935| 16000001 | The specified ability does not exist. | 936| 16000002 | Incorrect ability type. | 937| 16000004 | Failed to start the invisible ability. | 938| 16000005 | The specified process does not have the permission. | 939| 16000006 | Cross-user operations are not allowed. | 940| 16000008 | The crowdtesting application expires. | 941| 16000053 | The ability is not on the top of the UI. | 942| 16000055 | Installation-free timed out. | 943| 16000011 | The context does not exist. | 944| 16000050 | Internal error. | 945 946**示例:** 947 948```ts 949import { UIAbility, Want, common } from '@kit.AbilityKit'; 950import { rpc } from '@kit.IPCKit'; 951import { BusinessError } from '@kit.BasicServicesKit'; 952 953export default class EntryAbility extends UIAbility { 954 onForeground() { 955 let want: Want = { 956 deviceId: '', 957 bundleName: 'com.example.myapplication', 958 abilityName: 'ServiceExtensionAbility' 959 }; 960 let accountId = 100; 961 let commRemote: rpc.IRemoteObject; 962 let options: common.ConnectOptions = { 963 onConnect(elementName, remote) { 964 commRemote = remote; 965 console.info('onConnect...'); 966 }, 967 onDisconnect(elementName) { 968 console.info('onDisconnect...'); 969 }, 970 onFailed(code) { 971 console.info('onFailed...'); 972 } 973 }; 974 let connection: number; 975 976 try { 977 connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options); 978 } catch (err) { 979 // 处理入参错误异常 980 let code = (err as BusinessError).code; 981 let message = (err as BusinessError).message; 982 console.error(`connectServiceExtensionAbility failed, code is ${code}, message is ${message}`); 983 } 984 } 985} 986``` 987 988## UIAbilityContext.startAbilityWithAccount 989 990startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void\>): void 991 992根据want和accountId启动Ability。使用callback异步回调。仅支持在主线程调用。 993 994> **说明:** 995> 996> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 997> 当accountId为当前用户时,无需进行权限校验。 998 999**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1000 1001**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1002 1003**系统接口**:此接口为系统接口。 1004 1005**参数:** 1006 1007| 参数名 | 类型 | 必填 | 说明 | 1008| -------- | -------- | -------- | -------- | 1009| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 1010| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | 1011| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 | 1012 1013**错误码:** 1014 1015以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1016 1017| 错误码ID | 错误信息 | 1018| ------- | -------------------------------- | 1019| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1020| 16000001 | The specified ability does not exist. | 1021| 16000002 | Incorrect ability type. | 1022| 16000004 | Failed to start the invisible ability. | 1023| 16000005 | The specified process does not have the permission. | 1024| 16000006 | Cross-user operations are not allowed. | 1025| 16000008 | The crowdtesting application expires. | 1026| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1027| 16000010 | The call with the continuation flag is forbidden. | 1028| 16000011 | The context does not exist. | 1029| 16000012 | The application is controlled. | 1030| 16000013 | The application is controlled by EDM. | 1031| 16000019 | No matching ability is found. | 1032| 16000050 | Internal error. | 1033| 16000053 | The ability is not on the top of the UI. | 1034| 16000055 | Installation-free timed out. | 1035| 16000071 | App clone is not supported. | 1036| 16000072 | App clone or multi-instance is not supported. | 1037| 16000073 | The app clone index is invalid. | 1038| 16000076 | The app instance key is invalid. | 1039| 16000077 | The number of app instances reaches the limit. | 1040| 16000078 | The multi-instance is not supported. | 1041| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 1042| 16000080 | Creating an instance is not supported. | 1043| 16000082 | The UIAbility is being started. | 1044| 16200001 | The caller has been released. | 1045 1046**示例:** 1047 1048```ts 1049import { UIAbility, Want } from '@kit.AbilityKit'; 1050import { BusinessError } from '@kit.BasicServicesKit'; 1051 1052export default class EntryAbility extends UIAbility { 1053 onForeground() { 1054 let want: Want = { 1055 deviceId: '', 1056 bundleName: 'com.example.myapplication', 1057 abilityName: 'EntryAbility' 1058 }; 1059 let accountId = 100; 1060 1061 try { 1062 this.context.startAbilityWithAccount(want, accountId, (err: BusinessError) => { 1063 if (err.code) { 1064 // 处理业务逻辑错误 1065 console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); 1066 return; 1067 } 1068 // 执行正常业务 1069 console.info('startAbilityWithAccount succeed'); 1070 }); 1071 } catch (err) { 1072 // 处理入参错误异常 1073 let code = (err as BusinessError).code; 1074 let message = (err as BusinessError).message; 1075 console.error(`startAbilityWithAccount failed, code is ${code}, message is ${message}`); 1076 } 1077 } 1078} 1079``` 1080 1081 1082## UIAbilityContext.startAbilityWithAccount 1083 1084startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void 1085 1086根据want、accountId及startOptions启动Ability。使用callback异步回调。仅支持在主线程调用。 1087 1088> **说明:** 1089> 1090> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1091> 当accountId为当前用户时,无需进行权限校验。 1092 1093**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1094 1095**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1096 1097**系统接口**:此接口为系统接口。 1098 1099**参数:** 1100 1101| 参数名 | 类型 | 必填 | 说明 | 1102| -------- | -------- | -------- | -------- | 1103| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 1104| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。| 1105| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 | 1106| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 | 1107 1108**错误码:** 1109 1110以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1111 1112| 错误码ID | 错误信息 | 1113| ------- | -------------------------------- | 1114| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1115| 16000001 | The specified ability does not exist. | 1116| 16000002 | Incorrect ability type. | 1117| 16000004 | Failed to start the invisible ability. | 1118| 16000005 | The specified process does not have the permission. | 1119| 16000006 | Cross-user operations are not allowed. | 1120| 16000008 | The crowdtesting application expires. | 1121| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1122| 16000010 | The call with the continuation flag is forbidden. | 1123| 16000011 | The context does not exist. | 1124| 16000012 | The application is controlled. | 1125| 16000013 | The application is controlled by EDM. | 1126| 16000019 | No matching ability is found. | 1127| 16000050 | Internal error. | 1128| 16000053 | The ability is not on the top of the UI. | 1129| 16000055 | Installation-free timed out. | 1130| 16000071 | App clone is not supported. | 1131| 16000072 | App clone or multi-instance is not supported. | 1132| 16000073 | The app clone index is invalid. | 1133| 16000076 | The app instance key is invalid. | 1134| 16000077 | The number of app instances reaches the limit. | 1135| 16000078 | The multi-instance is not supported. | 1136| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 1137| 16000080 | Creating an instance is not supported. | 1138| 16000082 | The UIAbility is being started. | 1139| 16200001 | The caller has been released. | 1140 1141**示例:** 1142 1143```ts 1144import { UIAbility, Want, StartOptions } from '@kit.AbilityKit'; 1145import { BusinessError } from '@kit.BasicServicesKit'; 1146 1147export default class EntryAbility extends UIAbility { 1148 onForeground() { 1149 let want: Want = { 1150 deviceId: '', 1151 bundleName: 'com.example.myapplication', 1152 abilityName: 'EntryAbility' 1153 }; 1154 let accountId = 100; 1155 let options: StartOptions = { 1156 displayId: 0 1157 }; 1158 1159 try { 1160 this.context.startAbilityWithAccount(want, accountId, options, (err: BusinessError) => { 1161 if (err.code) { 1162 // 处理业务逻辑错误 1163 console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); 1164 return; 1165 } 1166 // 执行正常业务 1167 console.info('startAbilityWithAccount succeed'); 1168 }); 1169 } catch (err) { 1170 // 处理入参错误异常 1171 let code = (err as BusinessError).code; 1172 let message = (err as BusinessError).message; 1173 console.error(`startAbilityWithAccount failed, code is ${code}, message is ${message}`); 1174 } 1175 } 1176} 1177``` 1178 1179 1180## UIAbilityContext.startAbilityWithAccount 1181 1182startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void\> 1183 1184根据want、accountId和startOptions启动Ability。使用Promise异步回调。仅支持在主线程调用。 1185 1186> **说明:** 1187> 1188> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1189> 当accountId为当前用户时,无需进行权限校验。 1190 1191**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1192 1193**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1194 1195**系统接口**:此接口为系统接口。 1196 1197**参数:** 1198 1199| 参数名 | 类型 | 必填 | 说明 | 1200| -------- | -------- | -------- | -------- | 1201| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 1202| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountcount9)。 | 1203| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 | 1204 1205**错误码:** 1206 1207以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1208 1209| 错误码ID | 错误信息 | 1210| ------- | -------------------------------- | 1211| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1212| 16000001 | The specified ability does not exist. | 1213| 16000002 | Incorrect ability type. | 1214| 16000004 | Failed to start the invisible ability. | 1215| 16000005 | The specified process does not have the permission. | 1216| 16000006 | Cross-user operations are not allowed. | 1217| 16000008 | The crowdtesting application expires. | 1218| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1219| 16000010 | The call with the continuation flag is forbidden. | 1220| 16000011 | The context does not exist. | 1221| 16000012 | The application is controlled. | 1222| 16000013 | The application is controlled by EDM. | 1223| 16000019 | No matching ability is found. | 1224| 16000050 | Internal error. | 1225| 16000053 | The ability is not on the top of the UI. | 1226| 16000055 | Installation-free timed out. | 1227| 16000071 | App clone is not supported. | 1228| 16000072 | App clone or multi-instance is not supported. | 1229| 16000073 | The app clone index is invalid. | 1230| 16000076 | The app instance key is invalid. | 1231| 16000077 | The number of app instances reaches the limit. | 1232| 16000078 | The multi-instance is not supported. | 1233| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 1234| 16000080 | Creating an instance is not supported. | 1235| 16000082 | The UIAbility is being started. | 1236| 16200001 | The caller has been released. | 1237 1238**示例:** 1239 1240```ts 1241import { UIAbility, Want, StartOptions } from '@kit.AbilityKit'; 1242import { BusinessError } from '@kit.BasicServicesKit'; 1243 1244export default class EntryAbility extends UIAbility { 1245 onForeground() { 1246 let want: Want = { 1247 deviceId: '', 1248 bundleName: 'com.example.myapplication', 1249 abilityName: 'EntryAbility' 1250 }; 1251 let accountId = 100; 1252 let options: StartOptions = { 1253 displayId: 0 1254 }; 1255 1256 try { 1257 this.context.startAbilityWithAccount(want, accountId, options) 1258 .then(() => { 1259 // 执行正常业务 1260 console.info('startAbilityWithAccount succeed'); 1261 }) 1262 .catch((err: BusinessError) => { 1263 // 处理业务逻辑错误 1264 console.error(`startAbilityWithAccount failed, code is ${err.code}, message is ${err.message}`); 1265 }); 1266 } catch (err) { 1267 // 处理入参错误异常 1268 let code = (err as BusinessError).code; 1269 let message = (err as BusinessError).message; 1270 console.error(`startAbilityWithAccount failed, code is ${code}, message is ${message}`); 1271 } 1272 } 1273} 1274``` 1275 1276## UIAbilityContext.setMissionIcon 1277 1278setMissionIcon(icon: image.PixelMap, callback: AsyncCallback\<void>): void 1279 1280设置当前ability在任务中显示的图标, 图标大小最大为600M(callback形式)。 1281 1282**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1283 1284**系统接口**:此接口为系统接口。 1285 1286**参数:** 1287 1288| 参数名 | 类型 | 必填 | 说明 | 1289| -------- | -------- | -------- | -------- | 1290| icon | image.PixelMap | 是 | 在最近的任务中显示的ability图标。 | 1291| callback | AsyncCallback\<void> | 是 | 指定的回调函数的结果。 | 1292 1293**错误码:** 1294 1295以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1296 1297| 错误码ID | 错误信息 | 1298| ------- | -------------------------------- | 1299| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1300| 16000011 | The context does not exist. | 1301| 16000050 | Internal error. | 1302 1303**示例:** 1304 1305```ts 1306import { UIAbility } from '@kit.AbilityKit'; 1307import { image } from '@kit.ImageKit'; 1308import { BusinessError } from '@kit.BasicServicesKit'; 1309 1310export default class EntryAbility extends UIAbility { 1311 onForeground() { 1312 let imagePixelMap: image.PixelMap; 1313 let color = new ArrayBuffer(0); 1314 image.createPixelMap(color, { 1315 size: { 1316 height: 100, 1317 width: 100 1318 } 1319 }).then((data) => { 1320 imagePixelMap = data; 1321 this.context.setMissionIcon(imagePixelMap, (err: BusinessError) => { 1322 console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`); 1323 }) 1324 }).catch((err: BusinessError) => { 1325 console.error(`createPixelMap failed, code is ${err.code}, message is ${err.message}`); 1326 }); 1327 } 1328} 1329``` 1330 1331 1332## UIAbilityContext.setMissionIcon 1333 1334setMissionIcon(icon: image.PixelMap): Promise\<void> 1335 1336设置当前ability在任务中显示的图标, 图标大小最大为600M(promise形式)。 1337 1338**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1339 1340**系统接口**:此接口为系统接口。 1341 1342**参数:** 1343 1344| 参数名 | 类型 | 必填 | 说明 | 1345| -------- | -------- | -------- | -------- | 1346| icon | image.PixelMap | 是 | 在最近的任务中显示的ability图标。 | 1347 1348**返回值:** 1349 1350| 类型 | 说明 | 1351| -------- | -------- | 1352| Promise<void> | 返回一个Promise,包含接口的结果。 | 1353 1354**错误码:** 1355 1356以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1357 1358| 错误码ID | 错误信息 | 1359| ------- | -------------------------------- | 1360| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1361| 16000011 | The context does not exist. | 1362| 16000050 | Internal error. | 1363 1364**示例:** 1365 1366```ts 1367import { UIAbility } from '@kit.AbilityKit'; 1368import { image } from '@kit.ImageKit'; 1369import { BusinessError } from '@kit.BasicServicesKit'; 1370 1371export default class EntryAbility extends UIAbility { 1372 onForeground() { 1373 let imagePixelMap: image.PixelMap; 1374 let color = new ArrayBuffer(0); 1375 image.createPixelMap(color, { 1376 size: { 1377 height: 100, 1378 width: 100 1379 } 1380 }).then((data) => { 1381 imagePixelMap = data; 1382 this.context.setMissionIcon(imagePixelMap) 1383 .then(() => { 1384 console.info('setMissionIcon succeed'); 1385 }) 1386 .catch((err: BusinessError) => { 1387 console.error(`setMissionLabel failed, code is ${err.code}, message is ${err.message}`); 1388 }); 1389 }).catch((err: BusinessError) => { 1390 console.error(`createPixelMap failed, code is ${err.code}, message is ${err.message}`); 1391 }); 1392 } 1393} 1394``` 1395 1396## UIAbilityContext.startRecentAbility 1397 1398startRecentAbility(want: Want, callback: AsyncCallback<void>): void 1399 1400启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。使用callback异步回调。仅支持在主线程调用。 1401 1402> **说明:** 1403> 1404> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1405 1406**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1407 1408**系统接口**:此接口为系统接口。 1409 1410**参数:** 1411 1412| 参数名 | 类型 | 必填 | 说明 | 1413| -------- | -------- | -------- | -------- | 1414| want | [Want](js-apis-app-ability-want.md) | 是 | 需要启动Ability的want信息。 | 1415| callback | AsyncCallback\<void> | 是 | 指定的回调函数的结果。 | 1416 1417**错误码:** 1418 1419以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1420 1421| 错误码ID | 错误信息 | 1422| ------- | -------------------------------- | 1423| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1424| 16000001 | The specified ability does not exist. | 1425| 16000002 | Incorrect ability type. | 1426| 16000004 | Failed to start the invisible ability. | 1427| 16000005 | The specified process does not have the permission. | 1428| 16000006 | Cross-user operations are not allowed. | 1429| 16000008 | The crowdtesting application expires. | 1430| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1431| 16000010 | The call with the continuation flag is forbidden. | 1432| 16000011 | The context does not exist. | 1433| 16000012 | The application is controlled. | 1434| 16000013 | The application is controlled by EDM. | 1435| 16000050 | Internal error. | 1436| 16000053 | The ability is not on the top of the UI. | 1437| 16000055 | Installation-free timed out. | 1438| 16000082 | The UIAbility is being started. | 1439| 16200001 | The caller has been released. | 1440| 16000073 | The app clone index is invalid. | 1441 1442**示例:** 1443 1444```ts 1445import { UIAbility, Want } from '@kit.AbilityKit'; 1446import { BusinessError } from '@kit.BasicServicesKit'; 1447 1448export default class EntryAbility extends UIAbility { 1449 onForeground() { 1450 let want: Want = { 1451 bundleName: 'com.example.myapplication', 1452 abilityName: 'EntryAbility' 1453 }; 1454 1455 try { 1456 this.context.startRecentAbility(want, (err: BusinessError) => { 1457 if (err.code) { 1458 // 处理业务逻辑错误 1459 console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); 1460 return; 1461 } 1462 // 执行正常业务 1463 console.info('startRecentAbility succeed'); 1464 }); 1465 } catch (err) { 1466 // 处理入参错误异常 1467 let code = (err as BusinessError).code; 1468 let message = (err as BusinessError).message; 1469 console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); 1470 } 1471 } 1472} 1473``` 1474## UIAbilityContext.startRecentAbility 1475 1476startRecentAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void 1477 1478启动一个指定的Ability。如果这个Ability有多个实例,将拉起最近启动的那个实例。当开发者需要携带启动参数时可以选择此API。使用callback异步回调。仅支持在主线程调用。 1479 1480 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**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1829 1830**系统接口**:此接口为系统接口。 1831 1832**参数:** 1833 1834| 参数名 | 类型 | 必填 | 说明 | 1835| -------- | -------- | -------- | -------- | 1836| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 1837| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 | 1838| callback | AsyncCallback<void> | 是 | 回调函数。当启动Ability成功,err为undefined,否则为错误对象。 | 1839 1840**错误码:** 1841 1842以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1843 1844| 错误码ID | 错误信息 | 1845| ------- | -------------------------------- | 1846| 401 | 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.| 1847| 16000001 | The specified ability does not exist. | 1848| 16000004 | Failed to start the invisible ability. | 1849| 16000005 | The specified process does not have the permission. | 1850| 16000006 | Cross-user operations are not allowed. | 1851| 16000008 | The crowdtesting application expires. | 1852| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1853| 16000011 | The context does not exist. | 1854| 16000012 | The application is controlled. | 1855| 16000013 | The application is controlled by EDM. | 1856| 16000050 | Internal error. | 1857| 16000053 | The ability is not on the top of the UI. | 1858| 16000055 | Installation-free timed out. | 1859| 16000071 | App clone is not supported. | 1860| 16000072 | App clone or multi-instance is not supported. | 1861| 16000073 | The app clone index is invalid. | 1862| 16000076 | The app instance key is invalid. | 1863| 16000077 | The number of app instances reaches the limit. | 1864| 16000078 | The multi-instance is not supported. | 1865| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 1866| 16000080 | Creating an instance is not supported. | 1867| 16000082 | The UIAbility is being started. | 1868| 16200001 | The caller has been released. | 1869 1870**示例:** 1871 1872```ts 1873import { UIAbility, Want, AbilityConstant, StartOptions } from '@kit.AbilityKit'; 1874 1875export default class EntryAbility extends UIAbility { 1876 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 1877 // want包含启动该应用的Caller信息 1878 let localWant: Want = want; 1879 localWant.bundleName = 'com.example.demo'; 1880 localWant.moduleName = 'entry'; 1881 localWant.abilityName = 'TestAbility'; 1882 let option: StartOptions = { 1883 displayId: 0 1884 }; 1885 1886 // 使用启动方的Caller身份信息启动新Ability 1887 this.context.startAbilityAsCaller(localWant, option, (err) => { 1888 if (err && err.code != 0) { 1889 console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err)); 1890 } else { 1891 console.log('startAbilityAsCaller success.'); 1892 } 1893 }) 1894 } 1895} 1896``` 1897 1898## UIAbilityContext.startAbilityAsCaller<sup>10+<sup> 1899 1900startAbilityAsCaller(want: Want, options?: StartOptions): Promise\<void> 1901 1902使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用Promise异步回调。仅支持在主线程调用。 1903 1904> **说明:** 1905> 1906> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1907 1908**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1909 1910**系统接口**:此接口为系统接口。 1911 1912**参数:** 1913 1914| 参数名 | 类型 | 必填 | 说明 | 1915| -------- | -------- | -------- | -------- | 1916| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 1917| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 | 1918 1919**返回值:** 1920 1921| 类型 | 说明 | 1922| -------- | -------- | 1923| Promise<void> | Promise对象。无返回结果的Promise对象。 | 1924 1925**错误码:** 1926 1927以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1928 1929| 错误码ID | 错误信息 | 1930| ------- | -------------------------------- | 1931| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1932| 16000001 | The specified ability does not exist. | 1933| 16000002 | Incorrect ability type. | 1934| 16000004 | Failed to start the invisible ability. | 1935| 16000005 | The specified process does not have the permission. | 1936| 16000006 | Cross-user operations are not allowed. | 1937| 16000008 | The crowdtesting application expires. | 1938| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1939| 16000010 | The call with the continuation flag is forbidden. | 1940| 16000011 | The context does not exist. | 1941| 16000012 | The application is controlled. | 1942| 16000013 | The application is controlled by EDM. | 1943| 16000050 | Internal error. | 1944| 16000053 | The ability is not on the top of the UI. | 1945| 16000055 | Installation-free timed out. | 1946| 16000071 | App clone is not supported. | 1947| 16000072 | App clone or multi-instance is not supported. | 1948| 16000073 | The app clone index is invalid. | 1949| 16000076 | The app instance key is invalid. | 1950| 16000077 | The number of app instances reaches the limit. | 1951| 16000078 | The multi-instance is not supported. | 1952| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 1953| 16000080 | Creating an instance is not supported. | 1954| 16000082 | The UIAbility is being started. | 1955| 16200001 | The caller has been released. | 1956 1957**示例:** 1958 1959```ts 1960import { UIAbility, Want, AbilityConstant, StartOptions } from '@kit.AbilityKit'; 1961import { BusinessError } from '@kit.BasicServicesKit'; 1962 1963export default class EntryAbility extends UIAbility { 1964 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 1965 // want包含启动该应用的Caller信息 1966 let localWant: Want = want; 1967 localWant.bundleName = 'com.example.demo'; 1968 localWant.moduleName = 'entry'; 1969 localWant.abilityName = 'TestAbility'; 1970 let option: StartOptions = { 1971 displayId: 0 1972 }; 1973 1974 // 使用启动方的Caller身份信息启动新Ability 1975 this.context.startAbilityAsCaller(localWant, option) 1976 .then(() => { 1977 console.log('startAbilityAsCaller success.'); 1978 }) 1979 .catch((err: BusinessError) => { 1980 console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err)); 1981 }) 1982 } 1983} 1984``` 1985 1986## UIAbilityContext.requestModalUIExtension<sup>11+<sup> 1987 1988requestModalUIExtension(pickerWant: Want): Promise\<void> 1989 1990请求在指定的前台应用上拉起对应类型的UIExtensionAbility。使用Promise异步回调。仅支持在主线程调用。 1991 1992其中,前台应用通过want.parameters中bundleName来指定,如果未指定前台应用、bundleName指定的应用未在前台或指定的前台应用的bundleName不正确,则在系统界面上直接拉起UIExtensionAbility;被拉起的UIExtensionAbility通过want中bundleName、abilityName、moduleName字段共同确定,同时需要通过want.parameters中的ability.want.params.uiExtensionType字段配置UIExtensionAbility的类型。 1993 1994在前台应用上拉起UIExtensionAility之前,必须确保该应用已完成页面初始化,否则将导致拉起失败、并出现"uiContent is nullptr"的报错信息。应用可通过监听页面加载状态来判断拉起UIExtensionAbility的时机,页面初始化成功后会出现关键日志信息"UIContentImpl: focus again"。 1995 1996> **说明:** 1997> 1998> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1999 2000**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2001 2002**系统接口**:此接口为系统接口。 2003 2004**参数:** 2005 2006| 参数名 | 类型 | 必填 | 说明 | 2007| -------- | -------- | -------- | -------- | 2008| pickerWant | [Want](js-apis-app-ability-want.md) | 是 | 拉起UIExtension的want信息。 | 2009 2010**返回值:** 2011 2012| 类型 | 说明 | 2013| -------- | -------- | 2014| Promise<void> | Promise对象。无返回结果的Promise对象。 | 2015 2016**错误码:** 2017 2018以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2019 2020| 错误码ID | 错误信息 | 2021| ------- | -------------------------------- | 2022| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2023| 16000050 | Internal error. | 2024 2025**示例:** 2026 2027```ts 2028import { UIAbility, Want } from '@kit.AbilityKit'; 2029import { BusinessError } from '@kit.BasicServicesKit'; 2030 2031export default class EntryAbility extends UIAbility { 2032 onForeground() { 2033 let want: Want = { 2034 bundleName: 'com.example.myapplication', 2035 abilityName: 'com.example.myapplication.UIExtAbility', 2036 moduleName: 'entry_test', 2037 parameters: { 2038 'bundleName': 'com.example.myapplication', 2039 //与com.example.myapplication.UIExtAbility配置的type相同 2040 'ability.want.params.uiExtensionType': 'sys/commonUI' 2041 } 2042 }; 2043 2044 try { 2045 this.context.requestModalUIExtension(want) 2046 .then(() => { 2047 // 执行正常业务 2048 console.info('requestModalUIExtension succeed'); 2049 }) 2050 .catch((err: BusinessError) => { 2051 // 处理业务逻辑错误 2052 console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`); 2053 }); 2054 } catch (err) { 2055 // 处理入参错误异常 2056 let code = (err as BusinessError).code; 2057 let message = (err as BusinessError).message; 2058 console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`); 2059 } 2060 } 2061} 2062``` 2063 2064## UIAbilityContext.requestModalUIExtension<sup>11+<sup> 2065requestModalUIExtension(pickerWant: Want, callback: AsyncCallback\<void>): void 2066 2067请求在指定的前台应用上拉起对应类型的UIExtensionAbility。使用callback异步回调。仅支持在主线程调用。 2068 2069其中,前台应用通过want.parameters中bundleName来指定,如果未指定前台应用、bundleName指定的应用未在前台或指定的前台应用的bundleName不正确,则在系统界面上直接拉起UIExtensionAbility;被拉起的UIExtensionAbility通过want中bundleName、abilityName、moduleName字段共同确定,同时需要通过want.parameters中的ability.want.params.uiExtensionType字段配置UIExtensionAbility的类型。 2070 2071在前台应用上拉起UIExtensionAility之前,必须确保该应用已完成页面初始化,否则将导致拉起失败、并出现"uiContent is nullptr"的报错信息。应用可通过监听页面加载状态来判断拉起UIExtensionAbility的时机,页面初始化成功后会出现关键日志信息"UIContentImpl: focus again"。 2072 2073> **说明:** 2074> 2075> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 2076**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2077 2078**系统接口**:此接口为系统接口。 2079 2080**参数:** 2081 2082| 参数名 | 类型 | 必填 | 说明 | 2083| -------- | -------- | -------- | -------- | 2084| pickerWant | [Want](js-apis-app-ability-want.md) | 是 | 拉起UIExtension的want信息。 | 2085| callback | AsyncCallback<void> | 是 | 回调函数。当拉起UIExtension成功,err为undefined,否则为错误对象。 | 2086 2087**错误码:** 2088 2089以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2090 2091| 错误码ID | 错误信息 | 2092| ------- | -------------------------------- | 2093| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2094| 16000050 | Internal error. | 2095 2096**示例:** 2097 2098```ts 2099import { UIAbility, Want } from '@kit.AbilityKit'; 2100import { BusinessError } from '@kit.BasicServicesKit'; 2101 2102export default class EntryAbility extends UIAbility { 2103 onForeground() { 2104 let want: Want = { 2105 bundleName: 'com.example.myapplication', 2106 abilityName: 'UIExtAbility', 2107 moduleName: 'entry_test', 2108 parameters: { 2109 'bundleName': 'com.example.myapplication', 2110 //与com.example.myapplication.UIExtAbility配置的type相同 2111 'ability.want.params.uiExtensionType': 'sys/commonUI' 2112 } 2113 }; 2114 2115 try { 2116 this.context.requestModalUIExtension(want, (err: BusinessError) => { 2117 if (err.code) { 2118 // 处理业务逻辑错误 2119 console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`); 2120 return; 2121 } 2122 // 执行正常业务 2123 console.info('requestModalUIExtension succeed'); 2124 }); 2125 } catch (err) { 2126 // 处理入参错误异常 2127 let code = (err as BusinessError).code; 2128 let message = (err as BusinessError).message; 2129 console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`); 2130 } 2131 } 2132} 2133``` 2134