开发者

Detect my app's own android:versionCode at run time

开发者 https://www.devze.com 2023-01-18 19:51 出处:网络
Is there a way for my app to know the andro开发者_StackOverflow中文版id:versionCode from AndroidManifest.xml or do I have to create a separate constant in one of my classes?I put this in my subclassed

Is there a way for my app to know the andro开发者_StackOverflow中文版id:versionCode from AndroidManifest.xml or do I have to create a separate constant in one of my classes?


I put this in my subclassed android.app.Application but you can use it anywhere you have a context. Just change getPackageManager() to context.getPackageManager().

public int getVersion() {
    int v = 0;
    try {
        v = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
    } catch (NameNotFoundException e) {
        // Huh? Really?
    }
    return v;
}


Yes, you can access this information via PackageManager.getPackageInfo

Also, see details about versioning in the documentation


You can get version code simply from <package_name>.BuildConfig#VERSION_CODE reference. It is generated by ADT plugin/Intellij.

0

精彩评论

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