鸿蒙next 获取versionCode和versionName

前言导读

各位同学在实战开发中,是不是会遇到这样需求 服务端让你把你的app的versionCode版本号和versionName版本名,加入到公共参数里面 然后一起上传到服务端号分析数据呢。

效果图

具体实现

获取versionName

typescript 复制代码
/***
 *
 *  获取app版本号 versionName
 *
 */
public getVerisonName(): Promise <string> {
  return bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION)
    .then((bundleInfo) => bundleInfo.versionName)
    .catch(() => "");
}

获取versionCode

typescript 复制代码
/**
 *
 * 获取appversioncode
 * @returns
 */
public getVerisonNo(): Promise <string>{
  return bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION)
    .then((bundleInfo) => bundleInfo.versionCode.toString())
    .catch(() => "");

}

这里特别注意因为这获取versioncode和versionname 是异步获取 我们不能直接返回 需要使用 : Promise 返回 然后调用地方使用then回调去取值不然是拿不到值的

具体获取展示

scss 复制代码
import {SystemUtils} from '../utils/SystemUtils'


@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  @State versionCode:string='';
  @State versionName:string='';



  build() {
     Column(){
       Button('获取versionCode').onClick(()=>{
         SystemUtils.getInstance().getVerisonNo().then((data)=>{
           this.versionCode=data;
         })
       })

       Text(this.versionCode)
         . fontSize(20)
         .fontColor(Color.Black)
         .margin({top:20})

       Button('获取versionName').onClick(()=>{
         SystemUtils.getInstance().getVerisonName().then((data)=>{
           this.versionName=data;
         })
       }).margin({top:20})


       Text(this.versionName)
         . fontSize(20)
         .fontColor(Color.Black).margin({top:20})
     }
    .height('100%')
    .width('100%')
  }
}
相关推荐
熊猫钓鱼>_>4 小时前
鸿蒙ArkUI基础组件开发详解
华为·harmonyos
猫林老师5 小时前
Flutter for HarmonyOS开发指南(八):国际化与本地化深度实践
flutter·华为·harmonyos
夏文强6 小时前
HarmonyOS开发-ArkWeb开发指导
华为·harmonyos
Georgewu7 小时前
【HarmonyOS 6】SpeechKit中的朗读控件,初始化前不进行窗口舞台的设置,也不会报错,与文档描述不符。
harmonyos
Georgewu8 小时前
【HarmonyOS 6】静态和动态添加应用快捷方式详解
harmonyos
爱笑的眼睛119 小时前
HarmonyOS preview 预览文件 Kit 的入门讲解(配套后端代码)
华为·harmonyos
爱笑的眼睛1115 小时前
HarmonyOS后台代理提醒机制深度解析:从架构设计到场景化实践
华为·harmonyos
猫林老师17 小时前
Flutter for HarmonyOS开发指南(七):插件开发与平台能力桥接
flutter·华为·harmonyos
kirk_wang18 小时前
HarmonyOS 6.0 服务卡片实战:把「轻食刻」装进桌面,让轻断食一眼可控
华为·harmonyos
●VON1 天前
补充说明:Windows 完全可以开发 Qt 鸿蒙应用!(附专属适配方案)
windows·qt·华为·harmonyos·鸿蒙