@ohos.update (Update) (System API)

The update module implements update of the entire system, including built-in resources and preset applications, but not third-party applications.

There are three types of updates: SD card update, over the air (OTA) update, and factory reset update.

  • The SD card update depends on the update packages and SD cards.

  • The OTA update depends on the server deployed by the device manufacturer for managing update packages. The OTA server IP address is passed by the caller. The request interface is fixed and developed by the device manufacturer.

  • The factory reset update object provides the API for restoring factory settings.

NOTE

The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

The APIs provided by this module are system APIs.

Modules to Import

import { update } from '@kit.BasicServicesKit';

update.getOnlineUpdater

getOnlineUpdater(upgradeInfo: UpgradeInfo): Updater

Obtains an OnlineUpdater object.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Parameters

Name Type Mandatory Description
upgradeInfo UpgradeInfo Yes OnlineUpdater object information.

Return value

Type Description
Updater OnlineUpdater object.

Error codes

For details about the error codes, see Universal Error Codes.

ID Error Message
202 Permission verification failed. A non-system application calls a system API.

Example

try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

update.getRestorer

getRestorer(): Restorer

Obtains a Restorer object for restoring factory settings.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Return value

Type Description
Restorer Restorer object for restoring factory settings.

Error codes

For details about the error codes, see Universal Error Codes.

ID Error Message
202 Permission verification failed. A non-system application calls a system API.

Example

try {
  let restorer = update.getRestorer();
} catch(error) {
  console.error(`Fail to get restorer: ${error}`);
}

update.getLocalUpdater

getLocalUpdater(): LocalUpdater

Obtains a LocalUpdater object.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Return value

Type Description
LocalUpdater LocalUpdater object.

Error codes

For details about the error codes, see Universal Error Codes.

ID Error Message
202 Permission verification failed. A non-system application calls a system API.

Example

try {
  let localUpdater = update.getLocalUpdater();
} catch(error) {
  console.error(`Fail to get localUpdater error: ${error}`);
}

Updater

checkNewVersion

checkNewVersion(callback: AsyncCallback<CheckResult>): void

Checks whether a new version is available. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
callback AsyncCallback<CheckResult> Yes Callback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.checkNewVersion((err: BusinessError, result: update.CheckResult) => {
  console.info(`checkNewVersion isExistNewVersion  ${result?.isExistNewVersion}`);
});
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

checkNewVersion

checkNewVersion(): Promise<CheckResult>

Checks whether a new version is available. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Return value

Type Description
Promise<CheckResult> Promise used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.checkNewVersion().then((result: update.CheckResult) => {
    console.info(`checkNewVersion isExistNewVersion: ${result.isExistNewVersion}`);
    // Version digest information
    console.info(`checkNewVersion versionDigestInfo: ${result.newVersionInfo.versionDigestInfo.versionDigest}`);
    }).catch((err: BusinessError)=>{
      console.error(`checkNewVersion promise error ${JSON.stringify(err)}`);
    });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

getNewVersionInfo

getNewVersionInfo(callback: AsyncCallback<NewVersionInfo>): void

