好像大半年沒發點啥了,也不知道本身瞎忙啥,閒下來發給最近的東東《安卓在線升級》java
類已經封裝好了,簡單的調用就OK了。這裏的數據交互,我是用.NET寫的一個webservice來交互。廢話也不說了,直接上代碼了。android
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
package
com.android.xt.dg;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
org.apache.http.HttpEntity;
import
org.apache.http.HttpResponse;
import
org.apache.http.client.ClientProtocolException;
import
org.apache.http.client.HttpClient;
import
org.apache.http.client.methods.HttpGet;
import
org.apache.http.impl.client.DefaultHttpClient;
import
org.json.JSONArray;
import
org.json.JSONObject;
import
com.android.xt.BLL.DGWebServicesBLL;
import
com.android.xt.common.AppSettingInfo;
import
com.android.xt.common.Common;
import
com.xt.dialog.XTAlertDialog;
import
com.xt.dialog.XTProgressDialog;
import
android.app.Activity;
import
android.app.ProgressDialog;
import
android.content.Context;
import
android.content.DialogInterface;
import
android.content.Intent;
import
android.content.pm.PackageManager.NameNotFoundException;
import
android.graphics.Bitmap.Config;
import
android.net.Uri;
import
android.os.Bundle;
import
android.os.Environment;
import
android.os.Handler;
import
android.os.Message;
public
class
CheckUpgrade {
public
Activity Act;
//調用該類的Activity
public
String apkName=
"dgapk.apk"
;
//下載保存的APK名稱
public
XTProgressDialog pBar;
//進度條
public
CheckUpgrade(Activity act)
{
this
.Act=act;
}
/*************
*獲取當前的apk版本代碼
********************/
public
static
int
getVerCode(Context context) {
int
verCode = -
1
;
try
{
//com.android.xt.dg 爲程序的包名。
verCode = context.getPackageManager().getPackageInfo(
"com.android.xt.dg"
,
0
).versionCode;
}
catch
(NameNotFoundException e) {
}
return
verCode;
}
/*************
*獲取當前的apk版本名稱
********************/
public
static
String getVerName(Context context) {
String verName =
""
;
try
{
//com.android.xt.dg 爲程序的包名。
verName = context.getPackageManager().getPackageInfo(
"com.android.xt.dg"
,
0
).versionName;
}
catch
(NameNotFoundException e) {
}
return
verName;
}
/*************
*檢查版本是升級。
********************/
public
void
GetVersionInfo()
{
//顯示進度條(此處進度條爲自定義的一個進度條,能夠修改成原生或者你本身的進度條,也能夠直接去掉)
pBar = XTProgressDialog.createDialog(Act).setMessage(
"正在檢查,請稍候..."
);
pBar.show();
try
{
//啓動一個線程調用webservice讀取版本信息
new
Thread(
new
Runnable(){
public
void
run(){
try
{
DGWebServicesBLL bll=
new
DGWebServicesBLL();
String msg=bll.GetServerVersion();
Message message=
new
Message();
message.what=
1
;
Bundle data=
new
Bundle();
data.putString(
"msg"
, msg);
message.setData(data);
//讀取數據近些界面的處理
myHandler.sendMessage(message);
}
catch
(Exception e) {
e.printStackTrace();
}
}
}).start();
}
catch
(Exception e) {
e.printStackTrace();
Common.Message(e.getMessage(), Act);
}
}
Handler myHandler =
new
Handler() {
@Override
public
void
handleMessage(Message msg) {
try
{
pBar.dismiss();
//關閉進度條
switch
(msg.what)
{
case
1
:{
//取得數據並處理,我返回的數據格式爲json格式,格式示例以下
//{"result":"true","versionCode":"1","versionName":"1.0","downFile":"http://www.968610.com.cn/app/dgapp.apk"}
Bundle data=msg.getData();
String resultMsg=data.getString(
"msg"
);
JSONObject result=
new
JSONObject(resultMsg);
String jsonResult=
""
;
jsonResult=result.getString(
"result"
);
if
(jsonResult.equals(
"false"
))
{
resultMsg=result.getString(
"msg"
);
}
else
if
(jsonResult.equals(
"true"
))
{
//獲取服務器版本
String ServerCodeStr =result.getString(
"versionCode"
);
int
ServerCode=Integer.parseInt(ServerCodeStr);
//服務器版本名
String ServerName =result.getString(
"versionName"
);
String downFile =result.getString(
"downFile"
);
//獲取本地版本信息
int
localCode=getVerCode((Context)Act);
String localName=getVerName((Context)Act);
if
(ServerCode > localCode) {
doNewVersionUpdate(ServerName,localName,downFile);
// 更新新版本
}
else
{
notNewVersionShow(ServerName);
// 提示當前爲最新版本
}
}
else
{
resultMsg=
"讀取出錯."
;
}
}
break
;
}
}
catch
(Exception ex){
Common.Message(ex.getMessage(), Act);
}
super
.handleMessage(msg);
}
};
/******************
*提示無最新版本,
********************************/
private
void
notNewVersionShow(String ServerName) {
StringBuffer sb =
new
StringBuffer();
sb.append(
"當前版本:"
+ServerName);
sb.append(
",已經是最新版,無需更新!"
);
//此處對話框也爲自定義對話框,
XTAlertDialog.Builder builder=
new
XTAlertDialog.Builder(Act);
builder.setTitle(
"軟件更新"
).setMessage(sb.toString());
builder.setPositiveButton(
"肯定"
,
new
DialogInterface.OnClickListener() {
public
void
onClick(DialogInterface dialog,
int
which) {
dialog.dismiss();
}
});
builder.create().show();
}
/******************
*更新新版本
********************************/
private
void
doNewVersionUpdate(String ServerName,String localName,
final
String DownFile) {
StringBuffer sb =
new
StringBuffer();
sb.append(
"當前版本:"
+localName);
sb.append(
", 發現新版本:"
+ServerName);
sb.append(
", 是否更新?"
);
XTAlertDialog.Builder builder=
new
XTAlertDialog.Builder(Act);
builder.setTitle(
"軟件更新"
).setMessage(sb.toString());
builder.setPositiveButton(
"如今更新"
,
new
DialogInterface.OnClickListener() {
public
void
onClick(DialogInterface dialog,
int
which) {
// pBar = new XTProgressDialog(Act);
// pBar.setTitle("正在下載");
// pBar.setMessage("請稍候...");
//pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pBar = XTProgressDialog.createDialog(Act).setMessage(
"正在下載,請稍候..."
);
pBar.show();
downFile(DownFile);
}
});
builder.setNegativeButton(
"暫不更新"
,
new
DialogInterface.OnClickListener() {
public
void
onClick(DialogInterface dialog,
int
whichButton) {
// 點擊"取消"按鈕以後退出程序
dialog.dismiss();
}
});
builder.create().show();
}
/*********************
*下載文件操做
**********************/
void
downFile(
final
String url) {
pBar.show();
new
Thread() {
//啓動線程瞎子啊
public
void
run() {
HttpClient client =
new
DefaultHttpClient();
HttpGet get =
new
HttpGet(url);
HttpResponse response;
try
{
response = client.execute(get);
HttpEntity entity = response.getEntity();
long
length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream =
null
;
if
(is !=
null
) {
File file =
new
File(
Environment.getExternalStorageDirectory(),
apkName);
fileOutputStream =
new
FileOutputStream(file);
byte
[] buf =
new
byte
[
1024
];
int
ch = -
1
;
int
count =
0
;
while
((ch = is.read(buf)) != -
1
) {
fileOutputStream.write(buf,
0
, ch);
count += ch;
if
(length >
0
) {
}
}
}
fileOutputStream.flush();
if
(fileOutputStream !=
null
) {
fileOutputStream.close();
}
down();
//下載完成,並通知安裝
}
catch
(ClientProtocolException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}.start();
}
void
down() {
myHandler.post(
new
Runnable() {
public
void
run() {
pBar.cancel();
update();
//並通知安裝
}
});
}
void
update() {
Intent intent =
new
Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(
new
File(Environment
.getExternalStorageDirectory(), apkName)),
"application/vnd.android.package-archive"
);
Act.startActivity(intent);
}
}
|
這就封裝好了。記得調用webservices檢查新版本的方法我就不發了。那個就是個讀取數據的而已。web
看看怎麼調用吧。兩句話搞定(其實應該是一句。)apache
1
2
3
4
5
|
public
void
btnCheckUpgrade_Click(View v)
{
CheckUpgrade cu=
new
CheckUpgrade(
this
);
cu.GetVersionInfo();
}
|
不扯了,咱不是官場人士,廢話不說,代碼重要。。。。等有空繼續發些其餘的。json
本文從百度空間搬家到博客園服務器