Android提示版本更新+notification显示下载进度

来源:互联网 发布:代金券数据库设计 编辑:程序博客网 时间:2024/05/18 07:24

首先:参考了http://blog.csdn.net/harvic880925/article/details/25191159这篇文章。然后自己做了一遍,更改下载进度条

服务器端:放置了一个version.xml文件来保存版本号和版本名称等

客户端:点击查看版本信息时,去服务器端取version.xml这个文件,然后使用XmlPullParser解析,判断本地与服务器端的版本是否需要更新。如果需要更新,提供apk下载地址,然后下载就好。

一、从服务器端获取version.xml文件,解析

1、给定地址获取,转换成String类型的格式

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 blocke.printStackTrace();}connection.disconnect();} catch (Exception e1) {// TODO Auto-generated catch blocke1.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;}

二、比较本地版本和version.xml中获取的版本,确定是否需要更新

注:里边涉及到的网络请求需要异步

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;}

三、下载apk+更新进度条

1、下载文件
void downFile(final String downloadUrl) {new Thread() {@Overridepublic 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 blocke.printStackTrace();}connection.disconnect();} catch (Exception e1) {// TODO Auto-generated catch blocke1.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// RomoteViewsromoteView.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);


0 0