text: package com.example.administrator.cookman.utils;
  
  import android.content.Context;
  import android.content.pm.PackageInfo;
  import android.content.pm.PackageManager;
  
  import com.example.administrator.cookman.R;
  
  /**
   * Created by Administrator on 2017/3/29.
   */
  
  public class VersionUtil {
  
      /**
       * 获取版本号
       *
       * @return 当前应用的版本号
       */
  
      public static String getVersion(Context context) {
          try {
              PackageManager manager = context.getPackageManager();
              PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
              return info.versionName;
          } catch (Exception e) {
              e.printStackTrace();
              return context.getString(R.string.can_not_find_version_name);
          }
      }
  
      /**
       * @return 版本号
       */
      public static int getVersionCode(Context context) {
          try {
              PackageManager manager = context.getPackageManager();
              PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
              return info.versionCode;
          } catch (Exception e) {
              e.printStackTrace();
              return 0;
          }
      }
  
  }
  