Android应用升级,检测更新,下载,检验,安装
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android应用升级,检测更新,下载,检验,安装,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5130字,纯文字阅读大概需要8分钟。
内容图文
![Android应用升级,检测更新,下载,检验,安装](/upload/InfoBanner/zyjiaocheng/1140/78260f9c3b924282a979f0f007087fcd.jpg)
应用升级大致步骤:
-
检测是否有更新(读取服务器config文件,比对版本号)
-
若发现高版本则读取更新文件updateinfo.xml获取下载更新相关信息
-
校验信息确认升级后,下载apk
-
下载完apk后,进行MD5检验apk的完整性
-
安装apk
升级入口
private void upgrade() { //需要访问网络,避免主线程堵塞 new Thread(){ public void run() { if(checkUpdate()){//检查更新 handler.sendEmptyMessage(20);//通知界面提示有版本更新 } }; }.start(); } private boolean checkUpdate(){ String url = PATH_SERVER + "upgrade/config"; //从config文件读取Version信息,和UpdateInfo.xml文件地址 try { updateInfoMap = ParseUpdateFile.getConfigInfo(url); } catch (Exception e) { e.printStackTrace(); } //获取当前apk的版本号 PackageInfo packageInfo = null; try { packageInfo = MainActivity.this.getPackageManager().getPackageInfo(MainActivity.this.getPackageName(), 0); } catch (Exception e) { e.printStackTrace(); } int updateVCode = Integer.valueOf(updateInfoMap.get("Version")); //服务器端apk版本高于现在的版本,则读取updateinfo.xml文件 if(updateVCode > packageInfo.versionCode){ url = PATH_SERVER+"upgrade/updateinfo.xml"; try { updateInfoMap.putAll(ParseXmlUtil.parseXml(url)); } catch (Exception e) { e.printStackTrace(); } //输出读取结果 Set<String> set = updateInfoMap.keySet(); System.out.println("map.size():"+updateInfoMap.size()); for (Iterator<String> iterator = set.iterator(); iterator.hasNext();) { String string = (String) iterator.next(); System.out.println(string + "——>" + updateInfoMap.get(string)); } //检查信息合法性,通过则发送可更新消息 return checkUpdateInfo(updateInfoMap); } return false; }
解析config文件
public static Map<String,String> getConfigInfo(String strURL) throws Exception { Map<String,String> configMap = new HashMap<String, String>(); URL url = new URL(strURL); URLConnection conn = url.openConnection(); if (conn == null) { return configMap; } InputStream inputStream = conn.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String str = null; while (null != (str=bufferedReader.readLine())) { if (str != null) { if (str.contains("Version=")) { configMap.put("Version", str.substring(str.indexOf("=")+1)); } if (str.contains("VersionServer")) { configMap.put("VersionServer", str.substring(str.indexOf("::")+2)); } } } bufferedReader.close(); return configMap; }
checkUpdateInfo()主要校验信息的合法性
private boolean checkUpdateInfo(Map<String, String> updateInfoMap){ String downloadPath = updateInfoMap.get("DownloadPath"); String packageName = updateInfoMap.get("packageName"); String versionCode = updateInfoMap.get("versionCode"); String updateVCode = updateInfoMap.get("Version"); if (checkUrl(downloadPath)//检测是否可访问 && versionCode.equals(updateVCode)//config和updateinfoxml文件中版本号是否一致 && packageName.equals(getPackageName())) {//包名 return true; } return false; }
下载文件到设备需要权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
private void downLoadAPK(){ new Thread() { public void run() { String downLoadPath = updateInfoMap.get("DownloadPath"); String downLoadDir = "/acfg/"; File fileDir = new File(downLoadDir); if (!fileDir.exists()) { fileDir.mkdir(); } String fileName = downLoadDir + downLoadPath.substring(downLoadPath.lastIndexOf("/")+1); File file = new File(fileName); if (file.exists()) { file.delete(); } try { file.createNewFile(); // 构造URL URL url = new URL(downLoadPath); // 打开连接 URLConnection con = url.openConnection(); // 获得文件的长度 int contentLength = con.getContentLength(); System.out.println("长度 :" + contentLength); // 输入流 InputStream is = con.getInputStream(); // 1K的数据缓冲 byte[] bs = new byte[1024]; // 读取到的数据长度 int len; // 输出的文件流 OutputStream os = new FileOutputStream(fileName); // 开始读取 while ((len = is.read(bs)) != -1) { os.write(bs, 0, len); } // 完毕,关闭所有链接 os.close(); is.close(); updateInfoMap.put("fileName", fileName); } catch (Exception e) { e.printStackTrace(); handler.sendEmptyMessage(22); } handler.sendEmptyMessage(21);//通知界面下载完成 }; }.start(); }
下载完成后核对apk的MD5值
File file = new File(fileName); String fileMD5 = MD5Util.getMD5OfFile(file); if (fileMD5.equals(activity.updateInfoMap.get("md5sum"))) { Toast.makeText(activity, "Download Finished. It‘s ready to update!", Toast.LENGTH_LONG).show(); activity.update(fileName); }
获取文件的MD5值
public static String getMD5OfFile(File file) { String value = null; FileInputStream in = null; try { in = new FileInputStream(file); MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length()); MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(byteBuffer); BigInteger bi = new BigInteger(1, md5.digest()); value = bi.toString(16).toUpperCase(Locale.ENGLISH); } catch (Exception e) { e.printStackTrace(); } finally { if (null != in) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return value; }
安装下载好的apk
private void update(String filePath) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive"); startActivity(intent); }
安装完成后进入应用
原文:http://huamm.blog.51cto.com/5646020/1551337
内容总结
以上是互联网集市为您收集整理的Android应用升级,检测更新,下载,检验,安装全部内容,希望文章能够帮你解决Android应用升级,检测更新,下载,检验,安装所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。