开发者

How can I obtain and show the version number of an android apk?

In Xcode I do this:

NSString *temp;
temp = [@"© 2011 Robert Schoenburg "stringByAppendingString:@"Version "]开发者_开发技巧;
temp = [temp stringByAppendingString:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]];
myLabel.text = temp;

How can I do the same thing in an android app?


Use the getPackageInfo method from PackageManager (which can be obtained via getPackageManager method of any Context). In the resulting PackageInfo, there are versionCode and versionName fields to read from. They correspond to values set in the AndroidManifest.xml in <manifest> tag.

EDIT: Added example for obtaining version code/name of own .apk:

PackageManager pm = context.getPackageManager();
try {
    PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
    Log.d("Version " + pi.versionName + "(" + pi.versionCode + ")");
} catch (NameNotFoundException e) {
    // impossible here as we refer to our own package
}


PackageManager m = getPackageManager();
PackageInfo info = m.getPackageInfo("com.mypackage.name", 0);
Log.i("Version Code",info.versionCode+"");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