"Get current app version number from AndroidManifest.xml"

Sun 20 February 2011

  //Usage from activity:

  //String verison = GlobalSettings.getVersionName(this,MyActivity.class)

  public static String getVersionName(Context context, Class cls) {

    try {

      ComponentName comp = new ComponentName(context, cls);

      PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0);

      return "Version: " + pinfo.versionName;

    } catch (android.content.pm.PackageManager.NameNotFoundException e) {

      return null;

    }

  }