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