博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于版本更新
阅读量:6035 次
发布时间:2019-06-20

本文共 3281 字,大约阅读时间需要 10 分钟。

//获取服务器版本号

  HttpUtils utils_ver = new HttpUtils();

        utils_ver.send(HttpMethod.GET, Net_URL.VER_SION, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result_ver = responseInfo.result;
                Gson g = new Gson();
                Vis_Big json = g.fromJson(result_ver, Vis_Big.class);
                list_v = json.getVersion();
                //判断更新
                update();
            }
            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(HomeActivity.this, "请检查网络连接", Toast.LENGTH_LONG).show();
            }
        });

 

//判断更新

 

 try {

            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            String version = info.versionName;
            System.out.println("版本号" + version + list_v.getVersion());
            if (version != list_v.getVersion()) {
                System.out.println("=========" + list_v.getVersion());
                initAlert();
            } else {
                Toast.makeText(getApplicationContext(), "要更新了", Toast.LENGTH_LONG).show();
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

 

//自定义dialog

 

 try {

            Toast.makeText(getApplicationContext(), "更新给糖吃", Toast.LENGTH_LONG).show();
            AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
            builder.setTitle("亲!  快来更新啦");
            builder.setMessage("当前版本号为" + list_v.getVersion());
            builder.setPositiveButton("立即更新",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // 点击立即下载更新包
                            downLoadApk();
                        }
                    });
            builder.setNegativeButton("狠心丑拒",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }

 

//进行下载

public class DownLoadManager {

public static File getFileFromServer(String path, ProgressDialog pd) throws Exception{

        //濡傛灉鐩哥瓑鐨勮瘽琛ㄧず褰撳墠鐨剆dcard鎸傝浇鍦ㄦ墜鏈轰笂骞朵笖鏄彲鐢ㄧ殑
        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;
        }
    }

}

 

//下载进度框

 final ProgressDialog pd; // 进度条对话框

        pd = new ProgressDialog(HomeActivity.this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMessage("正在下载更新");
        pd.show();
        // 进行下载操作
        new Thread() {
            @Override
            public void run() {
                try {
                    // 下载
                    File file = DownLoadManager.getFileFromServer(
                            list_v.getUrl(), pd);
                    sleep(3000);
                    // 安装
                    installApk(file);
                    pd.dismiss(); // 结束掉进度条对话框
                } catch (Exception e) {
                }
            }
        }.start();

 

//跳转安装

 Intent intent = new Intent();

        // 执行动作
        intent.setAction(Intent.ACTION_VIEW);
        // 执行的数据类型
        intent.setDataAndType(Uri.fromFile(file),
                "application/vnd.android.package-archive");
        HomeActivity.this.startActivity(intent);

转载于:https://www.cnblogs.com/taogev5/p/5714726.html

你可能感兴趣的文章