本篇文章是直接下載最新的APK安裝的方法,并不是增量下載該APk。
想要實(shí)現(xiàn)一個(gè)android應(yīng)用,自動(dòng)更新下載APK軟件的方法,我采取的是以下幾步方法:
1.每次進(jìn)入主界面時(shí),獲取服務(wù)器的數(shù)據(jù),看是否是最新版本,是,則無操作,否,則進(jìn)行以下步驟;
2.彈出是否更新軟件的對(duì)話框,點(diǎn)擊下載后
3.彈出下載的進(jìn)度條的對(duì)話框,開始下載,可以上隨時(shí)點(diǎn)擊按鈕,停止下載
4.下載完成后,調(diào)用系統(tǒng)安裝軟件的服務(wù),安裝軟件
效果圖:
實(shí)現(xiàn)過程:
新建一個(gè)UpdateManager方法,具體內(nèi)容我已經(jīng)有詳細(xì)的注釋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
package lgx.acc.updatedemo; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; public class UpdateManager { // 應(yīng)用程序Context private Context mContext; // 是否是最新的應(yīng)用,默認(rèn)為false private boolean isNew = false ; private boolean intercept = false ; // 下載安裝包的網(wǎng)絡(luò)路徑 private String apkUrl = "http://shouji.360tpcdn.com/360sj/tpi/20130201/" + "com.flikie.wallpapers.gallery_4.apk" ; // 保存APK的文件夾 private static final String savePath = "/sdcard/updatedemo/" ; private static final String saveFileName = savePath + "UpdateDemoRelease.apk" ; // 下載線程 private Thread downLoadThread; private int progress; // 當(dāng)前進(jìn)度 TextView text; // 進(jìn)度條與通知UI刷新的handler和msg常量 private ProgressBar mProgress; private static final int DOWN_UPDATE = 1 ; private static final int DOWN_OVER = 2 ; public UpdateManager(Context context) { mContext = context; } /** * 檢查是否更新的內(nèi)容 */ public void checkUpdateInfo() { //這里的isNew本來是要從服務(wù)器獲取的,我在這里先假設(shè)他需要更新 if (isNew) { return ; } else { showUpdateDialog(); } } /** * 顯示更新程序?qū)υ捒颍┲鞒绦蛘{(diào)用 */ private void showUpdateDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setTitle( "軟件版本更新" ); builder.setMessage( "有最新的軟件包,請(qǐng)下載!" ); builder.setPositiveButton( "下載" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showDownloadDialog(); } }); builder.setNegativeButton( "以后再說" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } /** * 顯示下載進(jìn)度的對(duì)話框 */ private void showDownloadDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setTitle( "軟件版本更新" ); LayoutInflater inflater = LayoutInflater.from(mContext); View v = inflater.inflate(R.layout.progress, null ); mProgress = (ProgressBar) v.findViewById(R.id.progress); builder.setView(v); builder.setNegativeButton( "取消" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { intercept = true ; } }); builder.show(); downloadApk(); } /** * 從服務(wù)器下載APK安裝包 */ private void downloadApk() { downLoadThread = new Thread(mdownApkRunnable); downLoadThread.start(); } 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(); } File apkFile = new File(saveFileName); FileOutputStream fos = new FileOutputStream(apkFile); int count = 0 ; byte [] buf = new byte [ 1024 ]; while (!intercept) { int numread = ins.read(buf); count += numread; progress = ( int ) ((( float ) count / length) * 100 ); // 下載進(jìn)度 mHandler.sendEmptyMessage(DOWN_UPDATE); if (numread <= 0 ) { // 下載完成通知安裝 mHandler.sendEmptyMessage(DOWN_OVER); break ; } fos.write(buf, 0 , numread); } fos.close(); ins.close(); } catch (Exception e) { e.printStackTrace(); } } }; /** * 安裝APK內(nèi)容 */ private void installAPK() { File apkFile = new File(saveFileName); if (!apkFile.exists()) { return ; } Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse( "file://" + apkFile.toString()), "application/vnd.android.package-archive" ); mContext.startActivity(intent); }; private Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case DOWN_UPDATE: mProgress.setProgress(progress); break ; case DOWN_OVER: installAPK(); break ; default : break ; } } }; } |
還有progressBar.xml的具體代碼
1
2
3
4
5
6
7
8
9
10
11
12
|
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > < ProgressBar android:id = "@+id/progress" style = "?android:attr/progressBarStyleHorizontal" android:layout_width = "fill_parent" android:layout_height = "wrap_content" /> </ LinearLayout > |
之后在MainActivity的onCreate方法中,調(diào)用一下代碼即可
1
2
|
UpdateManager manager= new UpdateManager(MainActivity. this ); manager.checkUpdateInfo(); |
一定要記得在manifest里面加權(quán)限哈,
1
2
|
< uses-permission android:name = "android.permission.INTERNET" /> < uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" /> |
最后效果就出來了。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/HarryWeasley/article/details/44955719