get Current Version Name from Context - Android android.content

Android examples for android.content:Context

Description

get Current Version Name from Context

Demo Code

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

public class Main {

  public static String getCurrentVersionName(Context context) {
    String currentVersionName = "";

    try {//from www  .j  a va  2  s  .  c o  m
      PackageManager pm = context.getPackageManager();
      PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
      currentVersionName = pi.versionName;
      if (currentVersionName == null || currentVersionName.length() <= 0) {
        currentVersionName = "1.0.0";
      }
    } catch (Exception e) {
      currentVersionName = "1.0.0";
    }

    return currentVersionName;
  }

}

Related Tutorials