鸿蒙API14开发【@ohos.file.cloudSyncManager (端云同步管理能力)(系统接口)】文件基础服务

该模块向云空间应用提供端云同步管理能力:包括使能/去使能端云协同能力、修改应用同步开关,云端数据变化通知以及账号退出清理/保留云相关文件等。

说明:

  • 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
  • 本模块为系统接口。

导入模块

import { cloudSyncManager } from '@kit.CoreFileKit';
ts

cloudSyncManager.changeAppCloudSwitch

changeAppCloudSwitch(accountId: string, bundleName: string, status: boolean): Promise

异步方法修改应用的端云文件同步开关,以Promise形式返回结果。

系统能力:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

参数:

参数名类型必填说明
accountIdstring账号Id。
bundleNamestring应用包名。
statusboolean修改的应用云同步开关状态。true为打开;false为关闭。

返回值:

类型说明
Promise使用Promise形式返回修改应用的端云文件同步开关的结果。
错误码ID错误信息
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

示例:

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
let bundleName: string = "com.example.bundle";
cloudSyncManager.changeAppCloudSwitch(accountId, bundleName, true).then(() => {
  console.info("changeAppCloudSwitch successfully");
}).catch((err: BusinessError) => {
  console.error("changeAppCloudSwitch failed with error message: " + err.message + ", error code: " + err.code);
});
ts

cloudSyncManager.changeAppCloudSwitch

changeAppCloudSwitch(accountId: string, bundleName: string, status: boolean, callback: AsyncCallback): void

异步方法修改应用的端云文件同步开关,以callback形式返回结果。

系统能力:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

参数:

参数名类型必填说明
accountIdstring账号Id。
bundleNamestring应用包名
statusboolean修改的应用云同步开关状态。true为打开;false为关闭。
callbackAsyncCallback异步修改应用的端云文件同步开关之后的回调。
错误码ID错误信息
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

