Android强制更新
代码改变世界
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强制更新的更多相关文章
- [转]Android SDK更新 Connection to http://dl-ssl.google.com refused 解决方法
问题描述 使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository ...
- Android SDK更新 Connection to http://dl-ssl.google.com refused 解决方法
问题描述 使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository ...
- android最新更新方法
使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository-6.xm ...
- 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 ...
- Android SDK更新以及ADT更新出现问题的解决办法(转载)
转自:http://zyueqi.iteye.com/blog/1474323 问题描述 使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl. ...
- 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新
本系列将从以下三个方面对Tinker进行源码解析: Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Android热更新开源项目Tinker源码解析系列之二:资源文件热更新 A ...
- 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新
[原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...
- 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新
上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...
- fir.im Weekly - iOS / Android 动态化更新方案盘点
动态化更新是 App 开发必然面对的问题.在 iOS 环境下,Apple 开发者们像是" 带着手铐脚镣跳舞" ,相比之下 Android 开发者会轻松一点,有很多相关的开源框架帮助 ...
随机推荐
- POJ 2184 Cow Exhibition 奶牛展(01背包,变形)
题意:有只奶牛要证明奶牛不笨,所以要带一些奶牛伙伴去证明自己.牛有智商和幽默感,两者可为负的(难在这),要求所有牛的智商和之 / 幽默感之和都不为负.求两者之和的最大值. 思路:每只牛可以带或不带上, ...
- 得到本地应用程序的EXE的路径
实现效果: 知识运用: Application类的ExecutablePath属性 //获取启动了应用程序的可执行文件的路径和可执行文件的名称 public static string Exec ...
- Vmware 安装CentOS7时连不上网问题的解决
在VmWare 上安装Centos7时,装好vmware后还是连不上网,通过查找资料原来是因为有线网卡没有激活,默认centos和redhat7都是不启用有线网卡的,要么手动开启,要么安装时直接启用! ...
- java static block
java 中 静态块的作用 (一)java 静态代码块 静态方法区别一般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在 ...
- CornerStone使用跳坑总结
Cornerstone是专门为Mac用户设计的Subversion(SVN)的控制,使版本控制更加透明.cornerstone根Xcode相比,能够更好的忽略文件,所以除了项目经理第一次初始化项目的时 ...
- Manifest文件
Manifest文件是简单的文本文件,它告知浏览器缓存的内容(或不缓存的内容) Manifest文件可以分为三个部分: 1.CAHCEMANIFEST-在此标题下列出的文件将在首次下载后进行缓存. C ...
- Servlet的引入(即加入Servlet)
今天讲的Servlet是根据上一章节<创建一个学生信息表,与页面分离>而结合. 一.看图分析 此模式有问题: 1.jsp需要呼叫javabean StudentService stuSer ...
- cocos2dx 通过jni调用安卓底层方法
cocos2dx通过封装JniHelper类来调用安卓api底层函数,该文件在cocos/platform/android/jni/JniHelper.h,使用方法如下: 打开eclipse,导入co ...
- 使用lua实现Spine动画的预加载
创建spine动画有两种方法,分别是createwithfile和createwithdata. createWithFile是通过加载动作数据马上进行创建,如果spine动画中的json文件大小超过 ...
- 【费用流】bzoj1834: [ZJOI2010]network 网络扩容
还是稍微记一下这个拆点模型吧 Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的情况下,1到N的最大流: ...