Android 软件自动更新功能实现的方法
相信所有的用户都遇到过软件提醒更新的情况,下面就将实现此功能
首先看一下程序目录结构
步骤:
1、新建一个类updatemanger,用于显示提示更新
public class updatemanger {
// 应用程序context
private context mcontext;
// 提示消息
private string updatemsg = "有最新的软件包,请下载!";
// 下载安装包的网络路径
private string apkurl = "http://softfile.3g.qq.com:8080/msoft/179/24659/43549/qq_hd_mini_1.4.apk";
private dialog noticedialog;// 提示有软件更新的对话框
private dialog downloaddialog;// 下载对话框
private static final string savepath = "/sdcard/updatedemo/";// 保存apk的文件夹
private static final string savefilename = savepath + "updatedemorelease.apk";
// 进度条与通知ui刷新的handler和msg常量
private progressbar mprogress;
private static final int down_update = 1;
private static final int down_over = 2;
private int progress;// 当前进度
private thread downloadthread; // 下载线程
private boolean interceptflag = false;// 用户取消下载
// 通知处理刷新界面的handler
private handler mhandler = new handler() {
@suppresslint("handlerleak")
@override
public void handlemessage(message msg) {
switch (msg.what) {
case down_update:
mprogress.setprogress(progress);
break;
case down_over:
installapk();
break;
}
super.handlemessage(msg);
}
};
public updatemanger(context context) {
this.mcontext = context;
}
// 显示更新程序对话框,供主程序调用
public void checkupdateinfo() {
shownoticedialog();
}
private void shownoticedialog() {
android.app.alertdialog.builder builder = new android.app.alertdialog.builder(
mcontext);// builder,可以通过此builder设置改变aleartdialog的默认的主题样式及属性相关信息
builder.settitle("软件版本更新");
builder.setmessage(updatemsg);
builder.setpositivebutton("下载", new onclicklistener() {
@override
public void onclick(dialoginterface dialog, int which) {
dialog.dismiss();// 当取消对话框后进行操作一定的代码?取消对话框
showdownloaddialog();
}
});
builder.setnegativebutton("以后再说", new onclicklistener() {
@override
public void onclick(dialoginterface dialog, int which) {
dialog.dismiss();
}
});
noticedialog = builder.create();
noticedialog.show();
}
protected void showdownloaddialog() {
android.app.alertdialog.builder builder = new android.app.alertdialog.builder(mcontext);
builder.settitle("软件版本更新");
final layoutinflater inflater = layoutinflater.from(mcontext);
view v = inflater.inflate(r.layout.progress, null);mprogress = (progressbar) v.findviewbyid(r.id.progress);
builder.setview(v);// 设置对话框的内容为一个view
builder.setnegativebutton("取消", new onclicklistener() {
@override
public void onclick(dialoginterface dialog, int which) {
dialog.dismiss();
interceptflag = true;
}
});
downloaddialog = builder.create();
downloaddialog.show();
downloadapk();
}
private void downloadapk() {
downloadthread = new thread(mdownapkrunnable);
downloadthread.start();
}
protected void installapk() {
file apkfile = new file(savefilename);
if (!apkfile.exists()) {
return;
}
intent i = new intent(intent.action_view);
i.setdataandtype(uri.parse("file://" + apkfile.tostring()),
"application/vnd.android.package-archive");// file.tostring()会返回路径信息
mcontext.startactivity(i);
}
private runnable mdownapkrunnable = new runnable() {
@override
public void run() {
url url;
try {
url = new url(apkurl);
httpurlconnection conn = (httpurlconnection) url.openconnection();
conn.connect();
int length = conn.getcontentlength();
inputstream ins = conn.getinputstream();file file = new file(savepath);
if (!file.exists()) {
file.mkdir();
}
string apkfile = savefilename;
file apkfile = new file(apkfile);
fileoutputstream outstream = new fileoutputstream(apkfile);
int count = 0;
byte buf[] = new byte[1024];
do {
int numread = ins.read(buf);
count += numread;
progress = (int) (((float) count / length) * 100);
// 下载进度
mhandler.sendemptymessage(down_update);
if (numread <= 0) {
// 下载完成通知安装
mhandler.sendemptymessage(down_over);
break;
}
outstream.write(buf, 0, numread);
} while (!interceptflag);// 点击取消停止下载
outstream.close();
ins.close();
} catch (exception e) {
e.printstacktrace();
}
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<progressbar
android:id="@+id/progress"
style="?android:attr/progressbarstylehorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</linearlayout>
3、在mainactivity中oncreate()方法中,进行软件更新的检测
mupdatemanger = new updatemanger(mainactivity.this);// 注意此处不能传入getapplicationcontext();会报错,因为只有是一个activity才可以添加窗体
mupdatemanger.checkupdateinfo();
并且在配置清单文件中添加网络权限 <uses-permission android:name="android.permission.internet" />
4、运行程序
--点击下载-->