1# @ohos.application.BackupExtensionAbility (备份恢复扩展能力)(系统接口) 2<!--Kit: Core File Kit--> 3<!--Subsystem: FileManagement--> 4<!--Owner: @lvzhenjie--> 5<!--Designer: @wang_zhangjun; @chenxi0605--> 6<!--Tester: @liuhonggang123--> 7<!--Adviser: @foryourself--> 8 9BackupExtensionAbility模块提供备份恢复服务相关扩展能力,为应用提供扩展的备份恢复能力。 10 11> **说明:** 12> 13> - 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14> - 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.application.BackupExtensionAbility (备份恢复扩展能力)](js-apis-application-backupExtensionAbility-sys.md)。 15> - 本模块接口仅可在Stage模型下使用。 16 17## 导入模块 18 19```ts 20import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; 21``` 22 23### getBackupInfo<sup>12+</sup> 24 25getBackupInfo(): string 26 27在调用方查询应用数据时执行,由开发者提供扩展查询应用数据的操作。 28 29**系统接口**:此接口为系统接口 30 31**系统能力**:SystemCapability.FileManagement.StorageService.Backup 32 33**返回值:** 34 35| 类型 | 说明 | 36| --------------------- | :---- | 37| string | 应用自定义的备份信息。| 38 39**示例:** 40 41 ```ts 42 class BackupExt extends BackupExtensionAbility { 43 getBackupInfo(): string { 44 console.info('getBackupInfo ok'); 45 let info = "app diy info"; 46 return info; 47 } 48 } 49 ``` 50 51### getBackupCompatibilityInfo<sup>20+</sup> 52 53getBackupCompatibilityInfo(extInfo: string): Promise<string> 54 55在应用备份阶段,调用方获取应用自定义的能力描述信息时执行,由应用实现返回。使用Promise进行异步回调。 56 57**系统接口**:此接口为系统接口 58 59**系统能力**:SystemCapability.FileManagement.StorageService.Backup 60 61**参数:** 62 63| 参数名 | 类型 | 必填 | 说明 | 64| ------------- | ------------------------------- | ---- | ------------------------------ | 65| extInfo | string | 是 | 传递给应用的额外信息,由应用自行处理。| 66 67**返回值:** 68 69| 类型 | 说明 | 70| --------------------- | :---- | 71| Promise<string> | Promise对象,备份过程中应用自定义的能力描述信息。| 72 73**示例:** 74 75 ```ts 76 class BackupExt extends BackupExtensionAbility { 77 async getBackupCompatibilityInfo(extInfo: string): Promise<string> { 78 let ret: string = ''; 79 try { 80 // 此处仅以Json为示范,相应判断逻辑及相应字段由应用自定义 81 if (!extInfo) { 82 ret = '{"dbVersion": "1.0", "isThemCardEnable": "true"}'; 83 } else { 84 let extJson: Record<string, string> = JSON.parse(extInfo); 85 if (extJson?.requireCompatibility) { 86 ret = '{"isSupportBackup": "true"}'; 87 } else { 88 ret = '{"isSupportBackup": "false"}'; 89 } 90 } 91 } catch (error) { 92 console.error(`getBackupCompatibilityInfo failed with error. Code: ${error.code}, message: ${error.message}`); 93 } 94 return JSON.stringify(ret); 95 } 96 } 97 ``` 98 99### getRestoreCompatibilityInfo<sup>20+</sup> 100 101getRestoreCompatibilityInfo(extInfo: string): Promise<string> 102 103在应用恢复阶段,调用方获取应用自定义的能力描述信息时执行,由应用实现返回。使用Promise进行异步回调。 104 105**系统接口**:此接口为系统接口 106 107**系统能力**:SystemCapability.FileManagement.StorageService.Backup 108 109**参数:** 110 111| 参数名 | 类型 | 必填 | 说明 | 112| ------------- | ------------------------------- | ---- | ------------------------------ | 113| extInfo | string | 是 | 传递给应用的额外信息,由应用自行处理。| 114 115**返回值:** 116 117| 类型 | 说明 | 118| --------------------- | :---- | 119| Promise<string> | Promise对象,恢复过程中应用自定义的能力描述信息。| 120 121**示例:** 122 123 ```ts 124 class BackupExt extends BackupExtensionAbility { 125 async getRestoreCompatibilityInfo(extInfo: string): Promise<string> { 126 let ret: string = ''; 127 try { 128 // 此处仅以Json为示范,相应判断逻辑及相应字段由应用自定义 129 if (!extInfo) { 130 ret = '{"dbVersion": "1.0", "isThemCardEnable": "true"}'; 131 } else { 132 let extJson: Record<string, string> = JSON.parse(extInfo); 133 if (extJson?.requireCompatibility) { 134 ret = '{"isSupportRestore": "true"}'; 135 } else { 136 ret = '{"isSupportRestore": "false"}'; 137 } 138 } 139 } catch (error) { 140 console.error(`getRestoreCompatibilityInfo failed with error. Code: ${error.code}, message: ${error.message}`); 141 } 142 return JSON.stringify(ret); 143 } 144 } 145 ```