鸿蒙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%')
  }
}
相关推荐
w139548564223 小时前
Flutter跨平台组件集成框架鸿蒙化使用指南
flutter·华为·harmonyos
进击的前栈4 小时前
Flutter跨平台开发鸿蒙化HTTP解析工具包使用指南
flutter·http·harmonyos
进击的前栈6 小时前
Flutter跨平台开发鸿蒙化HTTP测试工具包使用指南
flutter·http·harmonyos
花开彼岸天~11 小时前
Flutter跨平台开发鸿蒙化定位服务组件使用指南
flutter·开源·harmonyos
特立独行的猫a11 小时前
移植开源软件Notepad--(NDD)到鸿蒙PC:环境搭建与配置
notepad++·开源软件·harmonyos·鸿蒙pc·notpad--
鸿蒙开发工程师—阿辉12 小时前
HarmonyOS 上下文的使用: 脱离 UI 怎么用 Context?
ui·华为·harmonyos
FrameNotWork13 小时前
HarmonyOS 教学实战(八):后台任务、Worker 与任务调度
华为·harmonyos
¥-oriented14 小时前
【鸿蒙相关活动】
harmonyos
梦想不只是梦与想15 小时前
鸿蒙中 倒计时实现方法
华为·harmonyos·鸿蒙·setinterval·texttimer
sinat_384241091 天前
HarmonyOS音乐播放器开发实战:从零到一打造完整鸿蒙系统音乐播放器应用 2
华为·gitlab·intellij-idea·harmonyos·visual studio·webstorm