Skip to content

Instantly share code, notes, and snippets.

@crowjdh
Last active August 29, 2015 14:13
Show Gist options
  • Save crowjdh/8ffc802fcf4c6245deac to your computer and use it in GitHub Desktop.
Save crowjdh/8ffc802fcf4c6245deac to your computer and use it in GitHub Desktop.
Way to get app's version information
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
/**
* Created by Dongheyon Jeong on 15. 1. 20.
*
* AppInfo
* 앱의 일반적인 정보를 제공하는 클래스
*/
public class AppInfo {
public static final int INVALID_VERSION_CODE = -1;
public static int getVersionCode(Context context) {
PackageInfo pInfo;
try {
if (context.getPackageManager() != null) {
pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
return pInfo.versionCode;
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return INVALID_VERSION_CODE;
}
public static String getVersionName(Context context) {
PackageInfo pInfo;
try {
if (context.getPackageManager() != null) {
pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
return pInfo.versionName;
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment