Android应用客户端通常会需要更新,而且根据需求分为普通更新和强制更新。是否强制更新可通过检查更新时从服务器获取的标志位来判断。

public class UpdateManager {
private Context mContext; //上下文 private String apkUrl =
"http://183.232.83.10/dd.myapp.com/16891/" +
"C4F9FF39FAE9184222DC278DC7309611.apk?mkey" +
"=5541c92b6a9cca8d&f=d388&fsname=com.yangmbin" +
".beauty_1.1_2.apk&asr=8eff&p=.apk"; //apk下载地址
private static final String savePath = "/sdcard/updateAPK/"; //apk保存到SD卡的路径
private static final String saveFileName = savePath + "apkName.apk"; //完整路径名 private ProgressBar mProgress; //下载进度条控件
private static final int DOWNLOADING = 1; //表示正在下载
private static final int DOWNLOADED = 2; //下载完毕
private static final int DOWNLOAD_FAILED = 3; //下载失败
private int progress; //下载进度
private boolean cancelFlag = false; //取消下载标志位 private double serverVersion = 2.0; //从服务器获取的版本号
private double clientVersion = 1.0; //客户端当前的版本号
private String updateDescription = "更新描述"; //更新内容描述信息
private boolean forceUpdate = true; //是否强制更新 private AlertDialog alertDialog1, alertDialog2; //表示提示对话框、进度条对话框 /** 构造函数 */
public UpdateManager(Context context) {
this.mContext = context;
} /** 显示更新对话框 */
public void showNoticeDialog() {
//如果版本最新,则不需要更新
if (serverVersion <= clientVersion)
return;
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle("发现新版本 :" + serverVersion);
dialog.setMessage(updateDescription);
dialog.setPositiveButton("现在更新", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
arg0.dismiss();
showDownloadDialog();
}
});
//是否强制更新
if (forceUpdate == false) {
dialog.setNegativeButton("待会更新", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
arg0.dismiss();
}
});
}
alertDialog1 = dialog.create();
alertDialog1.setCancelable(false);
alertDialog1.show();
} /** 显示进度条对话框 */
public void showDownloadDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle("正在更新");
final LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.softupdate_progress, null);
mProgress = (ProgressBar) v.findViewById(R.id.update_progress);
dialog.setView(v);
//如果是强制更新,则不显示取消按钮
if (forceUpdate == false) {
dialog.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
arg0.dismiss();
cancelFlag = false;
}
});
}
alertDialog2 = dialog.create();
alertDialog2.setCancelable(false);
alertDialog2.show(); //下载apk
downloadAPK();
} /** 下载apk的线程 */
public void downloadAPK() {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
URL url = new URL(apkUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect(); int length = conn.getContentLength();
InputStream is = conn.getInputStream(); File file = new File(savePath);
if(!file.exists()){
file.mkdir();
}
String apkFile = saveFileName;
File ApkFile = new File(apkFile);
FileOutputStream fos = new FileOutputStream(ApkFile); int count = 0;
byte buf[] = new byte[1024]; do{
int numread = is.read(buf);
count += numread;
progress = (int)(((float)count / length) * 100);
//更新进度
mHandler.sendEmptyMessage(DOWNLOADING);
if(numread <= 0){
//下载完成通知安装
mHandler.sendEmptyMessage(DOWNLOADED);
break;
}
fos.write(buf, 0, numread);
}while(!cancelFlag); //点击取消就停止下载. fos.close();
is.close();
} catch(Exception e) {
mHandler.sendEmptyMessage(DOWNLOAD_FAILED);
e.printStackTrace();
}
}
}).start();
} /** 更新UI的handler */
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case DOWNLOADING:
mProgress.setProgress(progress);
break;
case DOWNLOADED:
if (alertDialog2 != null)
alertDialog2.dismiss();
installAPK();
break;
case DOWNLOAD_FAILED:
Toast.makeText(mContext, "网络断开,请稍候再试", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}; /** 下载完成后自动安装apk */
public void installAPK() {
File apkFile = new File(saveFileName);
if (!apkFile.exists()) {
return;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.parse("file://" + apkFile.toString()), "application/vnd.android.package-archive");
mContext.startActivity(intent);
}
}
对话框中进度条用到的布局:softupdate_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/update_progress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
style="?android:attr/progressBarStyleHorizontal" />
</RelativeLayout>

使用方法:

//这里来检测版本是否需要更新
mUpdateManager = new UpdateManager(this);
mUpdateManager.showNoticeDialog();

检查更新通常是在初始化的时候,请求服务器检查版本,如果有更新,则把相关url、是否强制更新和版本号等信息通过showNoticeDialog的参数传递进去,做相关的初始化即可。

