Skip to content

Instantly share code, notes, and snippets.

@vuhung3990
Last active February 13, 2017 08:23
Show Gist options
  • Save vuhung3990/b1d1adcaba831cdf7bbc to your computer and use it in GitHub Desktop.
Save vuhung3990/b1d1adcaba831cdf7bbc to your computer and use it in GitHub Desktop.
Get app info from the Manifest file
/**
* Get the "android:versionName" value from the Manifest file.
*
* @param context The current Context or Activity that this method is called from
* @return the application version string, or "Unknown" if versionName cannot be found for
* the given context.
*/
public static String getVersionName(Context context) {
String versionName;
try {
versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
versionName = "Unknown";
}
return versionName;
}
/**
* Get the "android:versionCode" value from the Manifest file.
*
* @param context The current Context or Activity that this method is called from
* @return the application version code, or -999 if versionName cannot be found for the given context.
*/
public static int getVersionCode(Context context) {
int versionCode;
try {
versionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
versionCode = -999;
}
return versionCode;
}
/**
* Get application name from Manifest file.
*
* @param context The current Context or Activity that this method is called from
* @return application name.
*/
public static String getApplicationName(Context context) {
int stringId = context.getApplicationInfo().labelRes;
return context.getString(stringId);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment