一个好的应用软件都是须要好的维护,从初出版本到最后精品,这个过程须要版本不停的更新,那么如何让用户第一时间获取最新的应用安装包呢?那么就要求咱们从第一个版本就要实现升级模块这一功能。
自动更新功能的实现原理,就是咱们事先和后台协商好一个接口,咱们在应用的主Activity里,去访问这个接口,若是须要更新,后台会返回一些数据 (好比,提示语;最新版本的url等)。而后咱们给出提示框,用户点击开始下载,下载完成开始覆盖安装程序,这样用户的应用就保持最新的拉。
为了让你们容易理解,我像往常同样准备一个小例子,这里为了方便我就省去了和后台交互部分了。步骤分别以下:
第一步:新建一个Android工程命名为:UpdateDemo。代码结构以下图所示:java

第二步:新建一个UpdateManager。java类,负责软件更新功能模块,代码以下:android
001 |
package com.tutor.update; |
003 |
import java.io.FileOutputStream; |
004 |
import java.io.IOException; |
005 |
import java.io.InputStream; |
006 |
import java.net.HttpURLConnection; |
007 |
import java.net.MalformedURLException; |
009 |
import android.app.AlertDialog; |
010 |
import android.app.Dialog; |
011 |
import android.app.AlertDialog.Builder; |
012 |
import android.content.Context; |
013 |
import android.content.DialogInterface; |
014 |
import android.content.Intent; |
015 |
import android.content.DialogInterface.OnClickListener; |
016 |
import android.net.Uri; |
017 |
import android.os.Handler; |
018 |
import android.os.Message; |
019 |
import android.view.LayoutInflater; |
020 |
import android.view.View; |
021 |
import android.widget.ProgressBar; |
022 |
public class UpdateManager { |
023 |
private Context mContext; |
025 |
private String updateMsg = "有最新的软件包哦,亲快下载吧~"; |
027 |
private String apkUrl = "http://softfile.3g.qq.com:8080/msoft/179/24659/43549/qq_hd_mini_1.4.apk"; |
028 |
private Dialog noticeDialog; |
029 |
private Dialog downloadDialog; |
031 |
private static final String savePath = "/sdcard/updatedemo/"; |
032 |
private static final String saveFileName = savePath + "UpdateDemoRelease.apk"; |
033 |
/* 进度条与通知ui刷新的handler和msg常量 */ |
034 |
private ProgressBar mProgress; |
035 |
private static final int DOWN_UPDATE = 1; |
036 |
private static final int DOWN_OVER = 2; |
037 |
private int progress; |
038 |
private Thread downLoadThread; |
039 |
private boolean interceptFlag = false; |
040 |
private Handler mHandler = new Handler(){ |
041 |
public void handleMessage(Message msg) { |
044 |
mProgress.setProgress(progress); |
054 |
public UpdateManager(Context context) { |
055 |
this.mContext = context; |
058 |
public void checkUpdateInfo(){ |
061 |
private void showNoticeDialog(){ |
062 |
AlertDialog.Builder builder = new Builder(mContext); |
063 |
builder.setTitle("软件版本更新"); |
064 |
builder.setMessage(updateMsg); |
065 |
builder.setPositiveButton("下载", new OnClickListener() { |
067 |
public void onClick(DialogInterface dialog, int which) { |
069 |
showDownloadDialog(); |
072 |
builder.setNegativeButton("之后再说", new OnClickListener() { |
074 |
public void onClick(DialogInterface dialog, int which) { |
078 |
noticeDialog = builder.create(); |
081 |
private void showDownloadDialog(){ |
082 |
AlertDialog.Builder builder = new Builder(mContext); |
083 |
builder.setTitle("软件版本更新"); |
084 |
final LayoutInflater inflater = LayoutInflater.from(mContext); |
085 |
View v = inflater.inflate(R.layout.progress, null); |
086 |
mProgress = (ProgressBar)v.findViewById(R.id.progress); |
088 |
builder.setNegativeButton("取消", new OnClickListener() { |
090 |
public void onClick(DialogInterface dialog, int which) { |
092 |
interceptFlag = true; |
095 |
downloadDialog = builder.create(); |
096 |
downloadDialog.show(); |
099 |
private Runnable mdownApkRunnable = new Runnable() { |
103 |
URL url = new URL(apkUrl); |
104 |
HttpURLConnection conn = (HttpURLConnection)url.openConnection(); |
106 |
int length = conn.getContentLength(); |
107 |
InputStream is = conn.getInputStream(); |
108 |
File file = new File(savePath); |
112 |
String apkFile = saveFileName; |
113 |
File ApkFile = new File(apkFile); |
114 |
FileOutputStream fos = new FileOutputStream(ApkFile); |
116 |
byte buf[] = new byte[1024]; |
118 |
int numread = is.read(buf); |
120 |
progress =(int)(((float)count / length) * 100); |
122 |
mHandler.sendEmptyMessage(DOWN_UPDATE); |
125 |
mHandler.sendEmptyMessage(DOWN_OVER); |
128 |
fos.write(buf,0,numread); |
129 |
}while(!interceptFlag);//点击取消就中止下载. |
132 |
} catch (MalformedURLException e) { |
134 |
} catch(IOException e){ |
143 |
private void downloadApk(){ |
144 |
downLoadThread = new Thread(mdownApkRunnable); |
145 |
downLoadThread.start(); |
151 |
private void installApk(){ |
152 |
File apkfile = new File(saveFileName); |
153 |
if (!apkfile.exists()) { |
156 |
Intent i = new Intent(Intent.ACTION_VIEW); |
157 |
i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive"); |
158 |
mContext.startActivity(i); |
第三步:在MainActivity。java也就是主Activity调用,代码以下:网络
01 |
package com.tutor.update; |
02 |
import android.app.Activity; |
03 |
import android.os.Bundle; |
04 |
public class MainAcitivity extends Activity { |
05 |
private UpdateManager mUpdateManager; |
07 |
public void onCreate(Bundle savedInstanceState) { |
08 |
super.onCreate(savedInstanceState); |
09 |
setContentView(R.layout.main); |
11 |
mUpdateManager = new UpdateManager(this); |
12 |
mUpdateManager.checkUpdateInfo(); |
第四步:添加程序所用的资源与权限:下载的时候用到了ProgressBar,因此事先写了一个progress.xml布局文件,代码以下:
01 |
<?xml version="1.0" encoding="utf-8"?> |
03 |
xmlns:android="http://schemas.android.com/apk/res/android" |
04 |
android:layout_width="fill_parent" |
05 |
android:layout_height="wrap_content"> |
07 |
android:id="@+id/progress" |
08 |
android:layout_width="fill_parent" |
09 |
android:layout_height="wrap_content" |
10 |
style="?android:attr/progressBarStyleHorizontal" |
下载的时候用到了网络部分,因此要在AndroidManifest。xml中添加网络权限,代码以下:
<uses-permission android:name="android.permission.INTERNET" />