1# @ohos.app.ability.CompletionHandlerForAtomicService (打开原子化服务结果的操作类) 2<!--Kit: Ability Kit--> 3<!--Subsystem: Ability--> 4<!--Owner: @littlejerry1; @wendel; @Luobniz21--> 5<!--Designer: @ccllee1--> 6<!--Tester: @lixueqing513--> 7<!--Adviser: @huipeizi--> 8 9CompletionHandlerForAtomicService作为[AtomicServiceOptions](js-apis-app-ability-atomicServiceOptions.md)的可选参数,用于接收打开原子化服务请求的结果。 10 11 12> **说明:** 13> 14> - 本模块首批接口从API version 20 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 15> 16> - 本模块接口仅可在Stage模型下使用。 17 18## 导入模块 19 20```ts 21import { CompletionHandlerForAtomicService } from '@kit.AbilityKit'; 22``` 23 24## FailureCode 25 26打开原子化服务失败的特定错误码。 27 28**原子化服务API**:从API version 20开始,该接口支持在原子化服务中使用。 29 30**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 31 32| 名称 | 值 | 说明 | 33| ---------------------------------------- | ---- | ---------------------------------------- | 34| FAILURE_CODE_SYSTEM_MALFUNCTION | 0 | 表示由于系统错误(如跳转弹框崩溃)而无法打开原子化服务。 | 35| FAILURE_CODE_USER_CANCEL | 1 | 用户取消。 | 36| FAILURE_CODE_USER_REFUSE | 2 | 用户拒绝。 | 37 38## CompletionHandlerForAtomicService 39 40CompletionHandlerForAtomicService提供了[onAtomicServiceRequestSuccess](#onatomicservicerequestsuccess)和[onAtomicServiceRequestFailure](#onatomicservicerequestfailure)两个回调函数,分别在打开原子化服务成功和失败时回调。 41 42### onAtomicServiceRequestSuccess 43 44onAtomicServiceRequestSuccess(appId: string): void 45 46打开原子化服务成功时的回调函数。 47 48**原子化服务API**:从API version 20开始,该接口支持在原子化服务中使用。 49 50**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 51 52**参数:** 53 54| 参数名 | 类型 | 必填 | 说明 | 55| -------- | -------- | -------- | -------- | 56| appId | string | 是 | 被拉起原子化服务的appId。 | 57 58**示例:** 59 60参见[CompletionHandlerForAtomicService示例](#completionhandlerforatomicservice示例)。 61 62### onAtomicServiceRequestFailure 63 64onAtomicServiceRequestFailure(appId: string, failureCode: FailureCode, failureMessage: string): void 65 66打开原子化服务失败时的回调函数。 67 68**原子化服务API**:从API version 20开始,该接口支持在原子化服务中使用。 69 70**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 71 72**参数:** 73 74| 参数名 | 类型 | 必填 | 说明 | 75| -------- | -------- | -------- | -------- | 76| appId | string | 是 | 被拉起原子化服务的appId。 | 77| failureCode | [FailureCode](#failurecode) | 是 | 失败原因的错误码。 | 78| failureMessage | string | 是 | 失败原因的描述。 | 79 80**示例:** 81 82参见[CompletionHandlerForAtomicService示例](#completionhandlerforatomicservice示例)。 83 84### CompletionHandlerForAtomicService示例 85 86```ts 87import { AbilityConstant, AtomicServiceOptions, common, UIAbility, Want, CompletionHandlerForAtomicService } from '@kit.AbilityKit'; 88import { BusinessError } from '@kit.BasicServicesKit'; 89import { hilog } from '@kit.PerformanceAnalysisKit'; 90import { FailureCode } from '@ohos.app.ability.CompletionHandlerForAtomicService'; 91 92export default class EntryAbility extends UIAbility { 93 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 94 let completionHandler: CompletionHandlerForAtomicService = { 95 onAtomicServiceRequestSuccess(appId: string) { 96 hilog.info(0x0000, 'testTag', `appId:${appId}`); 97 }, 98 onAtomicServiceRequestFailure(appId: string, failureCode: FailureCode, failureMessage: string) { 99 hilog.info(0x0000, 'testTag', `appId:${appId}, failureCode:${failureCode}, failureMessage:${failureMessage}`); 100 } 101 }; 102 let options: AtomicServiceOptions = { 103 completionHandlerForAtomicService: completionHandler 104 }; 105 let appId: string = '5765880207853275489'; // 根据实际appId修改此值 106 this.context.openAtomicService(appId, options).then((result: common.AbilityResult) => { 107 hilog.info(0x0000, 'testTag', `openAtomicService succeed:${JSON.stringify(result)}`); 108 }).catch((err: BusinessError) => { 109 hilog.error(0x0000, 'testTag', `openAtomicService failed:${JSON.stringify(err)}`); 110 }); 111 } 112} 113 114```