Android How to - Get App Version Name








Question

We would like to know how to get App Version Name.

Answer

/*from  w w w . j av a  2 s  .  co m*/
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

public class Main {
  public static String getAppVersionName(Context context) {
    String versionName = "";
    try {
      PackageManager pm = context.getPackageManager();
      PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
      versionName = pi.versionName;
      if (versionName == null || versionName.length() <= 0) {
        return "";
      }
    }catch (Exception e) {
    }
    return versionName;
  }
}