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