android检测版本更新

/**
 *检测版本更新
 */
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("");
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值