openharmony 鸿蒙 js-apis-enterprise-systemManager

2025-06-12 浏览 (1)

@ohos.enterprise.systemManager (System Management)

The systemManager module provides system management capabilities.

NOTE

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

The APIs of this module can be used only in the stage model.

The APIs of this module can be called only by a device administrator application that is enabled.

Modules to Import

import { systemManager } from '@kit.MDMKit';

systemManager.setNTPServer

setNTPServer(admin: Want, server: string): void

Sets the NTP server.

Required permissions: ohos.permission.ENTERPRISE_MANAGE_SYSTEM

System capability: SystemCapability.Customization.EnterpriseDeviceManager

Parameters

NameTypeMandatoryDescription
adminWantYesEnterpriseAdminExtensionAbility.
serverstringYesNTP server addresses separated by a comma (,). For example, ntpserver1.com,ntpserver2.com. The value can contain a maximum of 96 bytes (including the end character).

Error codes

For details about the error codes, see Enterprise Device Management Error Codes and Universal Error Codes.

IDError Message
9200001The application is not an administrator application of the device.
9200002The administrator application does not have permission to manage the device.
201Permission verification failed. The application does not have the permission required to call the API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

Example

import { systemManager } from '@kit.MDMKit';
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let server: string = "ntpserver.com";
try {
  systemManager.setNTPServer(wantTemp, server);
  console.info('Succeeded in setting NTPserver.');
} catch (err) {
  console.error(`Failed to set ntp server. Code is ${err.code}, message is ${err.message}`);
}

systemManager.getNTPServer

getNTPServer(admin: Want): string

Obtains the NTP server information.

Required permissions: ohos.permission.ENTERPRISE_MANAGE_SYSTEM

System capability: SystemCapability.Customization.EnterpriseDeviceManager

Parameters

NameTypeMandatoryDescription
adminWantYesEnterpriseAdminExtensionAbility.

Return value

TypeDescription
stringNTP server information.

Error codes

For details about the error codes, see Enterprise Device Management Error Codes and Universal Error Codes.

IDError Message
9200001The application is not an administrator application of the device.
9200002The administrator application does not have permission to manage the device.
201Permission verification failed. The application does not have the permission required to call the API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

Example

import { systemManager } from '@kit.MDMKit';
import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@ohos.base';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
try {
  systemManager.getNTPServer(wantTemp);
  console.info('Succeeded in getting NTP server.');
} catch (err) {
  console.error(`Failed to get ntp server. Code is ${err.code}, message is ${err.message}`);
}

systemManager.setOtaUpdatePolicy

setOtaUpdatePolicy(admin: Want, policy: OtaUpdatePolicy): void

Sets the update policy. In intranet updates, call systemManager.notifyUpdatePackages to notify the system of the update packages and then call this API to set the upgrade policy.

Required permissions: ohos.permission.ENTERPRISE_MANAGE_SYSTEM

System capability: SystemCapability.Customization.EnterpriseDeviceManager

Parameters

NameTypeMandatoryDescription
adminWantYesEnterpriseAdminExtensionAbility.
policyOtaUpdatePolicyYesOTA update policy to set.

Error codes

For details about the error codes, see Enterprise Device Management Error Codes and Universal Error Codes.

IDError Message
9200001The application is not an administrator application of the device.
9200002The administrator application does not have permission to manage the device.
201Permission verification failed. The application does not have the permission required to call the API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

Example

import { systemManager } from '@kit.MDMKit';
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
// Default update policy.
let otaUpdatePolicy1: systemManager.OtaUpdatePolicy = {
  "policyType": systemManager.PolicyType.DEFAULT,
  "version": "version_1.0.0.0",
};
try {
  systemManager.setOtaUpdatePolicy(wantTemp, otaUpdatePolicy1);
  console.info('Succeeded in setting ota update policy.');
} catch (err) {
  console.error(`Failed to set ota update policy. Code is ${err.code}, message is ${err.message}`);
}
// Prohibit update.
let otaUpdatePolicy2: systemManager.OtaUpdatePolicy = {
  "policyType": systemManager.PolicyType.PROHIBIT,
  "version": "version_1.0.0.1",
};
try {
  systemManager.setOtaUpdatePolicy(wantTemp, otaUpdatePolicy2);
  console.info('Succeeded in setting ota update policy.');
} catch (err) {
  console.error(`Failed to set ota update policy. Code is ${err.code}, message is ${err.message}`);
}
// Enforce update. 
let otaUpdatePolicy3: systemManager.OtaUpdatePolicy = {
  "policyType": systemManager.PolicyType.UPDATE_TO_SPECIFIC_VERSION,
  "version": "version_1.0.0.2",
  "latestUpdateTime": 1716343200, // Timestamp
};
try {
  systemManager.setOtaUpdatePolicy(wantTemp, otaUpdatePolicy3);
  console.info('Succeeded in setting ota update policy.');
} catch (err) {
  console.error(`Failed to set ota update policy. Code is ${err.code}, message is ${err.message}`);
}
// Update at the specified time period.
let otaUpdatePolicy4: systemManager.OtaUpdatePolicy = {
  "policyType": systemManager.PolicyType.WINDOWS,
  "version": "version_1.0.0.3",
  "installStartTime": 1716281049, // Timestamp
  "installEndTime": 1716343200, // Timestamp
};
try {
  systemManager.setOtaUpdatePolicy(wantTemp, otaUpdatePolicy4);
  console.info('Succeeded in setting ota update policy.');
} catch (err) {
  console.error(`Failed to set ota update policy. Code is ${err.code}, message is ${err.message}`);
}
// Delay the update.
let otaUpdatePolicy5: systemManager.OtaUpdatePolicy = {
  "policyType": systemManager.PolicyType.POSTPONE,
  "version": "version_1.0.0.4",
  "delayUpdateTime": 5, // Time for which the update is delayed, in hours.
};
try {
  systemManager.setOtaUpdatePolicy(wantTemp, otaUpdatePolicy5);
  console.info('Succeeded in setting ota update policy.');
} catch (err) {
  console.error(`Failed to set ota update policy. Code is ${err.code}, message is ${err.message}`);
}

systemManager.getOtaUpdatePolicy

getOtaUpdatePolicy(admin: Want): OtaUpdatePolicy

Queries the update policy.

Required permissions: ohos.permission.ENTERPRISE_MANAGE_SYSTEM

System capability: SystemCapability.Customization.EnterpriseDeviceManager

Parameters

NameTypeMandatoryDescription
adminWantYesEnterpriseAdminExtensionAbility.

Return value

TypeDescription
OtaUpdatePolicyOtaUpdatePolicy object containing the update policy obtained.

Error codes

For details about the error codes, see Enterprise Device Management Error Codes and Universal Error Codes.

IDError Message
9200001The application is not an administrator application of the device.
9200002The administrator application does not have permission to manage the device.
201Permission verification failed. The application does not have the permission required to call the API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

Example

import { systemManager } from '@kit.MDMKit';
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
try {
  let policy: systemManager.OtaUpdatePolicy= systemManager.getOtaUpdatePolicy(wantTemp);
  console.info(`Succeeded in getting update policy: ${JSON.stringify(policy)}`);
} catch (err) {
  console.error(`Failed to get update policy. Code is ${err.code}, message is ${err.message}`);
}

systemManager.notifyUpdatePackages

notifyUpdatePackages(admin: Want, packageInfo: UpdatePackageInfo): Promise<void>

Notifies the system of the update packages. In intranet updates, call this API to notify the system of the update packages, and then call systemManager.setOtaUpdatePolicy to set the update policy.

Required permissions: ohos.permission.ENTERPRISE_MANAGE_SYSTEM

System capability: SystemCapability.Customization.EnterpriseDeviceManager

Parameters

NameTypeMandatoryDescription
adminWantYesEnterpriseAdminExtensionAbility.
packageInfoUpdatePackageInfoYesInformation about the system update packages.

Return value

TypeDescription
Promise<void>Promise that returns no value. An error object will be thrown if the operation fails.

Error codes

For details about the error codes, see Enterprise Device Management Error Codes and Universal Error Codes.

IDError Message
9200001The application is not an administrator application of the device.
9200002The administrator application does not have permission to manage the device.
9201004The update packages do not exist or analyzing failed.
201Permission verification failed. The application does not have the permission required to call the API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

Example

import { systemManager } from '@kit.MDMKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let notify: systemManager.NotifyDescription = {
  "installTips": "installTips",
  "installTipsDetail": "installTips detail"
}
let description: systemManager.PackageDescription = {
  "notify": notify,
}
let updatePackages: Array<systemManager.Package> = [{
  "type": systemManager.PackageType.FIRMWARE,
  "path": "path",
  "fd": 60,
}]
let updatePackageInfo: systemManager.UpdatePackageInfo = {
  "version" : "1.0",
  "packages" : updatePackages,
  "description" : description,
};
systemManager.notifyUpdatePackages(wantTemp, updatePackageInfo).then(() => {
  console.info('Succeeded in notifying update packages.');
}).catch ((error: BusinessError) => {
  console.error(`Failed to notify update packages. Code is ${error.code},message is ${error.message}`);
});

systemManager.getUpdateResult

getUpdateResult(admin: Want, version: string): Promise<UpdateResult>

Obtains the system update result.

Required permissions: ohos.permission.ENTERPRISE_MANAGE_SYSTEM

System capability: SystemCapability.Customization.EnterpriseDeviceManager

Parameters

NameTypeMandatoryDescription
adminWantYesEnterpriseAdminExtensionAbility.
versionstringYesVersion of the update package.

Return value

TypeDescription
Promise<UpdateResult>Promise used to return the system update result.

Error codes

For details about the error codes, see Enterprise Device Management Error Codes and Universal Error Codes.