示例:

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
let bundleName: string = "com.example.bundle";
cloudSyncManager.changeAppCloudSwitch(accountId, bundleName, true, (err: BusinessError) => {
  if (err) {
    console.error("changeAppCloudSwitch failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("changeAppCloudSwitch successfully");
  }
});
ts

cloudSyncManager.notifyDataChange

notifyDataChange(accountId: string, bundleName: string): Promise

异步方法通知端云服务应用的云数据变更,以Promise形式返回结果。

系统能力:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

参数:

参数名类型必填说明
accountIdstring账号Id。
bundleNamestring应用包名。

返回值:

类型说明
Promise使用Promise形式返回通知端云服务应用的云数据变更的结果。
错误码ID错误信息
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

示例:

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
let bundleName: string = "com.example.bundle";
cloudSyncManager.notifyDataChange(accountId, bundleName).then(() => {
  console.info("notifyDataChange successfully");
}).catch((err: BusinessError) => {
  console.error("notifyDataChange failed with error message: " + err.message + ", error code: " + err.code);
});
ts

cloudSyncManager.notifyDataChange

notifyDataChange(accountId: string, bundleName: string, callback: AsyncCallback): void

异步方法通知端云服务应用的云数据变更,以callback形式返回结果。

系统能力:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

参数:

参数名类型必填说明
accountIdstring账号Id。
bundleNamestring应用包名。
callbackAsyncCallback异步通知端云服务应用的云数据变更之后的回调。
错误码ID错误信息
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

示例:

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
let bundleName: string = "com.example.bundle";
cloudSyncManager.notifyDataChange(accountId, bundleName, (err: BusinessError) => {
  if (err) {
    console.error("notifyDataChange failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("notifyDataChange successfully");
  }
});
ts

ExtraData11+

云端数据变更信息。

系统能力:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

名称类型必填说明
eventIdstring变更事件id。
extraDataExtraData云端数据变更信息。

cloudSyncManager.notifyDataChange11+

notifyDataChange(userId: number, extraData: ExtraData): Promise

异步方法通知端云服务应用的云数据变更,以Promise形式返回结果。

需要权限:ohos.permission.CLOUDFILE_SYNC_MANAGER

系统能力:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

参数:

参数名类型必填说明
userIdnumber用户Id。
extraDataExtraData云端数据变更信息。

返回值:

类型说明
Promise使用Promise形式返回通知端云服务应用的云数据变更的结果。
错误码ID错误信息
201Permission verification failed, usually the result returned by VerifyAccessToken.
202Permission verification failed, application which is not a system application uses system API.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.

示例:

import { BusinessError } from '@kit.BasicServicesKit';
let userId: number = 100;
let extraData: cloudSyncManager.ExtraData = {eventId: "eventId", extraData: "data"};
cloudSyncManager.notifyDataChange(userId, extraData).then(() => {
  console.info("notifyDataChange successfully");
}).catch((err: BusinessError) => {
  console.error("notifyDataChange failed with error message: " + err.message + ", error code: " + err.code);
});
ts

cloudSyncManager.notifyDataChange11+

notifyDataChange(userId: number, extraData: ExtraData, callback: AsyncCallback): void

异步方法通知端云服务应用的云数据变更,以callback形式返回结果。

需要权限:ohos.permission.CLOUDFILE_SYNC_MANAGER

系统能力:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

参数:

参数名类型必填说明
userIdnumber用户Id。
extraDataExtraData云端数据变更信息。
callbackAsyncCallback异步通知端云服务应用的云数据变更之后的回调。
错误码ID错误信息
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid.
13600001IPC error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

示例:

import { BusinessError } from '@kit.BasicServicesKit';
let userId: number = 100;
let extraData: cloudSyncManager.ExtraData = {eventId: "eventId", extraData: "data"};
cloudSyncManager.notifyDataChange(userId, extraData, (err: BusinessError) => {
  if (err) {
    console.error("notifyDataChange failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("notifyDataChange successfully");
  }
});
ts

cloudSyncManager.enableCloud

enableCloud(accountId: string, switches: Record<string, boolean>): Promise

异步方法使能端云协同能力,以Promise形式返回结果。

需要权限:ohos.permission.CLOUDFILE_SYNC_MANAGER

系统能力:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

参数:

参数名类型必填说明
accountIdstring账号Id。
switchesobject应用的端云协同特性使能开关,bundleName为string类型应用包名,开关状态是个boolean类型。

返回值:

类型说明
Promise使用Promise形式返回使能端云协同能力的结果。
错误码ID错误信息
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

示例:

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
let switches: Record<string, boolean> = {
  'com.example.bundleName1': true,
  'com.example.bundleName2': false
}
cloudSyncManager.enableCloud(accountId, switches).then(() => {
  console.error("enableCloud successfully");
}).catch((err: BusinessError) => {
  console.info("enableCloud failed with error message: " + err.message + ", error code: " + err.code);
});
ts

cloudSyncManager.enableCloud

enableCloud(accountId: string, switches: Record<string, boolean>, callback: AsyncCallback): void

异步方法使能端云协同能力,以callback形式返回结果。

需要权限:ohos.permission.CLOUDFILE_SYNC_MANAGER

系统能力:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

参数:

参数名类型必填说明
accountIdstring账号Id。
switchesobject应用的端云协同特性使能开关,bundleName为string类型应用包名,开关状态是个boolean类型。
callbackAsyncCallback异步使能端云协同能力之后的回调。
错误码ID错误信息
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

示例:

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
let switches: Record<string, boolean> = {
  'com.example.bundleName1': true,
  'com.example.bundleName2': false
}
cloudSyncManager.enableCloud(accountId, switches, (err: BusinessError) => {
  if (err) {
    console.error("enableCloud failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("enableCloud successfully");
  }
});
ts

cloudSyncManager.disableCloud

disableCloud(accountId: string): Promise

异步方法去使能端云协同能力,以Promise形式返回结果。

需要权限:ohos.permission.CLOUDFILE_SYNC_MANAGER

系统能力:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

参数:

参数名类型必填说明
accountIdstring账号Id。

返回值:

类型说明
Promise使用Promise形式返回去使能端云协同能力的结果。
错误码ID错误信息
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

示例:

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
cloudSyncManager.disableCloud(accountId).then(() => {
  console.info("disableCloud successfully");
}).catch((err: BusinessError) => {
  console.error("disableCloud failed with error message: " + err.message + ", error code: " + err.code);
});
ts

cloudSyncManager.disableCloud

disableCloud(accountId: string, callback: AsyncCallback): void

异步方法去使能端云协同能力,以callback形式返回结果。

需要权限:ohos.permission.CLOUDFILE_SYNC_MANAGER

系统能力:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

参数:

参数名类型必填说明
accountIdstring账号Id。
callbackAsyncCallback异步去使能端云协同能力之后的回调。
错误码ID错误信息
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

示例:

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
cloudSyncManager.disableCloud(accountId, (err: BusinessError) => {
  if (err) {
    console.error("disableCloud failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("disableCloud successfully");
  }
});
ts

Action

清理本地云相关数据时的Action,为枚举类型。

需要权限:ohos.permission.CLOUDFILE_SYNC_MANAGER

系统能力: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

名称说明
RETAIN_DATA0仅清除云端标识,保留本地缓存文件。
CLEAR_DATA1清除云端标识信息,若存在本地缓存文件,一并删除。

cloudSyncManager.clean

clean(accountId: string, appActions: Record<string, Action>): Promise

异步方法清理本地云相关数据,以Promise形式返回结果。

需要权限:ohos.permission.CLOUDFILE_SYNC_MANAGER

系统能力:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

参数:

参数名类型必填说明
accountIdstring账号Id。
appActionsobject清理动作类型,bundleName为string类型待清理应用包名, [Action]为清理动作类型。

返回值:

类型说明
Promise使用Promise形式返回清理本地云相关数据的结果。
错误码ID错误信息
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

示例:

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
let appActions: Record<string, cloudSyncManager.Action> = {
  'com.example.bundleName1': cloudSyncManager.Action.RETAIN_DATA,
  'com.example.bundleName2': cloudSyncManager.Action.CLEAR_DATA
};
cloudSyncManager.clean(accountId, appActions).then(() => {
  console.info("clean successfully");
}).catch((err: BusinessError) => {
  console.error("clean failed with error message: " + err.message + ", error code: " + err.code);
});
ts

cloudSyncManager.clean

clean(accountId: string, appActions: Record<string, Action>, callback: AsyncCallback): void

异步方法清理本地云相关数据,以callback形式返回结果。

需要权限:ohos.permission.CLOUDFILE_SYNC_MANAGER

系统能力:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

参数:

参数名类型必填说明
accountIdstring账号Id。
appActionsobject清理动作类型,bundleName为string类型待清理应用包名, [Action]为清理动作类型。
callbackAsyncCallback异步方法清理本地云相关数据。
错误码ID错误信息
201Permission verification failed, usually the result returned by VerifyAccessToken.
202Permission verification failed, application which is not a system application uses system API.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.

示例:

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
  let appActions: Record<string, cloudSyncManager.Action> = {
  'com.example.bundleName1': cloudSyncManager.Action.RETAIN_DATA,
  'com.example.bundleName2': cloudSyncManager.Action.CLEAR_DATA
};
cloudSyncManager.clean(accountId, appActions, (err: BusinessError) => {
  if (err) {
    console.error("clean failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("clean successfully");
  }
});

加入鸿蒙学习阵营!

除了本文,面费领《HarmonyOS应用开发》+《鸿蒙openharmony系统开发大全》 等入门进阶资料!关注我的CSDN主页,持续获取:
在这里插入图片描述

最新鸿蒙技术文章
独家项目实战教程
免费直播/训练营信息
👉 下方资源区已为你备好!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值