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
- Android提示版本更新+notification显示下载进度
- Android中使用Notification实现应用更新显示下载进度
- android之apk自动更新采用Notification通知提示并显示下载进度
- android之apk自动更新采用Notification通知提示并显示下载进度
- Android实现Service下载文件,Notification显示下载进度
- Notification实现下载进度显示!
- Notification实现下载进度显示!
- Notification实现下载进度显示!
- Android版本提示更新下载
- Android提示版本更新(如何计算下载进度核心思路)
- Android使用ProgressBar、自定义Notification显示文件下载进度
- android中DownloadManager实现版本更新,监听下载进度实例
- Android版本更新下载提示对话框
- Android多任务下载,使用Notification显示、更新进度条
- android实现下载图片在(Notification)通知栏上显示进度。
- Android开发 下载显示进度
- Android开发 下载显示进度
- Android okhttputils显示下载进度
- AS3.0的元素处理(二)-- TextField
- Android中解决ScrollView下拉阴影的问题(ListView也应该可以的,没有亲测)
- 【任务学习之二】
- python mongodb scrapy例子
- Android系统Recovery工作原理之使用update.zip升级过程分析(九)---updater-script脚本语法简介以及执行流程
- Android提示版本更新+notification显示下载进度
- 面试心得与总结---BAT、网易、蘑菇街
- poj 3687 Labeling Balls
- Cocoa编码规范
- POJ1562
- AS3.0的元素处理(三)-- TextFormat
- Linux内核源码分析之文件系统(1) -- 三思而后行
- Visual C++ Tips: 创建 x64 Target
- Android 实现沉浸式状态栏