Skip to content

Instantly share code, notes, and snippets.

@francisnnumbi
Created January 12, 2017 15:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francisnnumbi/9e5983a078f325f2287b5fbdf25996cc to your computer and use it in GitHub Desktop.
Save francisnnumbi/9e5983a078f325f2287b5fbdf25996cc to your computer and use it in GitHub Desktop.
Android helper class. This is the way to get the version name and the version code of your application.
package fnn.smirl.rdc.constitution.info;
import android.content.Context;
import android.content.pm.PackageInfo;
/** by Francis Nduba Numbi */
public final class AppInfo
{
private static String vn= ""; // version name
private static int vc = 0; // version code
private static final AppInfo INSTANCE = new AppInfo();
private AppInfo(){} // private not instantiable
/** eg. AppInfo.getInstance(getApplicationContext()); */
public static AppInfo getInstance(Context ctx){
PackageInfo p = getPackageInfo(ctx);
INSTANCE.vn = p.versionName;
INSTANCE.vc = p.versionCode;
return INSTANCE;
}
@Override
public String toString(){return vn + vc;}
public static String getAppVersionName(){return vn;}
public static int getAppVersionCode(){return vc;}
private static PackageInfo getPackageInfo(Context ctx){
PackageInfo p = null;
try{
p = ctx.getPackageManager()
.getPackageInfo(ctx.getPackageName(), 0);
}catch (Exception e){}
return p;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment