How can I read the app version defined in the manifest file programmatically?
In the Android manifest file, there is a field that s开发者_JAVA技巧pecifies the application version.
How I can read that value programmatically?
You can get access to that information through the PackageInfo
class:
PackageInfo pinfo = this.getPackageManager().getPackageInfo(getPackageName(), 0);
Log.d("pinfoCode",pinfo.versionCode);
Log.d("pinfoName",pinfo.versionName);
Call GetApplicationContext().PackageManager().getPackageInfo()
. The PackageInfo
object it returns will have what you need.
try {
final String packageName = context.getPackageName();
PackageInfo packageInfo = context.getApplicationContext().getPackageManager().getPackageInfo(packageName, 0);
final int iconid = packageInfo.applicationInfo.icon; // for example
// etc
} catch (android.content.pm.PackageManager.NameNotFoundException e) {}
精彩评论