1 package com.example.grenaderose.redthunder.utils; 2 3 import android.content.Context; 4 import android.content.pm.PackageInfo; 5 import android.content.pm.PackageManager; 6 7 /** 8 * Description : 9 * Author : hanbao 10 * Date : 2019/3/9 0009 下午 4:30 11 */ 12 public class PackUtils { 13 14 //获取版本名称 15 public static String getVersionName(Context ctx) { 16 //包管理器 17 PackageManager pm = ctx.getPackageManager(); 18 19 //获取包的信息(app的信息) 20 try { 21 PackageInfo packageInfo = pm.getPackageInfo(ctx.getPackageName(), 0); 22 23 String versionName = packageInfo.versionName;//版本名称 24 int versionCode = packageInfo.versionCode;//版本号 25 26 System.out.println("versionName:" + versionName); 27 System.out.println("versionCode:" + versionCode); 28 29 return versionName; 30 31 } catch (PackageManager.NameNotFoundException e) { 32 //未找到包名的异常 33 e.printStackTrace(); 34 } 35 36 return ""; 37 } 38 39 //获取版本号 40 public static int getVersionCode(Context ctx) { 41 //包管理器 42 PackageManager pm = ctx.getPackageManager(); 43 44 //获取包的信息(app的信息) 45 try { 46 PackageInfo packageInfo = pm.getPackageInfo(ctx.getPackageName(), 0); 47 int versionCode = packageInfo.versionCode;//版本号 48 49 return versionCode; 50 } catch (PackageManager.NameNotFoundException e) { 51 //未找到包名的异常 52 e.printStackTrace(); 53 } 54 55 return -1; 56 } 57 }