okhttputils显示下载进度

来源:互联网 发布:cf手游雷神和无影数据 编辑:程序博客网 时间:2024/05/16 05:58
//download the new appprivate void downLoadNewApp(NewVersion.XianzaishiRfBean version) {        if (StringUtils.isEmpty(version.getUrl())) {            ToastUtils.showToast("新版本的APP url为空");            enterLoginActivity();            return;        }        final ProgressDialog dialog = new ProgressDialog(this);        dialog.setTitle("正在下载");        dialog.setMessage(version.getDes() + "");        dialog.setCancelable(false);        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);        dialog.show();        if (!NetUtils.hasNet(SplashActivity.this)) {            ToastUtils.showToast("请检查网络环境");            return;        }        System.out.println("SplashActivity.downLoadNewApp=" + version.getUrl());        OkHttpUtils                .get()                .url(version.getUrl())                .build()                .execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(), Conts.NEW_APP_NAME) {                    @Overridepublic void onError(Call call, Exception e, int id) {                        enterLoginActivity();                        System.out.println("SplashActivity.onError=哈哈哈");                        ToastUtils.showToast("下载更新包失败");                    }                    @Overridepublic void inProgress(float progress, long total, int id) {                        super.inProgress(progress, total, id);                        dialog.setProgress((int) (100 * progress));                    }                    @Overridepublic void onResponse(File response, int id) {                        Intent intent = new Intent();                        intent.setAction(Intent.ACTION_VIEW);                        intent.addCategory(Intent.CATEGORY_DEFAULT);                        intent.setDataAndType(Uri.fromFile(response),                                "application/vnd.android.package-archive");                        startActivityForResult(intent, 0);                    }                });    }