鸿蒙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%')
  }
}
相关推荐
Industio_触觉智能7 小时前
用鸿蒙开发鸿蒙!DevEco三件套已支持鸿蒙PC|触觉智能鸿蒙南向嵌入式厂家
华为·harmonyos·鸿蒙·鸿蒙系统·开发板·openharmony·开源鸿蒙
威哥爱编程13 小时前
HarmonyOS 7 Agent 开发实战 08:结构化输出解析
agent·harmonyos
威哥爱编程13 小时前
HarmonyOS 7 Agent 开发实战 09:Agent 测试评测集
agent·harmonyos
Eyfcom18 小时前
〖深度〗鸿蒙 ArkTS 移动应用开发经验:从 TypeScript 心智模型到声明式运行时的六层认知重构
typescript·移动开发·harmonyos·arkts
程序猿追18 小时前
把一份背包数据装进 NBT:KNBT 在鸿蒙上的标签与压缩实验
华为·harmonyos
行走的小派21 小时前
Harmony文件管理子系统实践:纯文本编辑器的大文件加载与编码处理
harmonyos·香橙派·应用开发
hqzing1 天前
在鸿蒙 PC 上使用 apt 安装 Debian 软件包(非虚拟机、非容器)
华为·debian·harmonyos
Marco_Marco1351 天前
定位SDK接入实战:Android/iOS/鸿蒙三端怎么选、怎么接
android·ios·harmonyos
梦想不只是梦与想1 天前
鸿蒙应用:区分真机和模拟器
harmonyos·deviceinfo·productmodel