Obtain application's version code, android:versionCode="1" - Android App

Android examples for App:App Information

Description

Obtain application's version code, android:versionCode="1"

Demo Code

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

public class Main {
  /**//from w w  w.j  a v a2  s .c om
   * Obtain application's version code,<br>
   * android:versionCode="1"
   * 
   * @param context
   * @return
   */
  public static final int obtainVersionCode(Context context) {
    try {
      PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
      return pi.versionCode;
    } catch (NameNotFoundException e) {

    }
    return 0;
  }
}

Related Tutorials