目录

Update

has.getUpdateManager

has.getUpdateManager(): UpdateManager

Obtains the globally unique UpdateManager object to manage atomic service updates.

Since: 1.0.16

Returns:

UpdateManager object.

Example:

const updateManager = has.getUpdateManager();

UpdateManager

An object used to manage version updates.

Since: 1.0.16

[h2]UpdateManager.applyUpdate

UpdateManager.applyUpdate(): void

Forcibly updates to the new version and restarts the atomic service. This API should be called only after receiving the onUpdateReady callback, which indicates that the new version has been downloaded.

Since: 1.0.16

Error code information

Error Code Error Message
126 capability is not supported.

Example:

const updateManager = has.getUpdateManager();
updateManager.onCheckForUpdate(res => {
  console.info('Whether a new version is available:', res.hasUpdate);
});
updateManager.onUpdateReady(() => {
  has.showModal({
    title: 'Update reminder',
    content: 'The new version is ready. Do you want to restart now?',
    confirmText: 'YES',
    success: (res) => {
      if (res.confirm) {
        updateManager.applyUpdate();
      }
    },
    fail: (err) => {
      console.error('showModal fail', err);
    },
    complete: (res) => {
      console.info('showModal complete', res);
    }
  });
});

[h2]UpdateManager.onCheckForUpdate

UpdateManager.onCheckForUpdate(function callback)

Listens for the update check result requested from AppGallery Connect. AppGallery Connect automatically checks for updates upon every cold start of the atomic service.

For the same device, this API can be called up to six times a day and at most once every 30 minutes by an atomic service. The maximum listening duration supported by the registered listener is 20 seconds.

Since: 1.0.16

Parameters

Parameter Type Mandatory Description
callback function Yes Listener function passed by UpdateManager.onCheckForUpdate. It is used to return the update check result requested from AppGallery Connect.

Returns

An object containing the following fields.

Parameter Type Description
hasUpdate boolean

Whether a new version is available.

- true: yes

- false: no

Error code information

Error Code Error Message
102 Parameter error. Possible causes: listener should be a function.
126 capability is not supported.

Example:

const updateManager = has.getUpdateManager();
updateManager.onCheckForUpdate((res) => {
    console.info('Whether a new version is available:', res.hasUpdate);
});

[h2]UpdateManager.onUpdateFailed

UpdateManager.onUpdateFailed(function callback)

Listens for events indicating that the atomic service fails to be updated. It is called when the new version fails to be downloaded due to network problems.

Unlike mini programs, updates in AppGallery Connect do not trigger this API to return update failures. Nonetheless, ASCF still reserves this API for compatibility purposes.

Since: 1.0.16

Parameters

Parameter Type Mandatory Description
callback function Yes Listener function passed by UpdateManager.onUpdateFailed. It is used to return an update failure message, indicating that the download failed and recommending that the user check for network connection or try again later.

Error code information

Error Code Error Message
102 Parameter error. Possible causes: listener should be a function.

Example:

const updateManager = has.getUpdateManager();
updateManager.onUpdateFailed(() => {
  console.info('Update failed.');
});

[h2]UpdateManager.onUpdateReady

UpdateManager.onUpdateReady(function callback)

Listens for events indicating that the new version of the atomic service is downloaded. Once detecting a new version, AppGallery Connect automatically starts download (without developer intervention). This callback will then be triggered upon the completion of the download.

Since: 1.0.16

Parameters

Parameter Type Mandatory Description
callback function Yes Listener function passed by UpdateManager.onUpdateReady. It is used to return the result, prompting the user to restart the application.

Error code information

Error Code Error Message
102 Parameter error. Possible causes: listener should be a function.

Example:

const updateManager = has.getUpdateManager();
updateManager.onUpdateReady(() => {
  console.info('New version is ready.');
});