相信所有的用户都遇到过软件提醒更新的情况,下面就将实现此功能
首先看一下程序目录结构
步骤:
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;
- <p> private int progress;// 当前进度<br> private thread downloadthread; // 下载线程<br> private boolean interceptflag = false;// 用户取消下载<br> // 通知处理刷新界面的handler<br> private handler mhandler = new handler() {<br> @suppresslint("handlerleak")<br> @override<br> public void handlemessage(message msg) {<br> switch (msg.what) {<br> case down_update:<br> mprogress.setprogress(progress);<br> break;<br> case down_over:<br> installapk();<br> break;<br> }<br> super.handlemessage(msg);<br> }<br> };<br> public updatemanger(context context) {<br> this.mcontext = context;<br> }<br> // 显示更新程序对话框,供主程序调用<br> public void checkupdateinfo() {<br> shownoticedialog();<br> }<br> private void shownoticedialog() {<br> android.app.alertdialog.builder builder = new android.app.alertdialog.builder(<br> mcontext);// builder,可以通过此builder设置改变aleartdialog的默认的主题样式及属性相关信息<br> builder.settitle("软件版本更新");<br> builder.setmessage(updatemsg);<br> builder.setpositivebutton("下载", new onclicklistener() {<br> @override<br> public void onclick(dialoginterface dialog, int which) {<br> dialog.dismiss();// 当取消对话框后进行操作一定的代码?取消对话框<br> showdownloaddialog();<br> }<br> });<br> builder.setnegativebutton("以后再说", new onclicklistener() {<br> @override<br> public void onclick(dialoginterface dialog, int which) {<br> dialog.dismiss();<br> }<br> });<br> noticedialog = builder.create();<br> noticedialog.show();<br> }<br> protected void showdownloaddialog() {<br> android.app.alertdialog.builder builder = new android.app.alertdialog.builder(mcontext);<br> builder.settitle("软件版本更新");<br> final layoutinflater inflater = layoutinflater.from(mcontext);<br> view v = inflater.inflate(r.layout.progress, null);</p><p> mprogress = (progressbar) v.findviewbyid(r.id.progress);<br> builder.setview(v);// 设置对话框的内容为一个view<br> builder.setnegativebutton("取消", new onclicklistener() {<br> @override<br> public void onclick(dialoginterface dialog, int which) {<br> dialog.dismiss();<br> interceptflag = true;<br> }<br> });<br> downloaddialog = builder.create();<br> downloaddialog.show();<br> downloadapk();<br> }<br> private void downloadapk() {<br> downloadthread = new thread(mdownapkrunnable);<br> downloadthread.start();<br> }<br> protected void installapk() {<br> file apkfile = new file(savefilename);<br> if (!apkfile.exists()) {<br> return;<br> }<br> intent i = new intent(intent.action_view);<br> i.setdataandtype(uri.parse("file://" + apkfile.tostring()),<br> "application/vnd.android.package-archive");// file.tostring()会返回路径信息<br> mcontext.startactivity(i);<br> }<br> private runnable mdownapkrunnable = new runnable() {<br> @override<br> public void run() {<br> url url;<br> try {<br> url = new url(apkurl);<br> httpurlconnection conn = (httpurlconnection) url.openconnection();<br> conn.connect();<br> int length = conn.getcontentlength();<br> inputstream ins = conn.getinputstream();</p><p> file file = new file(savepath);<br> if (!file.exists()) {<br> file.mkdir();<br> }<br> string apkfile = savefilename;<br> file apkfile = new file(apkfile);<br> fileoutputstream outstream = new fileoutputstream(apkfile);<br> int count = 0;<br> byte buf[] = new byte[1024];<br> do {<br> int numread = ins.read(buf);<br> count += numread;<br> progress = (int) (((float) count / length) * 100);<br> // 下载进度<br> mhandler.sendemptymessage(down_update);<br> if (numread <= 0) {<br> // 下载完成通知安装<br> mhandler.sendemptymessage(down_over);<br> break;<br> }<br> outstream.write(buf, 0, numread);<br> } while (!interceptflag);// 点击取消停止下载<br> outstream.close();<br> ins.close();<br> } catch (exception e) {<br> e.printstacktrace();<br> }<br> }<br> };<br>}<br></p>
2、新建一个xml文件,用于定义下载过程中的progressbar的进度显示,名为progress.xml
<?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、运行程序
--点击下载-->