Created
November 3, 2012 22:05
-
-
Save kristopherjohnson/4009032 to your computer and use it in GitHub Desktop.
Get Android app version name and version code
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
public class AppInfo { | |
/** | |
* Get application version string | |
* | |
* @param context | |
* Interface to global information about an application | |
* environment | |
* @return String of the form "VV-CC", where VV is the version name and CC | |
* is the version code (e.g., "1.0.3-25") | |
*/ | |
public static String getAppVersionNameAndVersionCode(Context context) { | |
try { | |
PackageManager pm = context.getPackageManager(); | |
String packageName = context.getPackageName(); | |
PackageInfo info = pm.getPackageInfo(packageName, 0); | |
String version = info.versionName + "-" + info.versionCode; | |
return version; | |
} | |
catch (Exception ex) { | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment