1
- package com . libowei . lib . update ;
1
+ package update ;
2
2
3
3
import android .app .AlertDialog ;
4
4
import android .app .ProgressDialog ;
11
11
import android .os .Message ;
12
12
import android .util .Log ;
13
13
14
+ import com .libowei .lib .update .Constants ;
15
+ import com .libowei .lib .update .DownUtil ;
16
+ import com .libowei .lib .update .UpdateInfo ;
14
17
15
18
import org .apache .commons .io .IOUtils ;
16
19
import org .json .JSONException ;
28
31
*/
29
32
public class UpdateManager {
30
33
31
-
32
34
private Context context ;
33
35
private String curVersion = null ;
36
+ private Integer curVersionCode = -1 ;
34
37
private UpdateHandler handler = null ;
35
38
36
-
37
39
UpdateInfo updateInfo ;
38
40
DownUtil downUtil ;
39
41
ProgressDialog progressDialog ;
@@ -48,7 +50,6 @@ public UpdateManager(Context context) {
48
50
this .handler = new UpdateHandler ();
49
51
}
50
52
51
-
52
53
/**
53
54
* 检查更新
54
55
*
@@ -64,31 +65,38 @@ public void run() {
64
65
HttpURLConnection conn = (HttpURLConnection ) new URL (url ).openConnection ();
65
66
conn .setConnectTimeout (5 * 1000 );
66
67
conn .connect ();
67
- //连接成功
68
+ // 连接成功
68
69
if (conn .getResponseCode () == HttpURLConnection .HTTP_OK ) {
69
70
70
71
String response = IOUtils .toString (conn .getInputStream ());
72
+
73
+ Log .i ("updateManager" , response );
74
+
71
75
updateInfo = parse (response );
72
76
if (updateInfo == null ) {
73
77
handler .sendEmptyMessage (Constants .MSG_PARSE_ERROR );
74
78
return ;
75
79
}
76
- //判断版本号
80
+ // 判断版本号
77
81
String currentVersionName = getVersionName ();
82
+ int currentVersionCode = getVersionCode ();
78
83
79
- if (currentVersionName != null ) {
80
- if (currentVersionName .equals (updateInfo .getVersionName ())) {
81
- //版本号一致->没有更新
82
- handler .sendEmptyMessage (Constants .MSG_NO_UPDATE );
83
- } else {
84
- //版本号不一致->有更新
84
+ if (currentVersionName != null && currentVersionCode != -1 ) {
85
+ if (!currentVersionName .equals (updateInfo .getVersionName ())
86
+ && currentVersionCode < updateInfo .getVersionCode ()) {
87
+
88
+ // 版本号不一致->有更新
85
89
if (updateInfo .getDownloadUrl () == null || "" .equals (updateInfo .getDownloadUrl ())) {
86
- //没有下载url
90
+ // 没有下载url
87
91
handler .sendEmptyMessage (Constants .MSG_PARSE_ERROR );
88
92
return ;
89
93
}
90
- //有更新, 发送Message
94
+ // 有更新, 发送Message
91
95
handler .sendEmptyMessage (Constants .MSG_NEW_UPDATE );
96
+
97
+ } else {
98
+ handler .sendEmptyMessage (Constants .MSG_NO_UPDATE );
99
+
92
100
}
93
101
}
94
102
}
@@ -101,7 +109,6 @@ public void run() {
101
109
}.start ();
102
110
}
103
111
104
-
105
112
/**
106
113
* 当前版本名称
107
114
*/
@@ -119,12 +126,25 @@ private String getVersionName() {
119
126
return "" ;
120
127
}
121
128
129
+ private int getVersionCode () {
130
+
131
+ if (this .curVersionCode != -1 ) {
132
+ return this .curVersionCode ;
133
+ }
134
+ try {
135
+ this .curVersionCode = this .context .getPackageManager ().getPackageInfo (this .context .getPackageName (), 0 ).versionCode ;
136
+ return this .curVersionCode ;
137
+ } catch (PackageManager .NameNotFoundException e ) {
138
+ e .printStackTrace ();
139
+ }
140
+ return -1 ;
141
+ }
142
+
122
143
/**
123
144
* 显示更新提示框
124
145
*/
125
146
private void showUpdateNotice () {
126
147
127
-
128
148
String title = "发现新版本:" + updateInfo .getVersionName ();
129
149
130
150
StringBuilder dialogText = new StringBuilder ();
@@ -134,16 +154,16 @@ private void showUpdateNotice() {
134
154
dialogText .append (updateMsg );
135
155
}
136
156
157
+ new AlertDialog .Builder (context ).setTitle (title ).setMessage (dialogText )
158
+ .setPositiveButton ("开始更新" , new DialogInterface .OnClickListener () {
159
+ @ Override
160
+ public void onClick (DialogInterface dialog , int which ) {
137
161
138
- new AlertDialog .Builder (context ).setTitle (title ).setMessage (dialogText ).setPositiveButton ("开始更新" , new DialogInterface .OnClickListener () {
139
- @ Override
140
- public void onClick (DialogInterface dialog , int which ) {
141
-
142
- //开始下载
143
- downloadApk ();
162
+ // 开始下载
163
+ downloadApk ();
144
164
145
- }
146
- }).setNegativeButton ("下次再说" , new DialogInterface .OnClickListener () {
165
+ }
166
+ }).setNegativeButton ("下次再说" , new DialogInterface .OnClickListener () {
147
167
@ Override
148
168
public void onClick (DialogInterface dialog , int which ) {
149
169
@@ -170,14 +190,12 @@ private void downloadApk() {
170
190
progressDialog .setTitle ("更新软件" );
171
191
progressDialog .show ();
172
192
173
-
174
193
new Thread () {
175
194
@ Override
176
195
public void run () {
177
196
try {
178
197
downUtil .download ();
179
198
180
-
181
199
final Timer timer = new Timer ();
182
200
timer .schedule (new TimerTask () {
183
201
@ Override
@@ -198,7 +216,6 @@ public void run() {
198
216
}
199
217
}, 0 , 100 );
200
218
201
-
202
219
} catch (Exception e ) {
203
220
e .printStackTrace ();
204
221
}
@@ -208,28 +225,26 @@ public void run() {
208
225
209
226
}
210
227
211
-
212
228
/**
213
- * 内部类 UpdateHandler
214
- * 用于处理更新中的各种msg
229
+ * 内部类 UpdateHandler 用于处理更新中的各种msg
215
230
*/
216
231
class UpdateHandler extends Handler {
217
232
@ Override
218
233
public void handleMessage (Message msg ) {
219
234
220
235
switch (msg .what ) {
221
236
case Constants .MSG_NETWORK_ERROR : {
222
- //网络问题
237
+ // 网络问题
223
238
Log .e ("UPDATE" , "网络问题" );
224
239
break ;
225
240
}
226
241
case Constants .MSG_NEW_UPDATE : {
227
- //有更新
242
+ // 有更新
228
243
showUpdateNotice ();
229
244
break ;
230
245
}
231
246
case Constants .MSG_NO_UPDATE : {
232
- //没有更新
247
+ // 没有更新
233
248
Log .e ("UPDATE" , "没有更新啊" );
234
249
break ;
235
250
}
@@ -256,7 +271,6 @@ public void handleMessage(Message msg) {
256
271
}
257
272
}
258
273
259
-
260
274
/**
261
275
* 解析升级的json数据
262
276
*
@@ -273,18 +287,19 @@ private UpdateInfo parse(String json) {
273
287
String versionName = object .getString (Constants .STR_VERSION_NAME );
274
288
String downloadUrl = object .getString (Constants .STR_DOWNLOAD_URL );
275
289
String updateMsg = object .getString (Constants .STR_UPDATE_MSG );
290
+ Integer versionCode = object .getInt (Constants .STR_VERSION_CODE );
276
291
updateInfo = new UpdateInfo ();
277
292
updateInfo .setDownloadUrl (downloadUrl );
278
293
updateInfo .setVersionName (versionName );
279
294
updateInfo .setUpdateMsg (updateMsg );
295
+ updateInfo .setVersionCode (versionCode );
280
296
281
297
} catch (JSONException e ) {
282
298
e .printStackTrace ();
283
299
}
284
300
return updateInfo ;
285
301
}
286
302
287
-
288
303
private void openFile (File file ) {
289
304
Intent intent = new Intent ();
290
305
intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
0 commit comments