我们接着看downloadUpdateFile这个方法:
/*** * 下载文件 * * @return * @throws MalformedURLException */ public long downloadUpdateFile(String down_url, String file) throws Exception { int down_step = 5;// 提示step int totalSize;// 文件总大小 int downloadCount = 0;// 已经下载好的大小 int updateCount = 0;// 已经上传的文件大小 InputStream inputStream; OutputStream outputStream; URL url = new URL(down_url); HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); httpURLConnection.setConnectTimeout(TIMEOUT); httpURLConnection.setReadTimeout(TIMEOUT); // 获取下载文件的size totalSize = httpURLConnection.getContentLength(); if (httpURLConnection.getResponseCode() == 404) { throw new Exception("fail!"); } inputStream = httpURLConnection.getInputStream(); outputStream = new FileOutputStream(file, false);// 文件存在则覆盖掉 byte buffer[] = new byte[1024]; int readsize = 0; while ((readsize = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, readsize); downloadCount += readsize;// 时时获取下载到的大小 /** * 每次增张5% */ if (updateCount == 0 || (downloadCount * 100 / totalSize - down_step) >= updateCount) { updateCount += down_step; // 改变通知栏 // notification.setLatestEventInfo(this, "正在下载...", updateCount // + "%" + "", pendingIntent); contentView.setTextViewText(R.id.notificationPercent, updateCount + "%"); contentView.setProgressBar(R.id.notificationProgress, 100, updateCount, false); // show_view notificationManager.notify(notification_id, notification); } } if (httpURLConnection != null) { httpURLConnection.disconnect(); } inputStream.close(); outputStream.close(); return downloadCount; }注释已经写的很详细,相信大家都看的明白,如果哪里有不足的地方,请留您吉言指出.
这里我用别的app代替了,简单省事,正常的话,你要对你的APP进行数字签名.然后才可以进行升级应用.
示意图: