代码获取Android程序的versionCode和versionName

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

public String getVersionCode(Context context){  

            PackageManager packageManager=context.getPackageManager();  

            PackageInfo packageInfo;  

            String versionCode="";  

            try {  

                         packageInfo=packageManager.getPackageInfo(context.getPackageName(),0);  

                         versionCode=packageInfo.versionCode+"";  

                } catch (PackageManager.NameNotFoundException e) {  

                 e.printStackTrace();  

                }  

            return versionCode;  

}  

  

 

public String getVersionName(Context context){  

                  PackageManager packageManager=context.getPackageManager();  

                  PackageInfo packageInfo;  

                 String versionName="";  

                try {  

                            packageInfo=packageManager.getPackageInfo(context.getPackageName(),0);  

                            versionName=packageInfo.versionName;  

                     } catch (PackageManager.NameNotFoundException e) {  

                            e.printStackTrace();  

                    }  

                     return versionName;  

}  

转载于:https://my.oschina.net/andyzzh1314/blog/1154246

你可能感兴趣的:(代码获取Android程序的versionCode和versionName)