HarmonyOS 鸿蒙Next中获取Api版本号
HarmonyOS 鸿蒙Next中获取Api版本号
demo:
import { deviceInfo } from '@kit.BasicServicesKit';
@Entry
@Component
struct GetObjectAllFun {
@State message: string = '版本信息';
build() {
Column() {
Text(this.message)
.fontSize(23)
Button('获取版本')
.onClick(() => {
// 系统软件API版本:15
let a = deviceInfo.sdkApiVersion
// 首个版本系统软件API版本:1
let b = deviceInfo.firstApiVersion
// 发行版系统api版本:50003
let c = deviceInfo.distributionOSApiVersion
this.message = `系统软件API版本:${a}\n首个版本系统软件API版本:${b}\n发行版系统api版本:${c}`
})
.margin(10)
}
.height('100%')
}
}
更多关于HarmonyOS 鸿蒙Next中获取Api版本号的实战教程也可以访问 https://www.itying.com/category-93-b0.html
3 回复
在HarmonyOS Next中,使用BundleInfo
的compatibleApi
和targetApi
属性获取API版本号。通过BundleManager
获取当前应用的BundleInfo
对象,然后访问这两个属性即可。示例代码:
import bundleManager from '@ohos.bundle.bundleManager';
let bundleInfo = await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
let compatibleApi = bundleInfo.compatibleApi;
let targetApi = bundleInfo.targetApi;
在HarmonyOS Next中,可以通过@kit.BasicServicesKit
中的deviceInfo
模块获取API版本信息。代码示例中展示了三个关键属性:
deviceInfo.sdkApiVersion
:获取当前系统软件API版本(示例值为15)deviceInfo.firstApiVersion
:获取首个版本的系统软件API版本(示例值为1)deviceInfo.distributionOSApiVersion
:获取发行版系统API版本(示例值为50003)
通过按钮点击事件触发版本信息获取,并将结果拼接显示在Text组件中。这种方式适用于需要根据API版本进行兼容性判断或功能调用的场景。