原文地址:http://www.apkbus.com/android-14551-1-1.html
这篇文章是
Android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用。
1.设计思路,使用VersionCode定义为版本升级参数。
android为我们定义版本提供了2个属性:
谷歌建议我们使用versionCode自增来表明版本升级,无论是大的改动还是小的改动,而versionName是显示用户看的软件版本,作为显示使用。所以我们选择了VersionCode作为我们定义版本升级的参数。
2.工程目录
为了对真实项目或者企业运用有实战指导作用,我模拟一个独立的项目,工程目录设置的合理严谨一些,而不是仅仅一个demo。 假设我们以上海地铁为项目,命名为"Subway",工程结构如下,
3.版本初始化和版本号的对比。
首先定义在全局文件Global.java中定义变量localVersion和serverVersion分别存放本地版本号和服务器版本号。
好,我们现在把这些东西串一下: 第一步在SubwayApplication的onCreate()方法中执行initGlobal()初始化版本变量。
现在入口已经打开,在checkVersion方法的第18行代码中看出,当用户点击更新,我们开启更新服务,从服务器上下载最新版本。
4.使用Service在后台从服务器端下载,完成后提示用户下载完成,并关闭服务。
定义一个服务UpdateService.java,首先定义与下载和通知相关的变量:
从代码中可以看出来,updateRunnable类才是真正下载的类,出于用户体验的考虑,这个类是我们单独一个线程后台去执行的。 下载的过程有两个工作:1.从服务器上下载数据;2.通知用户下载的进度。 线程通知,我们先定义一个空的updateHandler。
下载完成后,我们提示用户下载完成,并且可以点击安装,那么我们来补全前面的Handler吧。 先在UpdateService.java定义2个常量来表示下载状态:
至此,文件下载并且在通知栏通知进度。 发现本人废话很多,其实几句话的事情,来来回回写了这么多,啰嗦了,后面博文我会朝着精简方面努力。 PS:前面说要附上cheanUpdateFile()的代码
|