Android检查更新(是否强制更新)的更多相关文章

  1. Android 5.X新特性之为RecyclerView添加下拉刷新和上拉加载及SwipeRefreshLayout实现原理

    RecyclerView已经写过两篇文章了,分别是Android 5.X新特性之RecyclerView基本解析及无限复用 和 Android 5.X新特性之为RecyclerView添加Header ...

  2. Android 5.X新特性之为RecyclerView添加HeaderView和FooterView

    上一节我们讲到了 Android 5.X新特性之RecyclerView基本解析及无限复用 相信大家也应该熟悉了RecyclerView的基本使用,这一节我们来学习下,为RecyclerView添加H ...

  3. 【Android】一种提高Android应用进程存活率新方法

    [Android]一种提高Android应用进程存活率新方法 SkySeraph Jun. 19st 2016 Email:skyseraph00@163.com 更多精彩请直接访问SkySeraph ...

  4. Android下添加新的自定义键值和按键处理流程

            Android下添加新的自定义键值和按键处理流程     说出来不怕大家笑话,我写这篇博客的原因在于前几天去一个小公司面试Android系统工程师,然后在面试的时候对方的技术总监问了我 ...

  5. Android系统在新进程中启动自定义服务过程(startService)的原理分析

    在编写Android应用程序时,我们一般将一些计算型的逻辑放在一个独立的进程来处理,这样主进程仍然可以流畅地响应界面事件,提高用户体验.Android系统为我们提供了一个Service类,我们可以实现 ...

  6. 《Android群英传》读书笔记 (5) 第十一章 搭建云端服务器 + 第十二章 Android 5.X新特性详解 + 第十三章 Android实例提高

    第十一章 搭建云端服务器 该章主要介绍了移动后端服务的概念以及Bmob的使用,比较简单,所以略过不总结. 第十三章 Android实例提高 该章主要介绍了拼图游戏和2048的小项目实例,主要是代码,所 ...

  7. Android群英传》读书笔记 (1) 第一章 Android体系与系统架构 + 第二章 Android开发工具新接触

    第一章 Android体系与系统架构 1.Dalvik 和 ARTDalvik好比是一辆可折叠的自行车,平时是折叠的,只有骑的时候,才需要组装起来用.ART好比是一辆组装好了的自行车,装好就可以骑了. ...

  8. Android 6.0 新特性 整理 资料来自网络

    Android 6.0新特性 Runtime Permissions Doze and App Standby Apache HTTP Client Removal BoringSSL Access ...

  9. android 5.0新特性

    Android Lollipop 面向开发人员的主要功能 Material Design 设计 注重性能 通知 以大屏幕呈现 以文档为中心 连接性能再上一级 高性能图形 音频处理功能更强 摄像头和视频 ...

随机推荐

  1. 转 [Error]EOL while scanning string literal

    https://blog.csdn.net/orangleliu/article/details/38943749 项目中有个定时任务,每天取到一些表数据传到一个外部接口,但是最近总是有异常,今天查了 ...

  2. robotframework 常用快捷键

    重命名——>F2 执行用例——>F8 创建新工程——>ctrl+n 创建新测试套——>ctrl+shift+f 创建新用例——>ctrl+shift+t 创建新关键字—— ...

  3. app的deviceName,apppackage,appactivity获取

    deviceName值的获取: deviceName=192.168.137.150:5555 ip:手机ip地址,端口,通过如下命令开启 # adb devices //查看当前连接设备 # adb ...

  4. requirej入门(二)

    requirejs中的一些基本知识,包括API使用方式等. 基本API require会定义三个变量:define,require,requirejs,其中require === requirejs, ...

  5. 使用InstallShield打包VS程序

    使用InstallShield打包VS程序 InstallShield是微软自己的一个打包工具,这个打包工具,有其优势也有其弊端.其优势,可以很好且方便地将.NET平台的控件以及程序所需要的dll打包 ...

  6. Razor,aspx的占位控件(母版页)

    1.razor: 母版页中:@*类似master的占位控件*@ @RenderSection("scripts", required: false) 部分页中: @section ...

  7. jeecg3.8在子表页面中使用WebUploader组件

    bcAbout-update.jsp改动如下: 因为默认子表的上传组件不能回显,所以改造成WebUploader 1.在更新页面注销掉生成代码 <%--<script type=" ...

  8. 【踩坑】报错 non-static method xxx() cannot be referenced from a static context

    今天测试代码时遇到 Error:(6, 55) java: non-static method sayGoodbye() cannot be referenced from a static cont ...

  9. linux 查看防火墙状态

    1.查看防火墙状态 systemctl status firewalld firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running) 2 ...

  10. Jquery插件之ajaxForm简介

    我们平常在使用jQuery异步提交表单的时候,一般都是加载在submit事件中,如下所示: $(document).ready(function(){ $('#myForm').submit(func ...