版本更新

来源:互联网 发布:钉钉邮箱 绑定域名 编辑:程序博客网 时间:2024/06/06 20:47

一、bean包

/**
 * 版本更新实体类
 *
 *
 */
public class UpdateVersion  implements Serializable{
    private String versionCode;

    private String versionName;

    private String downloadUrl;

    private String updateLog;

    public String getVersionCode() {
        return versionCode;
    }

    public void setVersionCode(String versionCode) {
        this.versionCode = versionCode;
    }

    public String getVersionName() {
        return versionName;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public String getDownloadUrl() {
        return downloadUrl;
    }

    public void setDownloadUrl(String downloadUrl) {
        this.downloadUrl = downloadUrl;
    }

    public String getUpdateLog() {
        return updateLog;
    }

    public void setUpdateLog(String updateLog) {
        this.updateLog = updateLog;
    }
}


二、工具类



/**
 * 版本更新
 *
 *
 */
public class VersionUpdateUtils{

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String xml = (String) msg.obj;
            jiexiApkData(xml);
        }
    };


    private UpdateVersion update;

    private Context context;
    public void jiexiApkData(String xml) {
        try {
            XmlPullParser newPullParser = Xml.newPullParser();
            ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
            newPullParser.setInput(bais, "utf-8");
            int eventType = newPullParser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String name = newPullParser.getName();
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if ("android".equals(name)) {
                        update = new UpdateVersion();
                    } else if ("versionCode".equals(name)) {
                        update.setVersionCode(newPullParser.nextText());
                    } else if ("versionName".equals(name)) {
                        update.setVersionName(newPullParser.nextText());
                    } else if ("downloadUrl".equals(name)) {
                        update.setDownloadUrl(newPullParser.nextText());
                    } else if ("updateLog".equals(name)) {
                        update.setUpdateLog(newPullParser.nextText());
                    }
                    break;
                }
                eventType = newPullParser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        panduanUpdate();
    }

    public void reguestAPKData(Context context) {
        this.context=context;
        new Thread() {
            @Override
            public void run() {
                try {
                    URL url = new URL("http://www.oschina.net/MobileAppVersion.xml");
                    HttpURLConnection conn = (HttpURLConnection) url
                            .openConnection();
                    conn.setConnectTimeout(5000);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    InputStream is = conn.getInputStream();
                    byte[] buffer = new byte[1024];
                    int len = -1;
                    while ((len = is.read(buffer)) != -1) {
                        baos.write(buffer, 0, len);
                    }
                    Message message = handler.obtainMessage();
                    message.obj = baos.toString();
                    handler.sendMessage(message);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }


    protected void installApk(File file) {

        Intent intent = new Intent();


        intent.setAction(Intent.ACTION_VIEW);


        intent.setDataAndType(Uri.fromFile(file),
                "application/vnd.android.package-archive");

        context.startActivity(intent);

    }

    protected void downLoadApk() {

        final ProgressDialog pd;

        pd = new ProgressDialog(context);

        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        pd.setMessage("正在下载更新");

        pd.show();
        new Thread() {

            @Override
            public void run() {

                try {
                    File file = DownLoadManager.getFileFromServer(
                            update.getDownloadUrl(), pd);

                    sleep(3000);
                    installApk(file);

                    pd.dismiss();

                } catch (Exception e) {

                }

            }
        }.start();
    }

    private void initAlert() {
        try {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("软件版本更新");
            builder.setMessage(update.getUpdateLog());
            builder.setPositiveButton("立即更新",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            downLoadApk();
                        }
                    });
            builder.setNegativeButton("以后再说",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });

            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void panduanUpdate() {
        try {
            PackageManager packageManager = AppContext.getContext()
                    .getPackageManager();
            PackageInfo packInfo = packageManager.getPackageInfo(AppContext
                    .getContext().getPackageName(), 0);
            String versionName = packInfo.versionName;
            if (!versionName.equals(update.getVersionName())) {
                initAlert();
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}



/**
 * 下载
 *
 *
 */
public class DownLoadManager {
    public static File getFileFromServer(String path, ProgressDialog pd) throws Exception{

        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){

            URL url = new URL(path);

            HttpURLConnection conn =  (HttpURLConnection) url.openConnection();

            conn.setConnectTimeout(5000);


            pd.setMax(conn.getContentLength());

            InputStream is = conn.getInputStream();

            File file = new File(Environment.getExternalStorageDirectory(), "updata.apk");

            FileOutputStream fos = new FileOutputStream(file);

            BufferedInputStream bis = new BufferedInputStream(is);

            byte[] buffer = new byte[1024];

            int len ;

            int total=0;

            while((len =bis.read(buffer))!=-1){

                fos.write(buffer, 0, len);

                total+= len;


                pd.setProgress(total);

            }

            fos.close();

            bis.close();

            is.close();

            return file;

        }

        else{

            return null;

        }

    }
}


三、在哪使用就调用


VersionUpdateUtils vutils = new VersionUpdateUtils();
 vutils.reguestAPKData(this);


1 0
原创粉丝点击