一個好的應用軟件都是須要好的維護,從初出版本到最後精品,這個過程須要版本不停的更新,那麼如何讓用戶第一時間獲取最新的應用安裝包呢?那麼就要求咱們從第一個版本就要實現升級模塊這一功能。
自動更新功能的實現原理,就是咱們事先和後臺協商好一個接口,咱們在應用的主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" />