App 版本更新

App更新流程:检查更新==>提示更新==>下载apk==>安装新版App

检查更新:根据不同的业务和后台逻辑实现各自不同的检查更新。一般情况是获取并上传本地App的versioncode来和后台数据库进行比较。

提示更新:这里我直接弹出提示框

public void showVersionDialog(final Context context, final VersionBean versionBean) {
       if (versionBean.getVar().getForce() == 0) {//0:强制更新 1:普通更新
            AlertDialog.Builder normalDialog =
                    new AlertDialog.Builder(context);
            normalDialog.setTitle("版本强制更新");
            normalDialog.setCancelable(false);
            normalDialog.setMessage(versionBean.getVar().getContent());
            normalDialog.setPositiveButton("确定",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //...To-do
                            downloadApk(context, versionBean);
                        }
                    });
            // 显示
            normalDialog.show();
        } else {
            AlertDialog.Builder normalDialog =
                    new AlertDialog.Builder(context);
            normalDialog.setTitle("版本更新");
            normalDialog.setCancelable(false);
            normalDialog.setMessage(versionBean.getVar().getContent());
            normalDialog.setPositiveButton("确定",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //...To-do
                            downloadApk(context, versionBean);
                        }
                    });
            normalDialog.setNegativeButton("关闭",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //...To-do
                        }
                    });
            // 显示
            normalDialog.show();
        }

    }

下载App:

 /**
     * app下载提示框
     *
     * @param context
     * @param bean
     */
    private void downloadApk(final Context context, VersionBean bean) {
        final AlertDialog build = new AlertDialog.Builder(context).create();
        View view = LayoutInflater.from(context).inflate(R.layout.download_dialog, null);
        final TextView downloadSize = view.findViewById(R.id.downloadSize);
        final TextView netSpeed = view.findViewById(R.id.netSpeed);
        final TextView tvTitle = view.findViewById(R.id.tv_title);
        final TextView tvProgress = view.findViewById(R.id.tvProgress);
        final Button btnInstall = view.findViewById(R.id.btn_install);
        final NumberProgressBar pbProgress = view.findViewById(R.id.pbProgress);

        build.setView(view);
        build.setCancelable(false);
        build.show();
        build.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                OkGo.getInstance().cancelTag("download");
            }
        });
        if (bean.getVar().getForce() == 0) {
            btnInstall.setVisibility(View.GONE);
        } else {
            btnInstall.setVisibility(View.VISIBLE);
        }
        OkGo.get(bean.getVar().getDown_url())//
                .tag("download")
                .execute(new FileCallback("uav.apk") {
                    @Override
                    public void onSuccess(final Response response) {
                        tvTitle.setText("下载成功");
                        tvTitle.setTextColor(Color.BLACK);
                        installApk(context, response.body());
                        build.dismiss();
                    }

                    @Override
                    public void onError(Response response) {
                        tvTitle.setText("下载出错");
                        tvTitle.setTextColor(Color.RED);
                    }

                    @Override
                    public void downloadProgress(Progress progress) {
                        tvTitle.setText("下载中");
                        tvTitle.setTextColor(Color.BLACK);
                        NumberFormat numberFormat;
                        numberFormat = NumberFormat.getPercentInstance();
                        numberFormat.setMinimumFractionDigits(2);

                        String downloadLength = Formatter.formatFileSize(context, progress.currentSize);
                        String totalLength = Formatter.formatFileSize(context, progress.totalSize);
                        downloadSize.setText(downloadLength + "/" + totalLength);
                        String speed = Formatter.formatFileSize(context, progress.speed);
                        netSpeed.setText(String.format("%s/s", speed));
                        tvProgress.setText(numberFormat.format(progress.fraction));
                        pbProgress.setMax(10000);
                        pbProgress.setProgress((int) (progress.fraction * 10000));
                        btnInstall.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                build.dismiss();
                                OkGo.getInstance().cancelTag("download");
                            }
                        });
                    }
                });
    }

安装Apk:注意android 7.0要自己定义一个provider

 /**
     * @param file
     * @return
     * @Description 安装apk
     */
    protected void installApk(Context context, File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // 7.0+以上版本
            Uri apkUri = FileProvider.getUriForFile(context, "com.xxx.xxx.fileprovider", file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        }
        context.startActivity(intent);
    }

你可能感兴趣的:(App 版本更新)