android检测版本更新
2016-07-19 17:41
351 查看
/** *检测版本更新 */ 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("");
相关文章推荐
- Android PopupWindow水平偏移
- android mCameraInfo.orientation、setDisplayOrientation
- Android中Cursor类的概念和用法
- android应用开发SharedPreferences存储数据的使用方法
- Android RocooFix 热修复框架[1]
- 状态栏(Android)
- java,android定时器(Timer)的使用
- android图片压缩
- Android6.0以上 上传图片时 需要进行权限申请
- Android 玩转IOC,Retfotit源码解析,教你徒手实现自定义的Retrofit框架
- Android TV开发笔记
- Android 玩转IOC,Retfotit源码解析,教你徒手实现自定义的Retrofit框架
- android控件—MuAutoCompleteTextView
- Android Service 服务(一)—— Service
- Android内存泄漏02
- Android launcher 开发笔记(一) 从脸蛋开始
- Android App监听软键盘按键的三种方式与改变软键盘右下角确定键样式
- Android M权限管理机制:Runtime Permission简介
- 关于 Android 进程保活,你所需要知道的一切
- Android之MVP入门使用(一)