您的位置:首页 > 移动开发 > Android开发

android检测版本更新

2016-07-19 17:41 330 查看
/**
*检测版本更新
*/
package com.st.xld.uitl;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Xml;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ScrollView;
import android.widget.TextView;

import com.st.xld.R;
import com.st.xld.activity.main.GlobalApplication;

/**
* @author yuyue
*
*/
public class CheckUpdateTask extends AsyncTask<String, Integer, Integer> {
private Context mContext;

int versionCode;
int updateVersionCode;

public CheckUpdateTask(Context context) {
this.mContext = context;
}

// onPreExecute方法用于在执行后台任务前做一些UI操作
@Override
protected void onPreExecute() {

}

// doInBackground方法内部执行后台任务,不可在此方法内修改UI
@Override
protected Integer doInBackground(String... params) {
int nResult = 0;
HttpURLConnection connection = null;

try {
String requestUrl = GlobalApplication.mstrUpdateXML;

URL url = new URL(requestUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(GlobalApplication.NETWORK_TIMEOUT);
connection.setReadTimeout(GlobalApplication.NETWORK_TIMEOUT);

InputStream is = connection.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while ((n = is.read(buf)) > 0) {
bos.write(buf, 0, n);
}
String strXML = new String(bos.toByteArray());
strXML = strXML.substring(strXML.indexOf("<update>")); // 去掉头信息

// 利用ANDROID提供的API快速获得pull解析器
XmlPullParser pullParser = Xml.newPullParser();
// 设置需要解析的XML数据
pullParser.setInput(new StringReader(strXML));
// 取得事件
int event = pullParser.getEventType();
// 若为解析到末尾,文档结束

GlobalApplication.mUpdateInfo.clear();
while (event != XmlPullParser.END_DOCUMENT) {
String nodeName = pullParser.getName();
switch (event) {
// 文档开始
case XmlPullParser.START_DOCUMENT:
break;
// 标签开始
case XmlPullParser.START_TAG:
if (!"update".equalsIgnoreCase(nodeName)) {
String strText = pullParser.nextText();
GlobalApplication.mUpdateInfo.put(nodeName, strText);
if ("version".equalsIgnoreCase(nodeName)) {
// 获取软件版本号,
versionCode = this.mContext.getPackageManager()
.getPackageInfo("com.st.xld", 0).versionCode;
updateVersionCode = Integer.valueOf(strText);
if (updateVersionCode > versionCode) {
GlobalApplication.mbNewUpdate = true;
nResult = 1;
}
}
}
break;
// 标签结束
case XmlPullParser.END_TAG:
break;
}
// 下一个标签
event = pullParser.next();
}
} catch (Exception e) {
nResult = -1;
e.printStackTrace();
}
if (connection != null) {
connection.disconnect();
connection = null;
}

return nResult;
}

// onProgressUpdate方法用于更新进度信息
@Override
protected void onProgressUpdate(Integer... progresses) {

}

// onPostExecute方法用于在执行完后台任务后更新UI,显示结果
@Override
protected void onPostExecute(Integer nResult) {
// 隐藏进度对话框
if (nResult > 0) {
// 构造对话框
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
final Dialog noticeDialog = builder.create();
noticeDialog.show();

Window window = noticeDialog.getWindow();
window.setContentView(R.layout.update_dialog);
ScrollView scrollView = ((ScrollView) window.findViewById(
Window.ID_ANDROID_CONTENT)
.findViewById(R.id.layoutInfo));
if (Integer.parseInt(Build.VERSION.SDK) >= 9) {
scrollView.setOverScrollMode(View.OVER_SCROLL_NEVER);
}
((TextView) window.findViewById(R.id.textInfo))
.setText(GlobalApplication.mUpdateInfo.get("releasenotes"));
window.findViewById(R.id.textOK).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
// 实现下载的代码
Uri uri = Uri.parse(GlobalApplication.mUpdateInfo
.get("url"));
Intent intent = new Intent(Intent.ACTION_VIEW,
uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
noticeDialog.cancel();
}
});
window.findViewById(R.id.textCancel).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
noticeDialog.cancel();
}

});
}
}

// onCancelled方法用于在取消执行中的任务时更改UI
@Override
protected void onCancelled() {
}

}

// 检查版本更新调用
CheckUpdateTask task = new CheckUpdateTask(this);
task.execute("");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: