Created
January 12, 2017 15:26
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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