IDError Message
9200001The application is not an administrator application of the device.
9200002The administrator application does not have permission to manage the device.
201Permission verification failed. The application does not have the permission required to call the API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

Example

import { systemManager } from '@kit.MDMKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
systemManager.getUpdateResult(wantTemp, "1.0").then((result:systemManager.UpdateResult) => {
    console.info(`Succeeded in getting update result: ${JSON.stringify(result)}`);
  }).catch((error: BusinessError) => {
    console.error(`Get update result failed. Code is ${error.code},message is ${error.message}`);
  });

SystemUpdateInfo

Represents information about the system version to update.

System capability: SystemCapability.Customization.EnterpriseDeviceManager

NameTypeMandatoryDescription
versionNamestringYesSystem version to update.
firstReceivedTimenumberYesTime when the system update package is received for the first time.
packageTypestringYesType of the system update package to update.

OtaUpdatePolicy

Represents an OTA update policy.

System capability: SystemCapability.Customization.EnterpriseDeviceManager

NameTypeMandatoryDescription
policyTypePolicyTypeYesType of the update policy.
versionstringYesVersion of the software to update.
latestUpdateTimenumberNoLatest update time (timestamp).
delayUpdateTimenumberNoPeriod for which the update is postponed, in hours.
installStartTimenumberNoStart time (timestamp) of the installation window.
installEndTimenumberNoEnd time (timestamp) of the installation window.

PolicyType

Enumerates the update policy types.

System capability: SystemCapability.Customization.EnterpriseDeviceManager

NameValueDescription
DEFAULT0Default update policy, which periodically notifies the user of the update and starts the update after user confirmation.
PROHIBIT1Prohibit updates.
UPDATE_TO_SPECIFIC_VERSION2Enforce updates. In this case, latestUpdateTime must be specified.
WINDOWS3Update at the specified time window. In this case, installStartTime and installEndTime must be specified.
POSTPONE4Postpone updates. After the time specified by delayUpdateTime is over, the default update policy is used.

UpdatePackageInfo

Represents information about the system update packages.

System capability: SystemCapability.Customization.EnterpriseDeviceManager

NameTypeMandatoryDescription
versionstringYesVersion of the system update package.
packagesArray<Package>YesDetails about the system update packages.
descriptionPackageDescriptionNoDescription of the system update packages.

Package

Represents the details about a system update package.

System capability: SystemCapability.Customization.EnterpriseDeviceManager

NameTypeMandatoryDescription
typePackageTypeYesType of the system update package.
pathstringYesPath of the system update package. If fd is specified, pass in the update package name here.
fdnumberNoFile descriptor (FD) of the system update package. Currently, you cannot pass in path only. The fd parameter must also be passed in.

PackageDescription

Represents the description of a system update package.

System capability: SystemCapability.Customization.EnterpriseDeviceManager

NameTypeMandatoryDescription
notifyNotifyDescriptionNoUpdate notification defined by an enterprise.

NotifyDescription

Represents the update notification defined by an enterprise.

System capability: SystemCapability.Customization.EnterpriseDeviceManager

NameTypeMandatoryDescription
installTipsstringNoUpdate tips provided by the enterprise.
installTipsDetailstringNoDetails about the update tips customized by the enterprise.

UpdateResult

Represents the update result information.

System capability: SystemCapability.Customization.EnterpriseDeviceManager

NameTypeReadableWritableDescription
versionstringYesNoCurrent version of the system.
statusUpdateStatusYesNoSystem update status.
errorInfoErrorInfoYesNoError information.

ErrorInfo

Represents the update error information.

System capability: SystemCapability.Customization.EnterpriseDeviceManager

NameTypeReadableWritableDescription
codenumberYesNoError code.
messagestringYesNoError message.

PackageType

Enumerates the update package types.

System capability: SystemCapability.Customization.EnterpriseDeviceManager

NameValueDescription
FIRMWARE1Firmware.

UpdateStatus

Enumerates the system update statuses.

System capability: SystemCapability.Customization.EnterpriseDeviceManager

NameValueDescription
NO_UPDATE_PACKAGE-4The system update package of the specified version does not exist.
UPDATE_WAITING-3The system update package is waiting to be installed.
UPDATING-2The system update is being performed.
UPDATE_FAILURE-1The update failed.
UPDATE_SUCCESS0The update is successful.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙MDM Kit

harmony 鸿蒙Enterprise Device Management Error Codes

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

harmony 鸿蒙@ohos.enterprise.accountManager (Account Management) (System API)

harmony 鸿蒙@ohos.enterprise.accountManager (Account Management)

harmony 鸿蒙@ohos.enterprise.adminManager (Enterprise Device Management) (System API)

harmony 鸿蒙@ohos.enterprise.adminManager (Enterprise Device Management)

harmony 鸿蒙@ohos.enterprise.applicationManager (Application Management (System API)

harmony 鸿蒙@ohos.enterprise.applicationManager (Application Management)

harmony 鸿蒙@ohos.enterprise.bluetoothManager (Bluetooth Management) (System API)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/K4tzM0