项目上用到的是本地更新的方法.
主要的思路就是:每次初始化application的时候去请求接口,接口返回数据中有后台最新的版本号和最新app下载的地址,判断本地的版本号和请求的版本号,判断是否需要下载.如果版本有更新,进行弹框提示.
可以与后台商量好返回的字段,返回的字段包括提示框要显示的内容(versionDesc),版本号名称(versionName或者versionCode),app下载路径(setupUrl),和最低包容的版本(minCode).
在app中可以设置一个UpgradeManager管理类对更新进行操作,在MainActivity的initView()方法中请求数据
/** * 检查更新 */ private void checkUpdate() { if (upgradeManager == null) { upgradeManager = new UpgradeManager(MainActivity.this); } upgradeManager.checkUpgrade(true); }接下来的操作就在UpgradeManager中进行.
checkUpgrade(true)方法中,设置好请求的参数(本地的版本号),对返回值进行解析,根据返回的版本号判断是否显示弹窗showUpgradeDialog(boolean showCancel),
显示框是否显示取消按钮可以根据返回的数据想对比.
下载apk方法 downLoadAPK();
详细代码如下
/** * 检查更新 * * @param isFirstTime 是否初次启动,每次初次启动都会检查一次 */ public void checkUpgrade(boolean isFirstTime) { if (TextUtils.isEmpty(mQueryVersionUrl)) { return; } mCommonShared.saveLong(G.APP_CHECK_UPDATE_TIME, System.currentTimeMillis()); OkHttpUtils.get().url(mQueryVersionUrl).build().execute(new StringCallback() { @Override public void onError(Call call, Exception e, int id) { } @Override public void onResponse(String response, int id) { try { JSONObject json = new JSONObject(response); if (json.optInt("state") == 0) { mUpgradeResult = CommonUtils.getGson().fromJson(json.optString("data"), AppCheckUpdateModel.class); if (mUpgradeResult == null) { return; } int curVersionCode = 1; try { curVersionCode = mActivity.getPackageManager().getPackageInfo(mActivity.getPackageName(), 0).versionCode; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } if (curVersionCode < mUpgradeResult.versionCode) { // 提示升级弹窗 showUpgradeDialog(curVersionCode >= mUpgradeResult.minCode); } else { if (mCheckUpdateListener != null) { mCheckUpdateListener.onAppVersionNotUpdate(); } else { //ToastUtil.showToast(mActivity, R.string.no_new_version); } } } } catch (Exception e) { Logger.e(e.getLocalizedMessage()); } } }); }
/** * 弹出提示升级的弹窗 * * @param showCancel 是否显示取消按钮 */ private void showUpgradeDialog(boolean showCancel) { String titleName = mActivity.getString(R.string.update_version_title); String mMsg = mActivity.getString(R.string.update_version_msg); if (mUpgradeResult != null) { String serMsg = mUpgradeResult.versionDesc; if (!TextUtils.isEmpty(serMsg)) { mMsg = serMsg; Logger.e(serMsg); } if (!TextUtils.isEmpty(mUpgradeResult.versionName)) { titleName = titleName + mUpgradeResult.versionName; } } showMessageDialog(titleName, mMsg, showCancel); }
private void showMessageDialog(String title, String msg, boolean isShowCancel) { if (mMessageDialog != null) { return; } mMessageDialog = new AlertDialog.Builder(mActivity).setTitle(title).setMessage(msg).setCancelable(false) .setPositiveButton(ResUtils.getStringIDByName(mActivity, "update"), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { downloadAPK(); } }); if (isShowCancel) { mMessageDialog.setNegativeButton(ResUtils.getStringIDByName(mActivity, "cancel"), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (requestCall != null) { requestCall.cancel(); } //dismissDialog(); } }); } mMessageDialog.show(); }
private void showProgressDialog() { mProgressDialog = new ProgressDialog(mActivity); mProgressDialog.setTitle(R.string.download_loading); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgressDialog.setCancelable(false); mProgressDialog.show(); }
private void downloadAPK() { if (isDownloadLoading) { return; } showProgressDialog(); // if (mUpgradeResult == null) { // //有可能为空:1.检查需要强制升级。2.退出软件再次进入,再次检查升级。3.进入其他界面后返回首页,这时会弹出强制升级的弹窗。如果检查升级数据没有返回,这个值为空。 // mMessageDialog.setMessage(mActivity.getString(R.string.ui_upgrade_waiting)); // return; // } isDownloadLoading = true; String fileUrl = mUpgradeResult.setupUrl; String fileName = mUpgradeResult.versionName + ".apk"; //以版本号命名 String filePath = BasePath.getSDDownloadPath(); //文件下载路径 requestCall = OkHttpUtils.get().url(fileUrl).build(); requestCall.execute(new FileCallBack(filePath, fileName) { @Override public void onError(Call call, Exception e, int id) { isDownloadLoading = false; // if (mMessageDialog != null) { // mMessageDialog.setMessage(e.getLocalizedMessage()); // mMessageDialog.setShowNegativeButton(true); // } if (mProgressDialog != null) { mProgressDialog.dismiss(); } new AlertDialog.Builder(mActivity).setTitle(null).setMessage(R.string.download_fail).show(); } @Override public void inProgress(float progress, long total, int id) { // if (mProgressDialog != null) { mProgressDialog.setProgress((int)(progress * 100)); } } @Override public void onResponse(File response, int id) { isDownloadLoading = false; if (mProgressDialog != null) { mProgressDialog.dismiss(); } openUpgradeFile(response); } }); }
/** * 打开下载完成的apk文件 * * @param apkFile */ private void openUpgradeFile(File apkFile) { if (apkFile != null) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 设置目标应用安装包路径 intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); if (mActivity != null) { mActivity.startActivity(intent); } } }