鸿蒙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%')
  }
}
相关推荐
李李李勃谦2 小时前
鸿蒙PC密码管理器实战:本地加密存储与自动填充完整实现
华为·harmonyos
Swift社区3 小时前
鸿蒙 App 架构中的“领域拆分”
华为·架构·harmonyos
maaath6 小时前
【maaath】Flutter for OpenHarmony 手表配饰应用实战开发
flutter·华为·harmonyos
maaath7 小时前
【maaath】Flutter for OpenHarmony 跨平台计算器应用开发实践
flutter·华为·harmonyos
前端不太难12 小时前
鸿蒙PC和App:都在走向 System
华为·状态模式·harmonyos
maaath12 小时前
【maaath】Flutter for OpenHarmony 闹钟时钟应用开发实战
flutter·华为·harmonyos
maaath12 小时前
【maaath】Flutter for OpenHarmony 短信管理应用实战
flutter·华为·harmonyos
程序猿追12 小时前
从零打造一个“跳一跳”:在HarmonyOS模拟器上用Canvas复刻经典
华为·harmonyos
@不误正业12 小时前
第13章-开源鸿蒙是否适合做端侧AI操作系统
人工智能·开源·harmonyos
UnicornDev13 小时前
【HarmonyOS 6】底部悬浮导航的迷你栏适配(API23)
华为·harmonyos·arkts·鸿蒙