HarmonyOS鸿蒙Next中获取版本号

HarmonyOS鸿蒙Next中获取版本号 怎么获取app.json5中的versionName字段,参考截图

5 回复

可通过如下接口获取版本号,示例:

let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION | bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_METADATA;
try {
  bundleManager.getBundleInfoForSelf(bundleFlags).then((data) => {
    hilog.info(0x0000, 'testTag', 'getBundleInfoForSelf successfully. Data: %{public}s', JSON.stringify(data));
  }).catch((err: BusinessError) => {
    hilog.error(0x0000, 'testTag', 'getBundleInfoForSelf failed. Cause: %{public}s', err.message);
  });
} catch (err) {
  let message = (err as BusinessError).message;
  hilog.error(0x0000, 'testTag', 'getBundleInfoForSelf failed: %{public}s', message);
}

更多关于HarmonyOS鸿蒙Next中获取版本号的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


你好,这个权限申请必须AGL签名么?

let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION;
bundleManager.getBundleInfoForSelf(bundleFlags).then((result) => {
  let versionName = result.versionName
})

在HarmonyOS(鸿蒙)Next中,获取系统版本号可以通过ohos.system.version模块实现。具体步骤如下:

  1. 首先,导入ohos.system.version模块。
  2. 使用SystemVersion.getVersion()方法获取系统版本信息。

示例代码如下:

import systemVersion from '@ohos.system.version';

let versionInfo = systemVersion.getVersion();
console.log("System Version: " + versionInfo.version);
console.log("API Version: " + versionInfo.apiVersion);

getVersion()方法返回一个包含versionapiVersion属性的对象,version表示系统版本号,apiVersion表示API版本号。

注意:此代码适用于HarmonyOS Next版本,具体实现可能因版本不同而有所差异。

在HarmonyOS(鸿蒙)Next中,获取系统版本号可以通过SystemCapability.SystemInfo模块实现。使用systemInfo.getSystemInfo()方法获取系统信息对象,然后通过systemInfo.osRelease属性获取版本号。

import systemInfo from '@ohos.systemInfo';

let sysInfo = systemInfo.getSystemInfo();
let version = sysInfo.osRelease;
console.log("HarmonyOS Version: " + version);

这段代码会输出当前设备的HarmonyOS版本号。

回到顶部