判断APP当前版本号与提供的apk对比是否需要升级

来源:互联网 发布:mac google搜索打不开 编辑:程序博客网 时间:2024/06/05 23:46

通过PackageInfo 判断apk的版本与当前已安装的版本比较确定是否需要升级,避免web上是老版本apk,填写的版本号却大于当前apk版本导致升级失败

private static boolean packageContentIsCorrect(String filename,Context context) {        PackageManager mPm = context.getPackageManager();        PackageInfo pi = mPm.getPackageArchiveInfo(filename,                PackageManager.GET_RECEIVERS);        if (pi == null) {            System.out.println("Package could not be parsed successfully.");            return false;        }        if (!pi.packageName.equals("com.test")) {            System.out.println("Package name in apk (" + pi.packageName                    + ") does not match package name specified by programmer ("                    + "com.test" + ").");            return false;        }        //判断是否需要安装新版本        try {            int installVersionCode = pi.versionCode;            int versionCode = mPm.getPackageInfo(context.getPackageName(), 0).versionCode;            System.out.println("pi-installVersionCode----------------"+installVersionCode);            System.out.println("pi--versionCode---------------"+pi.versionCode);            if(installVersionCode <= versionCode){                System.out.println("Package not need update.");                return false;            }        } catch (Exception e) {            e.printStackTrace();        }        return true;    }
阅读全文
0 0
原创粉丝点击