美文网首页Android开发经验谈安卓开发
安卓获取当前app版本号(versionCode)和版本名称(v

安卓获取当前app版本号(versionCode)和版本名称(v

作者: 蓝不蓝编程 | 来源:发表于2018-12-10 11:33 被阅读82次
     /**
         * 获取当前app version code
         */
        public static long getAppVersionCode(Context context) {
            long appVersionCode = 0;
            try {
                PackageInfo packageInfo = context.getApplicationContext()
                        .getPackageManager()
                        .getPackageInfo(context.getPackageName(), 0);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    appVersionCode = packageInfo.getLongVersionCode();
                } else {
                    appVersionCode = packageInfo.versionCode;
                }
            } catch (PackageManager.NameNotFoundException e) {
                Log.e("", e.getMessage());
            }
            return appVersionCode;
        }
    
        /**
         * 获取当前app version name
         */
        public static String getAppVersionName(Context context) {
            String appVersionName = "";
            try {
                PackageInfo packageInfo = context.getApplicationContext()
                        .getPackageManager()
                        .getPackageInfo(context.getPackageName(), 0);
                appVersionName = packageInfo.versionName;
            } catch (PackageManager.NameNotFoundException e) {
                Log.e("", e.getMessage());
            }
            return appVersionName;
        }
        ```
    
    安卓开发技术分享: https://www.jianshu.com/p/442339952f26
    ![](https://img.haomeiwen.com/i6169789/c56af117f2c1d072.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    

    相关文章

      网友评论

        本文标题:安卓获取当前app版本号(versionCode)和版本名称(v

        本文链接:https://www.haomeiwen.com/subject/nkdrhqtx.html