Skip to content

Commit 97ca2a4

Browse files
committed
加入版本号的检测(versionCode)
1 parent 5659370 commit 97ca2a4

File tree

11 files changed

+84
-59
lines changed

11 files changed

+84
-59
lines changed

Diff for: .gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
*.iml
22
.gradle
33
/local.properties
4-
/.idea/workspace.xml
5-
/.idea/libraries
4+
.idea
65
.DS_Store
76
/build
87
/captures

Diff for: .idea/.name

-1
This file was deleted.

Diff for: .idea/gradle.xml

+2-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/inspectionProfiles/Project_Default.xml

-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/misc.xml

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/vcs.xml

-6
This file was deleted.

Diff for: README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
```javacript
1010
{
1111
"ver": "3.333",
12+
"verCode":1,
1213
"url": "http://od4xu3l0l.bkt.clouddn.com/libowei.apk",
1314
"msg": "测试需要"
1415
}
1516
```
16-
该json中需包含新版本的版本号、下载地址及更新说明
17+
该json中需包含新版本的版本号、版本名、下载地址及更新说明
18+
19+
### 检测规则 ###
20+
**json中的版本号大于本地版本号****json中版本名与本地版本名不同**,则进行更新。
1721

1822
### 添加到程序中 ###
1923

Diff for: build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.1.3'
8+
classpath 'com.android.tools.build:gradle:2.2.3'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files

