android自动更新软件版本
根据网上的然后小改
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
public class UpdateManager {
private Context mContext;
private final String updateMsg = "亲,有新版本,要更新吗!"; //下载消息提示
private Dialog noticeDialog; //下载提示对话框
private Dialog downloadDialog; //下载进度对话框
private ProgressBar mProgressBar; //进度条
private Boolean interceptFlag = false; //标记用户是否在下载过程中取消下载
private Thread downloadApkThread = null; //下载线程
private final String apkUrl = "http://www.xxx/xxx.apk"; //apk的URL地址
private final String savePath = "/sdcard/UpdateDemo/"; //下载的apk存放的路径
private final String saveFileName = savePath + "xxx.apk"; //下载的apk文件
private int progress = 0; //下载进度
private final int DOWNLOAD_ING = 1; //标记正在下载
private final int DOWNLOAD_OVER = 2; //标记下载完成
private final String TAG="版本更新";
//日志打印标签
private Handler mhandler = new Handler() { //更新UI的handler
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case DOWNLOAD_ING:
// 更新进度条
mProgressBar.setProgress(progress);
break;
case DOWNLOAD_OVER:
downloadDialog.dismiss();
installApk();
//安装
break;
default:
break;
}
}
};
/*
* 构造方法
*/
public UpdateManager(Context context) {
this.mContext = context;
}
/*
* 检查是否有需要更新,具体比较版本xml
*/
public void checkUpdate() {
// 到服务器检查软件是否有新版本
//如果有则
showNoticeDialog();
}
/*
* 显示版本更新对话框
*/
private void showNoticeDialog() {
AlertDialog.Builder builder = new Builder(mContext);
builder.setTitle("版本更新");
builder.setMessage(updateMsg);
builder.setPositiveButton("更新", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
noticeDialog.dismiss();
showDownloadDialog();
}
});
builder.setNegativeButton("以后再说", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
noticeDialog.dismiss();
}
});
noticeDialog = builder.create();
noticeDialog.show();
}
/*
* 弹出下载进度对话框
*/
private void showDownloadDialog() {
AlertDialog.Builder builder = new Builder(mContext);
builder.setTitle("软件更新");
final LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.progress, null);
mProgressBar = (ProgressBar) v.findViewById(R.id.updateProgress);
builder.setView(v);
builder.setNegativeButton("取消", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
downloadDialog.dismiss();
interceptFlag = true;
}
});
downloadDialog = builder.create();
downloadDialog.show();
downloadLatestVersionApk();
}
/*
* 下载最新的apk文件
*/
private void downloadLatestVersionApk() {
downloadApkThread = new Thread(downloadApkRunnable);
downloadApkThread.start();
}
//匿名内部类,apk文件下载线程
private Runnable downloadApkRunnable = new Runnable() {
public void run() {
try {
URL url = new URL(apkUrl);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(5*1000);
conn.connect();
int length = conn.getContentLength();
Log.e(TAG, "总字节数:"+length);
InputStream is = conn.getInputStream();
File file = new File(savePath);
if (!file.exists()) {
file.mkdir();
}
File apkFile = new File(saveFileName);
FileOutputStream out = new FileOutputStream(apkFile);
int count = 0;
int readnum = 0;
byte[] buffer = new byte[1024];
do {
readnum = is.read(buffer);
count += readnum;
progress = (int) (((float) count / length) * 100);
Log.e(TAG, "下载进度"+progress);
mhandler.sendEmptyMessage(DOWNLOAD_ING);
if (readnum <= 0) {
// 下载结束
mhandler.sendEmptyMessage(DOWNLOAD_OVER);
break;
}
out.write(buffer,0,readnum);
} while (!interceptFlag);
is.close();
out.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
/*
* 安装下载的apk文件
*/
private void installApk() {
File file= new File(saveFileName);
if(!file.exists()){
return;
}
Intent intent= new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://"+file.toString()), "application/vnd.android.package-archive");
mContext.startActivity(intent);
}
}
android自动更新软件版本的更多相关文章
- 批量自动更新SVN版本库 - Windows
开发过程中每天都要从SVN代码库里一个一个的update各个项目代码,不仅效率实在是低,也不符合程序员的"懒"精神,由于是在Windows环境做开发,自然就想到了使用bat来实现自 ...
- android自动更新程序,安装完以后就什么都没有了,没有出现安装成功的界面的问题
转载自: http://blog.csdn.net/lovexieyuan520/article/details/9250099 在android软件开发中,总是需要更新版本,所以当有新版本开发的时候 ...
- Android 自动更新 + IIS7 添加APK mime
如果APK文件放在IIS下面需要添加APK的mime,否则会出现下面错误 可以在IIS上添加mime映射 .apk application/vnd.android 下面内容转自:http://ww ...
- fedora23没有/var/log/messages &如何禁用后台自动更新软件?
警告!! Linux是一个非常敏感的操作系统,若删除文件错误,很容易造成系统崩溃. fedora23没有/var/log/messages 不是没有messages这个文件,而是 从 fc core ...
- CentOS 7 环境下部署 SVN 并实现自动更新(版本库放在Tomcat下)
1.安装 SVN 1.1先检查是否有安装 svn rpm -qa subversion #没有什么显示就说明没有安装过yum remove subversion #如果有安装就运行删除老版本yum i ...
- 游戏《Minecraft》或其他应用程序 实现 自动更新 客户端版本
本渣又来写(水)博客了. 先说一下,我这个解决方案的安全性并不是企业级的,咱们就是一群穷开服的Minecraft玩家. 如果你要投入到企业级应用(容易被黑客攻击的场景),请自己写,思路凑合看看.不然安 ...
- C#程序自动更新软件版本号
最近因为服务器程序管理多,所以在查看服务器程序的时候,只能通过EXE的编译时间来判断服务器程序版本时间,费神伤身啊 现在想了一个方式,在目录下新增一个version文件,里面写上年月日,并且只是在程序 ...
- android studio 更新Gradle版本方法
在导入其他项目时,经常由于gradle版本不一致而导致不能编译 解决方法: 第一步: 按提示点击让它下载,其实目的并不是要它下载,因为这样速度会很慢,这样做只是为了让它在本地创建相应的目录结构 第二步 ...
- Android自动更新安装后显示‘完成’‘打开’按钮
/** * 安装apk * * @param url */ private void installApk() { File apkfile = new File(apkFilePath); if ( ...
随机推荐
- 非阻塞io与记录锁
非阻塞io 1.对比 阻塞io的例子:scanf从终端获取输入时,如果不输入程序就会一直停在那; 对一个已经有写锁的文件请求读时, 会一直空等直到前面的进程释放锁... 非阻塞的例子:读取文件内容, ...
- 第六周 E题 期望.....
Description Given a dice with n sides, you have to find the expected number of times you have to thr ...
- Linux内核学习方法
Makefile不是Make Love 从前在学校,混了四年,没有学到任何东西,每天就是逃课,上网,玩游戏,睡觉.毕业的时候,人家跟我说Makefile我完全不知,但是一说Make Love我就来劲了 ...
- MVC初学 - The type or namespace name 'DbContext' could not be found
问题: The type or namespace name 'DbContext' could not be found (are you missing a using directive or ...
- Enum(枚举)示例
package main; public class EnumTest { /** * 普通枚举 */ public enum ColorEnum { red, g ...
- BZOJ 3875: [Ahoi2014]骑士游戏 dp+spfa
题目链接: 题目 3875: [Ahoi2014]骑士游戏 Time Limit: 30 Sec Memory Limit: 256 MB 问题描述 [故事背景] 长期的宅男生活中,JYY又挖掘出了一 ...
- 有关javascript中的JSON.parse和JSON.stringify的使用一二
有没有想过,当我们的大后台只是扮演一个数据库的角色,json在前后台的数据交换中扮演极其重要的角色时,作为依托node的前端开发,其实相当多的时间都是在处理数据,准确地说就是在处理逻辑和数据(这周实习 ...
- memcached-repcached
memcached的复制功能 下载对应的repcached版本:http://sourceforge.jp/projects/sfnet_repcached/,必须版本对应才行 当前只支持到1.2.8 ...
- Codeforces Round #241 (Div. 2)->B. Art Union
B. Art Union time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- [设计模式] 3 创建者模式 builder
转载http://blog.csdn.net/wuzhekai1985/article/details/6667467 建造者模式的定义将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不 ...