Android versionCode programmatically

Is there any way to know dynamically and programmatically the versionCode of an Android application??

I don´t know... maybe something like

 getApplicationContext.getVersionCode();

Thank you very much.


如果您使用gradle(AndroidStudio中的默认设置),您可以:

BuildConfig.VERSION_CODE

You find the following from using this code

 public int getVersionCode() {
    int v = 0;
    try {
        v = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
    } catch (NameNotFoundException e) {

    }
    return v;
}

Class Required

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

这应该被try ... catch所包围。

PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
version = pInfo.versionName;
versionCode = pInfo.versionCode;
链接地址: http://www.djcxy.com/p/90764.html

上一篇: 如何获得另一个应用程序的版本名称

下一篇: 以编程方式Android版本码