app 版本更新
版本更新:
app 版本更新
从服务器获取更新信息: 1.软件版本号 2. 升级信息 3. 软件下载的url
private String version; private String description; private String downloadUrl; private String localUrl;
根据获取的信息比对软件版本号
//从服务器获取更新信息 URL url = new URL(Path.UPDATESERVERURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); int code = conn.getResponseCode(); if (code == 200) { InputStream is = conn.getInputStream(); int len = 0; byte[] buf = new byte[512]; StringBuffer sb = new StringBuffer(); while ((len = is.read(buf)) != -1) { sb.append(new String(buf, 0, len, "utf-8")); } is.close(); conn.disconnect(); String jsonStr = sb.toString(); sb.setLength(0); sb = null; LogUtil.i(jsonStr); Gson gson = new Gson(); mUpdateInfo = gson.fromJson(jsonStr, UpdateInfo.class); //解析json字符串 }
//比对信息
String version = getVersoin(); version = version.replace(".", ""); int versionCode = Integer.parseInt(version); if (mUpdateInfo != null) { if (versionCode < Integer.parseInt(mUpdateInfo.getVersion().replace(".", ""))) { AlertDialog.Builder builder = new Builder(getActivity()); builder.setCancelable(false); builder.setTitle("升级提醒"); builder.setMessage("版本号:" + mUpdateInfo.getVersion() + "\n" + "更新详情:" + mUpdateInfo.getDescription()); builder.setNegativeButton("取消", null); builder.setPositiveButton("更新", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { LogUtil.i("确定更新"); String state = Environment.getExternalStorageState(); LogUtil.i("state: " + state); if (Environment.MEDIA_MOUNTED.equals(state)) { // 下载文件 downloadApk(); } else { MyToast.showMsg(getActivity(), "存储设备异常,更新失败"); } } }); builder.show(); } }
//获取软件版本号
private String getVersoin() { try { PackageInfo pi = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0); return pi.versionName; } catch (NameNotFoundException e) { e.printStackTrace(); LogUtil.i("获取版本号出错"); return null; } }
根据软件版本号对比,根据下载地址下载
protected void downloadApk() { final ProgressDialog pd = new ProgressDialog(getActivity()); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setTitle("下载"); pd.setMessage("正在下载,请稍候..."); pd.show(); pd.setCancelable(false); pd.setOnDismissListener(new OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { if (TextUtils.isEmpty(mUpdateInfo.getLocalUrl())) { MyToast.showMsg(getActivity(), "下载出错了"); } else { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(mUpdateInfo.getLocalUrl())), "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } } }); new Thread() { public void run() { try { String apkUrl = mUpdateInfo.getDownloadUrl(); URL url = new URL(apkUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); int code = conn.getResponseCode(); if (code == 200) { int length = conn.getContentLength(); pd.setMax(length); String fileName = apkUrl.substring(apkUrl.lastIndexOf("/")); File file = new File(Environment.getExternalStorageDirectory(), getActivity().getPackageName() + fileName); if (!file.exists()) { file.getParentFile().mkdir(); } mUpdateInfo.setLocalUrl(file.getAbsolutePath()); FileOutputStream fos = new FileOutputStream(file); InputStream is = conn.getInputStream(); int len = 0; int progress = 0; byte[] buf = new byte[1024]; while ((len = is.read(buf)) != -1) { fos.write(buf, 0, len); progress += len; pd.setProgress(progress); } fos.flush(); fos.close(); is.close(); conn.disconnect(); } } catch (Exception e) { e.printStackTrace(); mUpdateInfo.setLocalUrl(""); } finally { pd.dismiss(); } } }.start(); }
下载完成安装
pd.setOnDismissListener(new OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { if (TextUtils.isEmpty(mUpdateInfo.getLocalUrl())) { MyToast.showMsg(getActivity(), "下载出错了"); } else { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(mUpdateInfo.getLocalUrl())), "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } } });