首先:参考了http://blog.csdn.net/harvic880925/article/details/25191159这篇文章。然后自己做了一遍,更改下载进度条
服务器端:放置了一个version.xml文件来保存版本号和版本名称等
客户端:点击查看版本信息时,去服务器端取version.xml这个文件,然后使用XmlPullParser解析,判断本地与服务器端的版本是否需要更新。如果需要更新,提供apk下载地址,然后下载就好。
public static String getInputStreamFromServer(String urlSpec) { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { URL url = new URL(urlSpec); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); try { if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { return null; } InputStream is = connection.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); byte[] buffer = new byte[1024]; int len = -1; while ((len = bis.read(buffer)) != -1) { out.write(buffer, 0, len); } out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } connection.disconnect(); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return new String(out.toByteArray()); }2、将以上文件使用XmlPullParser解析,其中UpdateInfo是本地用于存储xml转换后的数据结构
public static UpdateInfo getUpdateInfo(String urlString) throws Exception { XmlPullParser parser = Xml.newPullParser(); parser.setInput(new StringReader(urlString)); int eventType = parser.getEventType(); UpdateInfo info = new UpdateInfo(); while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG) { if ("verCode".equals(parser.getName())) { info.setVerCode(Integer.parseInt(parser.nextText())); } else if ("verName".equals(parser.getName())) { info.setVerName(parser.nextText()); } else if ("description".equals(parser.getName())) { info.setDescription(parser.nextText()); } else if ("apkUrl".equals(parser.getName())) { info.setApkUrl(parser.nextText()); } } eventType = parser.next(); } return info; }
注:里边涉及到的网络请求需要异步
UpdateInfo info = UpdateInfoParser.getUpdateInfo(Common .getInputStreamFromServer(Common.SERVER_IP_VERSIONXML)); m_verName = info.getVerName(); m_verCode = info.getVerCode(); if (info.getVerCode() > vercode) { return true; } else { return false; }
void downFile(final String downloadUrl) { new Thread() { @Override public void run() { try { URL url = new URL(downloadUrl); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); try { if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { return; } long length = connection.getContentLength(); InputStream is = connection.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); File file = new File(Environment .getExternalStorageDirectory() .getAbsoluteFile() + File.separator + "test.apk"); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(file)); byte[] buffer = new byte[1024]; int len = -1; int count = 0; // 记录进度 while ((len = bis.read(buffer)) != -1) { bos.write(buffer, 0, len); count += len; progress = (int) (((double) count / length) * 100); if (length > 0 && progress % 5 == 0) { Message msg = progressHandler.obtainMessage( MESSAGE_UPDATEPROGRESS, progress); msg.arg1 = progress; msg.sendToTarget(); } } bos.flush(); bos.close(); down(); // 告诉HANDER已经下载完成了,可以安装了 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } connection.disconnect(); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }; }.start(); }2、更新进度条(注意:每次更新进度条RemoteViews需要新new)
Handler progressHandler = new Handler() { public void handleMessage(Message msg) { if (msg.what == MESSAGE_UPDATEPROGRESS) { romoteView = new RemoteViews(getPackageName(), R.layout.noti_progressbar);// 每次更新notification,必须重新new // RomoteViews romoteView.setProgressBar(R.id.pb_notification, 100, msg.arg1, false); romoteView.setTextViewText(R.id.tv_notification, "已下载" + msg.arg1 + "%"); notification.contentView = romoteView; nm.notify(0, notification); } super.handleMessage(msg); }; };
Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() .getAbsoluteFile() + File.separator + "test.apk")), "application/vnd.android.package-archive"); startActivity(intent);