代码改变世界

Android版本强制更新

package com.lianpos.util;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast; import com.lianpos.activity.R; import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; /**
* Created by Administrator on 2018/1/2 0002.
*/ public class UpdateManager {
private Context mContext; //上下文 private String apkUrl = ResultUtils.versionUrlValue; //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(double serverVersion,double clientVersion) {
//如果版本最新,则不需要更新
if (serverVersion <= clientVersion)
return;
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle("发现新版本 :" + serverVersion);
dialog.setMessage(updateDescription);
dialog.setPositiveButton("现在更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();
showDownloadDialog();
}
});
//是否强制更新
if (forceUpdate == false) {
dialog.setNegativeButton("待会更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
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 DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
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() {
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) {
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);
}
}

  使用方法:

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

  

Android强制更新的更多相关文章

  1. [转]Android SDK更新 Connection to http://dl-ssl.google.com refused 解决方法

    问题描述 使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository ...

  2. Android SDK更新 Connection to http://dl-ssl.google.com refused 解决方法

    问题描述 使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository ...

  3. android最新更新方法

    使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository-6.xm ...

  4. Failed to fetch URl https://dl-ssl.google.com/android/repository/addo Android SDK更新以及ADT更新出现问题的解决办法

    问题描述 使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository ...

  5. Android SDK更新以及ADT更新出现问题的解决办法(转载)

    转自:http://zyueqi.iteye.com/blog/1474323 问题描述 使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl. ...

  6. 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新

    本系列将从以下三个方面对Tinker进行源码解析: Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Android热更新开源项目Tinker源码解析系列之二:资源文件热更新 A ...

  7. 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新

    [原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...

  8. 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新

    上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...

  9. fir.im Weekly - iOS / Android 动态化更新方案盘点

    动态化更新是 App 开发必然面对的问题.在 iOS 环境下,Apple 开发者们像是" 带着手铐脚镣跳舞" ,相比之下 Android 开发者会轻松一点,有很多相关的开源框架帮助 ...

随机推荐

  1. [nmon]使用nmon工具监控系统资源

    1.下载nmon 下载正确的nmon版本, 查看linux服务器版本,命令:lsb_release -a,查看到当前系统为RedHat 6.4 然后我们根据我们的linux版本,下载相应nmon版本, ...

  2. 简单案列完美搞定Mvc设计模式

    一个小列子搞定Mvc模式,包括数据库以及如何提高用户体验度 1.首先来web.xml配置servlet的访问路径: <?xml version="1.0" encoding= ...

  3. LeetCode Min Stack 最小值栈

    题意:实现栈的四个基本功能.要求:在get最小元素值时,复杂度O(1). 思路:链表直接实现.最快竟然还要61ms,醉了. class MinStack { public: MinStack(){ h ...

  4. LibreOJ #514. 「LibreOJ β Round #2」模拟只会猜题意

    内存限制:256 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 题目描述 给定一个长度为 nnn 的序列 AAA . 定义 f(l,r)=∑i=lrAif(l,r ...

  5. python基础教程总结3—字典

    1.字典 1.1 字典类型与序列类型的区别: 存取和访问数据的方式不同. 序列类型只用数字类型的键(从序列的开始按数值顺序索引): 映射类型可以用其他对象类型作键(如:数字.字符串.元祖,一般用字符串 ...

  6. 2018.4.18 Ubuntu 的telnet命令详解

    Ubuntu 的telnet命令详解 1.作用用途 Telnet 命令通常用来远程登录,Telnet 程序是基于 Telnet 协议的远程登录客户端程序.Telnet 协议是TCP/IP协议族中的一员 ...

  7. SVN:The working copy is locked due to a previous error (一)

    使用 Cornerstone  时,碰到如题问题,SVN无法Update.Commit等操作. 解决办法:Working Copies ⟹ '右键' ⟹ Clean 即可解决! 尊重作者劳动成果,转载 ...

  8. unbuntu14下Qt4.8 和MySQL连接问题 QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE

    使用 QSqlDatabase::addDatabase创建数据库时 会报错: QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: availab ...

  9. 如何用纯 CSS 创作一个方块旋转动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/gjgyWm 可交互视频 ...

  10. cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

    参考  http://blog.csdn.net/mazicwong/article/details/54946952 1.到https://curl.haxx.se/ca/cacert.pem复制下 ...