Diff for: library/src/main/java/com/libowei/lib/update/Constants.java

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class Constants {
99
public static final String STR_VERSION_NAME = "ver";
1010
public static final String STR_DOWNLOAD_URL = "url";
1111
public static final String STR_UPDATE_MSG = "msg";
12+
public static final String STR_VERSION_CODE = "verCode";
1213

1314
//msg what
1415
public static final int MSG_NO_UPDATE = 0x001;

Diff for: library/src/main/java/com/libowei/lib/update/UpdateInfo.java

+8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ public class UpdateInfo {
88
private String versionName;
99
private String downloadUrl;
1010
private String updateMsg;
11+
private Integer versionCode;
1112

13+
public Integer getVersionCode() {
14+
return versionCode;
15+
}
16+
17+
public void setVersionCode(Integer versionCode) {
18+
this.versionCode = versionCode;
19+
}
1220

1321
public String getUpdateMsg() {
1422
return updateMsg;

Diff for: library/src/main/java/com/libowei/lib/update/UpdateManager.java

+50-35
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.libowei.lib.update;
1+
package update;
22

33
import android.app.AlertDialog;
44
import android.app.ProgressDialog;
@@ -11,6 +11,9 @@
1111
import android.os.Message;
1212
import android.util.Log;
1313

14+
import com.libowei.lib.update.Constants;
15+
import com.libowei.lib.update.DownUtil;
16+
import com.libowei.lib.update.UpdateInfo;
1417

1518
import org.apache.commons.io.IOUtils;
1619
import org.json.JSONException;
@@ -28,12 +31,11 @@
2831
*/
2932
public class UpdateManager {
3033

31-
3234
private Context context;
3335
private String curVersion = null;
36+
private Integer curVersionCode = -1;
3437
private UpdateHandler handler = null;
3538

36-
3739
UpdateInfo updateInfo;
3840
DownUtil downUtil;
3941
ProgressDialog progressDialog;
@@ -48,7 +50,6 @@ public UpdateManager(Context context) {
4850
this.handler = new UpdateHandler();
4951
}
5052

51-
5253
/**
5354
* 检查更新
5455
*
@@ -64,31 +65,38 @@ public void run() {
6465
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
6566
conn.setConnectTimeout(5 * 1000);
6667
conn.connect();
67-
//连接成功
68+
// 连接成功
6869
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
6970

7071
String response = IOUtils.toString(conn.getInputStream());
72+
73+
Log.i("updateManager", response);
74+
7175
updateInfo = parse(response);
7276
if (updateInfo == null) {
7377
handler.sendEmptyMessage(Constants.MSG_PARSE_ERROR);
7478
return;
7579
}
76-
//判断版本号
80+
// 判断版本号
7781
String currentVersionName = getVersionName();
82+
int currentVersionCode = getVersionCode();
7883

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+
// 版本号不一致->有更新
8589
if (updateInfo.getDownloadUrl() == null || "".equals(updateInfo.getDownloadUrl())) {
86-
//没有下载url
90+
// 没有下载url
8791
handler.sendEmptyMessage(Constants.MSG_PARSE_ERROR);
8892
return;
8993
}
90-
//有更新, 发送Message
94+
// 有更新, 发送Message
9195
handler.sendEmptyMessage(Constants.MSG_NEW_UPDATE);
96+
97+
} else {
98+
handler.sendEmptyMessage(Constants.MSG_NO_UPDATE);
99+
92100
}
93101
}
94102
}
@@ -101,7 +109,6 @@ public void run() {
101109
}.start();
102110
}
103111

104-
105112
/**
106113
* 当前版本名称
107114
*/
@@ -119,12 +126,25 @@ private String getVersionName() {
119126
return "";
120127
}
121128

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+
122143
/**
123144
* 显示更新提示框
124145
*/
125146
private void showUpdateNotice() {
126147

127-
128148
String title = "发现新版本:" + updateInfo.getVersionName();
129149

130150
StringBuilder dialogText = new StringBuilder();
@@ -134,16 +154,16 @@ private void showUpdateNotice() {
134154
dialogText.append(updateMsg);
135155
}
136156

157+
new AlertDialog.Builder(context).setTitle(title).setMessage(dialogText)
158+
.setPositiveButton("开始更新", new DialogInterface.OnClickListener() {
159+
@Override
160+
public void onClick(DialogInterface dialog, int which) {
137161

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();
144164

145-
}
146-
}).setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
165+
}
166+
}).setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
147167
@Override
148168
public void onClick(DialogInterface dialog, int which) {
149169

@@ -170,14 +190,12 @@ private void downloadApk() {
170190
progressDialog.setTitle("更新软件");
171191
progressDialog.show();
172192

173-
174193
new Thread() {
175194
@Override
176195
public void run() {
177196
try {
178197
downUtil.download();
179198

180-
181199
final Timer timer = new Timer();
182200
timer.schedule(new TimerTask() {
183201
@Override
@@ -198,7 +216,6 @@ public void run() {
198216
}
199217
}, 0, 100);
200218

201-
202219
} catch (Exception e) {
203220
e.printStackTrace();
204221
}
@@ -208,28 +225,26 @@ public void run() {
208225

209226
}
210227

211-
212228
/**
213-
* 内部类 UpdateHandler
214-
* 用于处理更新中的各种msg
229+
* 内部类 UpdateHandler 用于处理更新中的各种msg
215230
*/
216231
class UpdateHandler extends Handler {
217232
@Override
218233
public void handleMessage(Message msg) {
219234

220235
switch (msg.what) {
221236
case Constants.MSG_NETWORK_ERROR: {
222-
//网络问题
237+
// 网络问题
223238
Log.e("UPDATE", "网络问题");
224239
break;
225240
}
226241
case Constants.MSG_NEW_UPDATE: {
227-
//有更新
242+
// 有更新
228243
showUpdateNotice();
229244
break;
230245
}
231246
case Constants.MSG_NO_UPDATE: {
232-
//没有更新
247+
// 没有更新
233248
Log.e("UPDATE", "没有更新啊");
234249
break;
235250
}
@@ -256,7 +271,6 @@ public void handleMessage(Message msg) {
256271
}
257272
}
258273

259-
260274
/**
261275
* 解析升级的json数据
262276
*
@@ -273,18 +287,19 @@ private UpdateInfo parse(String json) {
273287
String versionName = object.getString(Constants.STR_VERSION_NAME);
274288
String downloadUrl = object.getString(Constants.STR_DOWNLOAD_URL);
275289
String updateMsg = object.getString(Constants.STR_UPDATE_MSG);
290+
Integer versionCode = object.getInt(Constants.STR_VERSION_CODE);
276291
updateInfo = new UpdateInfo();
277292
updateInfo.setDownloadUrl(downloadUrl);
278293
updateInfo.setVersionName(versionName);
279294
updateInfo.setUpdateMsg(updateMsg);
295+
updateInfo.setVersionCode(versionCode);
280296

281297
} catch (JSONException e) {
282298
e.printStackTrace();
283299
}
284300
return updateInfo;
285301
}
286302

287-
288303
private void openFile(File file) {
289304
Intent intent = new Intent();
290305
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

0 commit comments

Comments
 (0)