Obtains information about the new version. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
callback AsyncCallback<NewVersionInfo> Yes Callback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.getNewVersionInfo((err: BusinessError, info: update.NewVersionInfo) => {
    console.info(`info displayVersion = ${info?.versionComponents[0].displayVersion}`);
    console.info(`info innerVersion = ${info?.versionComponents[0].innerVersion}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

getNewVersionInfo

getNewVersionInfo(): Promise<NewVersionInfo>

Obtains information about the new version. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Return value

Type Description
Promise<NewVersionInfo> Promise used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.getNewVersionInfo().then((info: update.NewVersionInfo) => {
    console.info(`info displayVersion = ${info.versionComponents[0].displayVersion}`);
    console.info(`info innerVersion = ${info.versionComponents[0].innerVersion}`);
  }).catch((err: BusinessError) => {
    console.error(`getNewVersionInfo promise error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

getNewVersionDescription

getNewVersionDescription(versionDigestInfo: VersionDigestInfo, descriptionOptions: DescriptionOptions, callback: AsyncCallback<Array<ComponentDescription>>): void

Obtains the description file of the new version. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
versionDigestInfo VersionDigestInfo Yes Version digest information.
descriptionOptions DescriptionOptions Yes Options of the description file.
callback AsyncCallback<Array<ComponentDescription>> Yes Callback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Version digest information
const versionDigestInfo: update.VersionDigestInfo = {
  versionDigest: "versionDigest" // Version digest information in the check result
};

// Options of the description file
const descriptionOptions: update.DescriptionOptions = {
  format: update.DescriptionFormat.STANDARD, // Standard format
  language: "zh-cn" // Chinese
};

try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.getNewVersionDescription(versionDigestInfo, descriptionOptions, (err, info) => {
    console.info(`getNewVersionDescription info ${JSON.stringify(info)}`);
    console.info(`getNewVersionDescription err ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

getNewVersionDescription

getNewVersionDescription(versionDigestInfo: VersionDigestInfo, descriptionOptions: DescriptionOptions): Promise<Array<ComponentDescription>>

Obtains the description file of the new version. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
versionDigestInfo VersionDigestInfo Yes Version digest information.
descriptionOptions DescriptionOptions Yes Options of the description file.

Return value

Type Description
Promise<Array<ComponentDescription>> Promise used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Version digest information
const versionDigestInfo: update.VersionDigestInfo = {
  versionDigest: "versionDigest" // Version digest information in the check result
};

// Options of the description file
const descriptionOptions: update.DescriptionOptions = {
  format: update.DescriptionFormat.STANDARD, // Standard format
  language: "zh-cn" // Chinese
};

try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.getNewVersionDescription(versionDigestInfo, descriptionOptions)
    .then((info: Array<update.ComponentDescription>)=> {
    console.info(`getNewVersionDescription promise info ${JSON.stringify(info)}`);
  }).catch((err: BusinessError) => {
    console.error(`getNewVersionDescription promise error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

getCurrentVersionInfo

getCurrentVersionInfo(callback: AsyncCallback<CurrentVersionInfo>): void

Obtains information about the current version. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
callback AsyncCallback<CurrentVersionInfo> Yes Callback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.getCurrentVersionInfo((err: BusinessError, info: update.CurrentVersionInfo) => {
    console.info(`info osVersion = ${info?.osVersion}`);
    console.info(`info deviceName = ${info?.deviceName}`);
    console.info(`info displayVersion = ${info?.versionComponents[0].displayVersion}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

getCurrentVersionInfo

getCurrentVersionInfo(): Promise<CurrentVersionInfo>

Obtains information about the current version. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Return value

Type Description
Promise<CurrentVersionInfo> Promise used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.getCurrentVersionInfo().then((info: update.CurrentVersionInfo) => {
    console.info(`info osVersion = ${info.osVersion}`);
    console.info(`info deviceName = ${info.deviceName}`);
    console.info(`info displayVersion = ${info.versionComponents[0].displayVersion}`);
  }).catch((err: BusinessError) => {
    console.error(`getCurrentVersionInfo promise error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

getCurrentVersionDescription

getCurrentVersionDescription(descriptionOptions: DescriptionOptions, callback: AsyncCallback<Array<ComponentDescription>>): void

Obtains the description file of the current version. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
descriptionOptions DescriptionOptions Yes Options of the description file.
callback AsyncCallback<Array<ComponentDescription>> Yes Callback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

// Options of the description file
const descriptionOptions: update.DescriptionOptions = {
  format: update.DescriptionFormat.STANDARD, // Standard format
  language: "zh-cn" // Chinese
};

try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.getCurrentVersionDescription(descriptionOptions, (err, info) => {
    console.info(`getCurrentVersionDescription info ${JSON.stringify(info)}`);
    console.info(`getCurrentVersionDescription err ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

getCurrentVersionDescription

getCurrentVersionDescription(descriptionOptions: DescriptionOptions): Promise<Array<ComponentDescription>>

Obtains the description file of the current version. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
descriptionOptions DescriptionOptions Yes Options of the description file.

Return value

Type Description
Promise<Array<ComponentDescription>> Promise used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
// Options of the description file
const descriptionOptions: update.DescriptionOptions = {
  format: update.DescriptionFormat.STANDARD, // Standard format
  language: "zh-cn" // Chinese
};
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.getCurrentVersionDescription(descriptionOptions).then((info: Array<update.ComponentDescription>) => {
    console.info(`getCurrentVersionDescription promise info ${JSON.stringify(info)}`);
  }).catch((err: BusinessError) => {
    console.error(`getCurrentVersionDescription promise error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

getTaskInfo

getTaskInfo(callback: AsyncCallback<TaskInfo>): void

Obtains information about the update task. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
callback AsyncCallback<TaskInfo> Yes Callback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.getTaskInfo((err: BusinessError, info: update.TaskInfo) => {
    console.info(`getTaskInfo isexistTask= ${info?.existTask}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

getTaskInfo

getTaskInfo(): Promise<TaskInfo>

Obtains information about the update task. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Return value

Type Description
Promise<TaskInfo> Promise used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.getTaskInfo().then((info: update.TaskInfo) => {
    console.info(`getTaskInfo isexistTask= ${info.existTask}`);
  }).catch((err: BusinessError) => {
    console.error(`getTaskInfo promise error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

download

download(versionDigestInfo: VersionDigestInfo, downloadOptions: DownloadOptions, callback: AsyncCallback<void>): void

Downloads the new version. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
versionDigestInfo VersionDigestInfo Yes Version digest information.
downloadOptions DownloadOptions Yes Download options.
callback AsyncCallback<void> Yes Callback used to return the result. If the operation is successful, err is undefined; otherwise, err is an Error object.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Version digest information
const versionDigestInfo: update.VersionDigestInfo = {
  versionDigest: "versionDigest" // Version digest information in the check result
};

// Download options
const downloadOptions: update.DownloadOptions = {
  allowNetwork: update.NetType.CELLULAR, // Whether to allow download over data network
  order: update.Order.DOWNLOAD // Download
};
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.download(versionDigestInfo, downloadOptions, (err: BusinessError) => {
    console.info(`download error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

download

download(versionDigestInfo: VersionDigestInfo, downloadOptions: DownloadOptions): Promise<void>

Downloads the new version. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
versionDigestInfo VersionDigestInfo Yes Version digest information.
downloadOptions DownloadOptions Yes Download options.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Version digest information
const versionDigestInfo: update.VersionDigestInfo = {
  versionDigest: "versionDigest" // Version digest information in the check result
};

// Download options
const downloadOptions: update.DownloadOptions = {
  allowNetwork: update.NetType.CELLULAR, // Whether to allow download over data network
   order: update.Order.DOWNLOAD // Download
};
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.download(versionDigestInfo, downloadOptions).then(() => {
    console.info(`download start`);
  }).catch((err: BusinessError) => {
    console.error(`download error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

resumeDownload

resumeDownload(versionDigestInfo: VersionDigestInfo, resumeDownloadOptions: ResumeDownloadOptions, callback: AsyncCallback<void>): void

Resumes download of the new version. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
versionDigestInfo VersionDigestInfo Yes Version digest information.
resumeDownloadOptions ResumeDownloadOptions Yes Options for resuming download.
callback AsyncCallback<void> Yes Callback used to return the result. If the operation is successful, err is undefined; otherwise, err is an Error object.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Version digest information
const versionDigestInfo : update.VersionDigestInfo= {
  versionDigest: "versionDigest" // Version digest information in the check result
};

// Options for resuming download
const resumeDownloadOptions : update.ResumeDownloadOptions= {
  allowNetwork: update.NetType.CELLULAR, // Whether to allow download over data network
};
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.resumeDownload(versionDigestInfo, resumeDownloadOptions, (err: BusinessError) => {
    console.info(`resumeDownload error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

resumeDownload

resumeDownload(versionDigestInfo: VersionDigestInfo, resumeDownloadOptions: ResumeDownloadOptions): Promise<void>

Resumes download of the new version. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
versionDigestInfo VersionDigestInfo Yes Version digest information.
resumeDownloadOptions ResumeDownloadOptions Yes Options for resuming download.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Version digest information
const versionDigestInfo: update.VersionDigestInfo = {
  versionDigest: "versionDigest" // Version digest information in the check result
};

// Options for resuming download
const resumeDownloadOptions: update.ResumeDownloadOptions = {
  allowNetwork: update.NetType.CELLULAR, // Whether to allow download over data network
};
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.resumeDownload(versionDigestInfo, resumeDownloadOptions).then(() => {
    console.info(`resumeDownload start`);
  }).catch((err: BusinessError) => {
    console.error(`resumeDownload error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

pauseDownload

pauseDownload(versionDigestInfo: VersionDigestInfo, pauseDownloadOptions: PauseDownloadOptions, callback: AsyncCallback<void>): void

Pauses download of the new version. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
versionDigestInfo VersionDigestInfo Yes Version digest information.
pauseDownloadOptions PauseDownloadOptions Yes Options for pausing download.
callback AsyncCallback<void> Yes Callback used to return the result. If the operation is successful, err is undefined; otherwise, err is an Error object.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Version digest information
const versionDigestInfo: update.VersionDigestInfo = {
  versionDigest: "versionDigest" // Version digest information in the check result
};

// Options for pausing download
const pauseDownloadOptions: update.PauseDownloadOptions = {
  isAllowAutoResume: true // Whether to allow automatic resuming of download
};
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.pauseDownload(versionDigestInfo, pauseDownloadOptions, (err: BusinessError) => {
    console.info(`pauseDownload error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

pauseDownload

pauseDownload(versionDigestInfo: VersionDigestInfo, pauseDownloadOptions: PauseDownloadOptions): Promise<void>

Pauses download of the new version. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
versionDigestInfo VersionDigestInfo Yes Version digest information.
pauseDownloadOptions PauseDownloadOptions Yes Options for pausing download.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Version digest information
const versionDigestInfo: update.VersionDigestInfo = {
  versionDigest: "versionDigest" // Version digest information in the check result
};

// Options for pausing download
const pauseDownloadOptions: update.PauseDownloadOptions = {
  isAllowAutoResume: true // Whether to allow automatic resuming of download
};
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.pauseDownload(versionDigestInfo, pauseDownloadOptions).then(() => {
    console.info(`pauseDownload`);
  }).catch((err: BusinessError)  => {
    console.error(`pauseDownload error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

upgrade

upgrade(versionDigestInfo: VersionDigestInfo, upgradeOptions: UpgradeOptions, callback: AsyncCallback<void>): void

Updates the version. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
versionDigestInfo VersionDigestInfo Yes Version digest information.
upgradeOptions UpgradeOptions Yes Update options.
callback AsyncCallback<void> Yes Callback used to return the result. If the operation is successful, err is undefined; otherwise, err is an Error object.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Version digest information
const versionDigestInfo: update.VersionDigestInfo = {
  versionDigest: "versionDigest" // Version digest information in the check result
};

// Installation options
const upgradeOptions: update.UpgradeOptions = {
  order: update.Order.INSTALL // Installation command
};
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.upgrade(versionDigestInfo, upgradeOptions, (err: BusinessError) => {
    console.info(`upgrade error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

upgrade

upgrade(versionDigestInfo: VersionDigestInfo, upgradeOptions: UpgradeOptions): Promise<void>

Updates the version. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
versionDigestInfo VersionDigestInfo Yes Version digest information.
upgradeOptions UpgradeOptions Yes Update options.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Version digest information
const versionDigestInfo: update.VersionDigestInfo = {
  versionDigest: "versionDigest" // Version digest information in the check result
};

// Installation options
const upgradeOptions: update.UpgradeOptions = {
  order: update.Order.INSTALL // Installation command
};
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.upgrade(versionDigestInfo, upgradeOptions).then(() => {
    console.info(`upgrade start`);
  }).catch((err: BusinessError) => {
    console.error(`upgrade error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

clearError

clearError(versionDigestInfo: VersionDigestInfo, clearOptions: ClearOptions, callback: AsyncCallback<void>): void

Clears errors. If an exception occurs during version download or installation, the upgrade package and upgrade status are cleared. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
versionDigestInfo VersionDigestInfo Yes Version digest information.
clearOptions ClearOptions Yes Clear options.
callback AsyncCallback<void> Yes Callback used to return the result. If the operation is successful, err is undefined; otherwise, err is an Error object.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Version digest information
const versionDigestInfo: update.VersionDigestInfo = {
  versionDigest: "versionDigest" // Version digest information in the check result
};

// Options for clearing errors
const clearOptions: update.ClearOptions = {
  status: update.UpgradeStatus.UPGRADE_FAIL,
};
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.clearError(versionDigestInfo, clearOptions, (err: BusinessError) => {
    console.info(`clearError error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

clearError

clearError(versionDigestInfo: VersionDigestInfo, clearOptions: ClearOptions): Promise<void>

Clears errors. If an exception occurs during version download or installation, the upgrade package and upgrade status are cleared. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
versionDigestInfo VersionDigestInfo Yes Version digest information.
clearOptions ClearOptions Yes Update options.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Version digest information
const versionDigestInfo: update.VersionDigestInfo = {
  versionDigest: "versionDigest" // Version digest information in the check result
};

// Options for clearing errors
const clearOptions: update.ClearOptions = {
  status: update.UpgradeStatus.UPGRADE_FAIL,
};
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.clearError(versionDigestInfo, clearOptions).then(() => {
    console.info(`clearError success`);
  }).catch((err: BusinessError) => {
    console.error(`clearError error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

getUpgradePolicy

getUpgradePolicy(callback: AsyncCallback<UpgradePolicy>): void

Obtains the update policy. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
callback AsyncCallback<UpgradePolicy> Yes Callback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.getUpgradePolicy((err: BusinessError, policy: update.UpgradePolicy) => {
    console.info(`policy downloadStrategy = ${policy?.downloadStrategy}`);
    console.info(`policy autoUpgradeStrategy = ${policy?.autoUpgradeStrategy}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

getUpgradePolicy

getUpgradePolicy(): Promise<UpgradePolicy>

Obtains the update policy. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Return value

Type Description
Promise<UpgradePolicy> Promise used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.getUpgradePolicy().then((policy: update.UpgradePolicy) => {
    console.info(`policy downloadStrategy = ${policy.downloadStrategy}`);
    console.info(`policy autoUpgradeStrategy = ${policy.autoUpgradeStrategy}`);
  }).catch((err: BusinessError)  => {
    console.error(`getUpgradePolicy promise error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

setUpgradePolicy

setUpgradePolicy(policy: UpgradePolicy, callback: AsyncCallback<void>): void

Sets the update policy. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
policy UpgradePolicy Yes Update policy.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

const policy: update.UpgradePolicy = {
  downloadStrategy: false,
  autoUpgradeStrategy: false,
  autoUpgradePeriods: [ { start: 120, end: 240 }] // Automatic update period, in minutes
};
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.setUpgradePolicy(policy, (err: BusinessError) => {
    console.info(`setUpgradePolicy result: ${err}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

setUpgradePolicy

setUpgradePolicy(policy: UpgradePolicy): Promise<void>

Sets the update policy. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
policy UpgradePolicy Yes Update policy.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

const policy: update.UpgradePolicy = {
  downloadStrategy: false,
  autoUpgradeStrategy: false,
  autoUpgradePeriods: [ { start: 120, end: 240 }] // Automatic update period, in minutes
};
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.setUpgradePolicy(policy).then(() => {
    console.info(`setUpgradePolicy success`);
  }).catch((err: BusinessError) => {
    console.error(`setUpgradePolicy promise error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

terminateUpgrade

terminateUpgrade(callback: AsyncCallback<void>): void

Terminates the update. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
callback AsyncCallback<void> Yes Callback used to return the result. If the operation is successful, err is undefined; otherwise, err is an Error object.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.terminateUpgrade((err: BusinessError) => {
    console.info(`terminateUpgrade error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

terminateUpgrade

terminateUpgrade(): Promise<void>

Terminates the update. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.terminateUpgrade().then(() => {
    console.info(`terminateUpgrade success`);
  }).catch((err: BusinessError) => {
    console.error(`terminateUpgrade error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

on

on(eventClassifyInfo: EventClassifyInfo, taskCallback: UpgradeTaskCallback): void

Enables listening for update events. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Parameters

Name Type Mandatory Description
eventClassifyInfo EventClassifyInfo Yes Event information.
taskCallback23+ UpgradeTaskCallback Yes Event callback.

Error codes

For details about the error codes, see Universal Error Codes.

ID Error Message
202 Permission verification failed. A non-system application calls a system API.

Example

const eventClassifyInfo: update.EventClassifyInfo = {
  eventClassify: update.EventClassify.TASK, // Listening for update events
  extraInfo: ""
};
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.on(eventClassifyInfo, (eventInfo: update.EventInfo) => {
    console.info(`updater on ${JSON.stringify(eventInfo)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

off

off(eventClassifyInfo: EventClassifyInfo, taskCallback?: UpgradeTaskCallback): void

Disables listening for update events. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Parameters

Name Type Mandatory Description
eventClassifyInfo EventClassifyInfo Yes Event information.
taskCallback23+ UpgradeTaskCallback No Event callback.

Error codes

For details about the error codes, see Universal Error Codes.

ID Error Message
202 Permission verification failed. A non-system application calls a system API.

Example

const eventClassifyInfo: update.EventClassifyInfo = {
  eventClassify: update.EventClassify.TASK, // Listening for update events
  extraInfo: ""
};
try {
  const upgradeInfo: update.UpgradeInfo = {
    upgradeApp: "com.ohos.ota.updateclient",
    businessType: {
      vendor: update.BusinessVendor.PUBLIC,
      subType: update.BusinessSubType.FIRMWARE
    }
  };
  let updater = update.getOnlineUpdater(upgradeInfo);
  updater.off(eventClassifyInfo, (eventInfo: update.EventInfo) => {
    console.info(`updater off ${JSON.stringify(eventInfo)}`);
  });
} catch(error) {
  console.error(`Fail to get updater error: ${error}`);
}

Restorer

factoryReset

factoryReset(callback: AsyncCallback<void>): void

Clears the user data partition. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.FACTORY_RESET

Parameters

Name Type Mandatory Description
callback AsyncCallback<void> Yes Callback used to return the result. If the operation fails, err is an error object and a callback is returned. If the operation is successful, err is undefined and no callback is returned.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

try {
  let restorer = update.getRestorer();
  restorer.factoryReset((err) => {
    console.info(`factoryReset error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get restorer: ${error}`);
}

factoryReset

factoryReset(): Promise<void>

Clears the user data partition. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.FACTORY_RESET

Return value

Type Description
Promise<void> Promise that returns no value. If the operation fails, a callback is returned. If the operation is successful, no callback is returned.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
try {
  let restorer = update.getRestorer();
  restorer.factoryReset().then(() => {
    console.info(`factoryReset success`);
  }).catch((err: BusinessError) => {
    console.error(`factoryReset error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get restorer: ${error}`);
}

forceFactoryReset23+

forceFactoryReset(): Promise<void>

Clears the user data partition and the file key. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.FORCE_FACTORY_RESET

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
try {
  let restorer = update.getRestorer();
  restorer.forceFactoryReset().then(() => {
    console.info(`forceFactoryReset success`);
  }).catch((err: BusinessError) => {
    console.error(`forceFactoryReset error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get restorer: ${error}`);
}

deepFactoryReset

deepFactoryReset(factoryResetStrategy: FactoryResetStrategy): Promise<void>

Clears the user data partition and OS partition by means of overwriting. This API uses a promise to return the result.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.FACTORY_RESET

Parameters

Name Type Mandatory Description
factoryResetStrategy FactoryResetStrategy Yes Factory reset strategy.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
try {
  let restorer = update.getRestorer();
  let factoryResetStrategy: update.FactoryResetStrategy = {
    scope: update.FactoryResetScope.DATA,
    strategy: "deepFactoryReset test"
  };
  restorer.deepFactoryReset(factoryResetStrategy).then(() => {
    console.info(`deepFactoryReset success`);
  }).catch((err: BusinessError) => {
    console.error(`deepFactoryReset error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get restorer: ${error}`);
}

getDeepFactoryResetInfo

getDeepFactoryResetInfo(factoryResetStrategy: FactoryResetStrategy): Promise<FactoryResetInfo>

Obtains the factory reset information. This API uses a promise to return the result.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.FACTORY_RESET

Parameters

Name Type Mandatory Description
factoryResetStrategy FactoryResetStrategy Yes Factory reset strategy.

Return value

Type Description
Promise<FactoryResetInfo> Promise used to return the factory reset information.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let factoryResetStrategy: update.FactoryResetStrategy = {
  scope: update.FactoryResetScope.DATA,
  strategy: "deepFactoryReset"
};
try {
  let restorer = update.getRestorer();
  restorer.getDeepFactoryResetInfo(factoryResetStrategy).then((info: update.FactoryResetInfo) => {
    console.info(`getDeepFactoryResetInfo success`);
  }).catch((err: BusinessError) => {
    console.error(`getDeepFactoryResetInfo promise error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get restorer: ${error}`);
}

LocalUpdater

verifyUpgradePackage

verifyUpgradePackage(upgradeFile: UpgradeFile, certsFile: string, callback: AsyncCallback<void>): void

Verifies the update package. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
upgradeFile UpgradeFile Yes Update file.
certsFile string Yes Path of the certificate file.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

const upgradeFile: update.UpgradeFile = {
  fileType: update.ComponentType.OTA, // OTA package
  filePath: "path" // Path of the local update package
};

try {
  let localUpdater = update.getLocalUpdater();
  localUpdater.verifyUpgradePackage(upgradeFile, "certFilePath", (err) => {
    console.info(`verifyUpgradePackage error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get localUpdater error: ${error}`);
}

verifyUpgradePackage

verifyUpgradePackage(upgradeFile: UpgradeFile, certsFile: string): Promise<void>

Verifies the update package. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
upgradeFile UpgradeFile Yes Update file.
certsFile string Yes Path of the certificate file.

Return value

Type Description
Promise<void> Promise used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

const upgradeFile: update.UpgradeFile = {
  fileType: update.ComponentType.OTA, // OTA package
  filePath: "path" // Path of the local update package
};

try {
  let localUpdater = update.getLocalUpdater();
  localUpdater.verifyUpgradePackage(upgradeFile, "certFilePath").then(() => {
    console.info(`verifyUpgradePackage success`);
  }).catch((err: BusinessError) => {
    console.error(`verifyUpgradePackage error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get localUpdater error: ${error}`);
}

applyNewVersion

applyNewVersion(upgradeFiles: Array<UpgradeFile>, callback: AsyncCallback<void>): void

Installs the update package. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
upgradeFiles Array<UpgradeFile> Yes Update file.
callback AsyncCallback<void> Yes Callback used to return the result. If the operation is successful, err is undefined; otherwise, err is an Error object.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

const upgradeFiles: Array<update.UpgradeFile> = [{
  fileType: update.ComponentType.OTA, // OTA package
  filePath: "path" // Path of the local update package
}];

try {
  let localUpdater = update.getLocalUpdater();
  localUpdater.applyNewVersion(upgradeFiles, (err) => {
    console.info(`applyNewVersion error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get localUpdater error: ${error}`);
}

applyNewVersion

applyNewVersion(upgradeFiles: Array<UpgradeFile>): Promise<void>

Installs the update package. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Required permissions: ohos.permission.UPDATE_SYSTEM

Parameters

Name Type Mandatory Description
upgradeFiles Array<UpgradeFile> Yes Update file.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Update Error Codes.

ID Error Message
201 Permission denied.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter verification failed.
11500104 IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

const upgradeFiles: Array<update.UpgradeFile> = [{
  fileType: update.ComponentType.OTA, // OTA package
  filePath: "path" // Path of the local update package
}];

try {
  let localUpdater = update.getLocalUpdater();
  localUpdater.applyNewVersion(upgradeFiles).then(() => {
    console.info(`applyNewVersion success`);
  }).catch((err: BusinessError) => {
    console.error(`applyNewVersion error ${JSON.stringify(err)}`);
  });
} catch(error) {
  console.error(`Fail to get localUpdater error: ${error}`);
}

on

on(eventClassifyInfo: EventClassifyInfo, taskCallback: UpgradeTaskCallback): void

Enables listening for update events. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Parameters

Name Type Mandatory Description
eventClassifyInfo EventClassifyInfo Yes Event information.
taskCallback23+ UpgradeTaskCallback Yes Event callback.

Error codes

For details about the error codes, see Universal Error Codes.

ID Error Message
202 Permission verification failed. A non-system application calls a system API.

Example

const eventClassifyInfo: update.EventClassifyInfo = {
  eventClassify: update.EventClassify.TASK, // Listening for update events
  extraInfo: ""
};

let onTaskUpdate: update.UpgradeTaskCallback = (eventInfo: update.EventInfo) => {
  console.info(`on eventInfo id `, eventInfo.eventId);
};

try {
  let localUpdater = update.getLocalUpdater();
  localUpdater.on(eventClassifyInfo, onTaskUpdate);
} catch(error) {
  console.error(`Fail to get localUpdater error: ${error}`);
}

off

off(eventClassifyInfo: EventClassifyInfo, taskCallback?: UpgradeTaskCallback): void

Disables listening for update events. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Parameters

Name Type Mandatory Description
eventClassifyInfo EventClassifyInfo Yes Event information.
taskCallback23+ UpgradeTaskCallback No Event callback.

Error codes

For details about the error codes, see Universal Error Codes.

ID Error Message
202 Permission verification failed. A non-system application calls a system API.

Example

const eventClassifyInfo: update.EventClassifyInfo = {
  eventClassify: update.EventClassify.TASK, // Listening for update events
  extraInfo: ""
};

let onTaskUpdate: update.UpgradeTaskCallback = (eventInfo: update.EventInfo) => {
  console.info(`on eventInfo id `, eventInfo.eventId);
};

try {
  let localUpdater = update.getLocalUpdater();
  localUpdater.off(eventClassifyInfo, onTaskUpdate);
} catch(error) {
  console.error(`Fail to get localUpdater error: ${error}`);
}

UpgradeInfo

Represents update information.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
upgradeApp string No No Application package name.
businessType BusinessType No No Update service type.

BusinessType

Represents an update service type.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
vendor BusinessVendor No No Supplier or vendor.
subType BusinessSubType No No Represents an update type.

CheckResult

Represents the package check result.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
isExistNewVersion boolean No No Whether a new version is available.
The value true indicates that a new version is available, and the value false indicates the opposite.
newVersionInfo NewVersionInfo No No Information about the new version.

NewVersionInfo

Represents information about the new version.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
versionDigestInfo VersionDigestInfo No No Version digest information.
versionComponents Array<VersionComponent> No No Version components.

VersionDigestInfo

Represents version digest information.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
versionDigest string No No Version digest information.

VersionComponent

Represents a version component.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
componentId string No No Component ID.
componentType ComponentType No No Component type.
upgradeAction UpgradeAction No No Update mode.
displayVersion string No No Display version number.
innerVersion string No No Internal version number.
size int No No Size of the update package, in bytes.
effectiveMode EffectiveMode No No Effective mode.
descriptionInfo DescriptionInfo No No Information about the version description file.
otaMode20+ OtaMode No Yes OTA mode.

DescriptionOptions

Represents options of the description file.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
format DescriptionFormat No No Format of the description file.
language string No No Language of the description file.

ComponentDescription

Represents a component description file.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
componentId string No No Component ID.
descriptionInfo DescriptionInfo No No Information about the description file.

DescriptionInfo

Represents information about the version description file.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
descriptionType DescriptionType No No Type of the description file.
content string No No Content of the description file.

CurrentVersionInfo

Represents information about the current version.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
osVersion string No No System version number.
deviceName string No No Device name.
versionComponents Array<VersionComponent> No No Version components.

DownloadOptions

Represents download options.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
allowNetwork NetType No No Network type.
order Order No No Update command.

ResumeDownloadOptions

Represents options for resuming download.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
allowNetwork NetType No No Network type.

PauseDownloadOptions

Represents options for pausing download.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
isAllowAutoResume boolean No No Whether to allow automatic resuming of download.
The value true indicates that automatic resuming is allowed, and the value false indicates the opposite.

UpgradeOptions

Represents update options.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
order Order No No Update command.

ClearOptions

Represents options for clearing errors.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
status UpgradeStatus No No Error status.

UpgradePolicy

Represents an update policy.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
downloadStrategy boolean No No Automatic download policy.
The value true indicates that automatic download is supported, and the value false indicates the opposite.
autoUpgradeStrategy boolean No No Automatic update policy.
The value true indicates that automatic update is supported, and the value false indicates the opposite.
autoUpgradePeriods Array<UpgradePeriod> No No Automatic update period.

UpgradePeriod

Represents an automatic update period.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
start int No No Start time.
end int No No End time.

TaskInfo

Task information.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
existTask boolean No No Whether a task exists.
The value true indicates that the task exists, and the value false indicates the opposite.
taskBody TaskBody No No Task data.

EventInfo

Describes event information.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
eventId EventId No No Event ID.
taskBody TaskBody No No Task data.

TaskBody

Represents task data.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
versionDigestInfo VersionDigestInfo No No Version digest information.
status UpgradeStatus No No Update status.
subStatus int No No Sub-status.
progress int No No Progress.
installMode int No No Installation mode.
errorMessages Array<ErrorMessage> No No Error message.
versionComponents Array<VersionComponent> No No Version components.

ErrorMessage

Represents an error message.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
errorCode int No No Error code.
errorMessage string No No Error message.

EventClassifyInfo

Describes event type information.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
eventClassify EventClassify No No Event type.
extraInfo string No No Additional information.

UpgradeFile

Represents an update file.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
fileType ComponentType No No File type.
filePath string No No File path.

FactoryResetStrategy

Describes the factory reset strategy.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
scope FactoryResetScope No No Reset scope.
strategy string No No Reset strategy.

FactoryResetInfo

Describes the information of restoring factory settings.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Read-Only Optional Description
duration int No No Duration required for restoring factory settings.

FactoryResetScope

Describes the scope of restoring factory settings.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Value Description
DATA 1 User data.
DATA_AND_OS 2 User data and operating system.

UpgradeTaskCallback23+

type UpgradeTaskCallback = (eventInfo: EventInfo) => void

Represents an event callback.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Type Mandatory Description
eventInfo EventInfo Yes Event information.

BusinessVendor

Represents a device vendor.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Value Description
PUBLIC "public" Open source.

BusinessSubType

Represents an update type.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Value Description
FIRMWARE 1 Firmware.

ComponentType

Represents a component type.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Value Description
OTA 1 Firmware.

UpgradeAction

Enumerates update actions.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Value Description
UPGRADE "upgrade" Differential package.
RECOVERY "recovery" Recovery package.

EffectiveMode

Enumerates effective modes.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Value Description
COLD 1 Cold update.
LIVE 2 Live update.
LIVE_AND_COLD 3 Hybrid live and cold update.

OtaMode20+

Enumerates the update modes.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Value Description
REGULAR_OTA 0 Regular update.
STREAM_OTA 1 Streaming update.
AB_REGULAR_OTA 2 Regular A/B update.
AB_STREAM_OTA 3 Streaming A/B update.

DescriptionType

Enumerates description file types.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Value Description
CONTENT 0 Content.
URI 1 Link.

DescriptionFormat

Enumerates description file formats.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Value Description
STANDARD 0 Standard format.
SIMPLIFIED 1 Simple format.

NetType

Enumerates network types.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Value Description
CELLULAR 1 Data network.
METERED_WIFI 2 Wi-Fi hotspot.
NOT_METERED_WIFI 4 Non Wi-Fi hotspot.
WIFI 6 Wi-Fi.
CELLULAR_AND_WIFI 7 Data network and Wi-Fi.

Order

Enumerates update commands.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Value Description
DOWNLOAD 1 Download.
INSTALL 2 Install.
DOWNLOAD_AND_INSTALL 3 Download and install.
APPLY 4 Apply.
INSTALL_AND_APPLY 6 Install and apply.

UpgradeStatus

Enumerates update states.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Value Description
WAITING_DOWNLOAD 20 Waiting for download.
DOWNLOADING 21 Downloading.
DOWNLOAD_PAUSED 22 Download paused.
DOWNLOAD_FAIL 23 Download failed.
WAITING_INSTALL 30 Waiting for installation.
UPDATING 31 Updating.
WAITING_APPLY 40 Waiting for applying the update.
APPLYING 41 Applying the update.
UPGRADE_SUCCESS 50 Update succeeded.
UPGRADE_FAIL 51 Update failed.

EventClassify

Represents an event type.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Value Description
TASK 0x01000000 Task event.

EventId

Enumerates event IDs.

System API: This is a system API.

System capability: SystemCapability.Update.UpdateService

Name Value Description
EVENT_TASK_BASE EventClassify.TASK Task event.
EVENT_TASK_RECEIVE 0x01000001 Task received.
EVENT_TASK_CANCEL 0x01000002 Task cancelled.
EVENT_DOWNLOAD_WAIT 0x01000003 Waiting for download.
EVENT_DOWNLOAD_START 0x01000004 Download started.
EVENT_DOWNLOAD_UPDATE 0x01000005 Download progress update.
EVENT_DOWNLOAD_PAUSE 0x01000006 Download paused.
EVENT_DOWNLOAD_RESUME 0x01000007 Download resumed.
EVENT_DOWNLOAD_SUCCESS 0x01000008 Download succeeded.
EVENT_DOWNLOAD_FAIL 0x01000009 Download failed.
EVENT_UPGRADE_WAIT 0x0100000A Waiting for update.
EVENT_UPGRADE_START 0x0100000B Update started.
EVENT_UPGRADE_UPDATE 0x0100000C Update in progress.
EVENT_APPLY_WAIT 0x0100000D Waiting for applying the update.
EVENT_APPLY_START 0x0100000E Applying the update.
EVENT_UPGRADE_SUCCESS 0x0100000F Update succeeded.
EVENT_UPGRADE_FAIL 0x01000010 Update failed.