Android提⽰版本更新+notification显⽰下载进度⾸先:参考了blog.csdn/harvic880925/article/details/25191159这篇⽂章。然后⾃⼰做了⼀遍,更改下载进度条
服务器端:放置了⼀个l⽂件来保存版本号和版本名称等
客户端:点击查看版本信息时,去服务器端取l这个⽂件,然后使⽤XmlPullParser解析,判断本地与服务器端的版本是否需要更新。如果需要更新,提供apk下载地址,然后下载就好。
⼀、从服务器端获取l⽂件,解析
1、给定地址获取,转换成String类型的格式
public static String getInputStreamFromServer(String urlSpec) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
URL url = new URL(urlSpec);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
try {
if (ResponseCode() != HttpURLConnection.HTTP_OK) {
return null;
}
InputStream is = InputStream();
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int len = -1;
while ((len = ad(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection.disconnect();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return new ByteArray());
}
2、将以上⽂件使⽤XmlPullParser解析,其中UpdateInfo是本地⽤于存储xml转换后的数据结构
public static UpdateInfo getUpdateInfo(String urlString) throws Exception {
XmlPullParser parser = wPullParser();
parser.setInput(new StringReader(urlString));
int eventType = EventType();
UpdateInfo info = new UpdateInfo();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if ("verCode".Name())) {document有安卓版吗
info.setVerCode(Integer.Text()));
} else if ("verName".Name())) {
info.Text());
} else if ("description".Name())) {
info.Text());
} else if ("apkUrl".Name())) {
info.Text());
}
}
eventType = ();
}
return info;
}
⼆、⽐较本地版本和l中获取的版本,确定是否需要更新注:⾥边涉及到的⽹络请求需要异步
UpdateInfo info = UpdateInfo(Common
.getInputStreamFromServer(Common.SERVER_IP_VERSIONXML));
m_verName = VerName();
m_verCode = VerCode();
if (VerCode() > vercode) {
return true;
} else {
return false;
}
三、下载apk+更新进度条
1、下载⽂件
void downFile(final String downloadUrl) {
new Thread() {
@Override
public void run() {
try {
URL url = new URL(downloadUrl);
HttpURLConnection connection = (HttpURLConnection) url
.
openConnection();
try {
if (ResponseCode() != HttpURLConnection.HTTP_OK) {      return;
}
long length = ContentLength();
InputStream is = InputStream();
BufferedInputStream bis = new BufferedInputStream(is);
File file = new File(Environment
.getExternalStorageDirectory()
.getAbsoluteFile()
+ File.separator + "test.apk");
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(file));
byte[] buffer = new byte[1024];
int len = -1;
int count = 0; // 记录进度
while ((len = ad(buffer)) != -1) {
bos.write(buffer, 0, len);
count += len;
progress = (int) (((double) count / length) * 100);
if (length > 0 && progress % 5 == 0) {
Message msg = progressHandler.obtainMessage(
MESSAGE_UPDATEPROGRESS, progress);
msg.arg1 = progress;
msg.sendToTarget();
}
}
bos.flush();
bos.close();
down(); // 告诉HANDER已经下载完成了,可以安装了
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection.disconnect();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
};
}.start();
}
2、更新进度条(注意:每次更新进度条RemoteViews需要新new)
Handler progressHandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == MESSAGE_UPDATEPROGRESS) {
romoteView = new RemoteViews(getPackageName(),
i_progressbar);// 每次更新notification,必须重新new
// RomoteViews
romoteView.setProgressBar(R.id.pb_notification, 100, msg.arg1,
false);
romoteView.setTextViewText(R.id.tv_notification, "已下载"
+ msg.arg1 + "%");
}
super.handleMessage(msg);
};
};
四、下载完成以后就需要安装
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new ExternalStorageDirectory()  .getAbsoluteFile() + File.separator + "test.apk")),
"application/vnd.android.package-archive");
startActivity(intent);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。