美文网首页
App 版本更新

App 版本更新

作者: YuGoal | 来源:发表于2018-06-13 17:11 被阅读31次

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.<File>get(bean.getVar().getDown_url())//
                .tag("download")
                .execute(new FileCallback("uav.apk") {
                    @Override
                    public void onSuccess(final Response<File> response) {
                        tvTitle.setText("下载成功");
                        tvTitle.setTextColor(Color.BLACK);
                        installApk(context, response.body());
                        build.dismiss();
                    }

                    @Override
                    public void onError(Response<File> 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版本更新

    转自:https://blog.csdn.net/qq_34963282/article/details/7923...

  • App 版本更新

    App更新流程:检查更新==>提示更新==>下载apk==>安装新版App 检查更新:根据不同的业务和后台逻辑实现...

  • app版本更新

    实战版本更新(okhttp3、service、notification) https://github.com/c...

  • APP版本更新

    原文链接1原文链接2 iOS程序自动提示更新的实现方案大致分为两种: 第一种,自己服务器提供一个接口,告知相关ap...

  • APP版本更新

    因为项目中间时间允许,抽时间做了一下app新版本迭代.下面贴一下代码供大家参考,要是感觉还可以请给我一颗鼓励的小星...

  • iOS版本更新

    iOS APP版本 更新

  • Android更新App版本、更新Android版本

    库地址: https://github.com/YaYaG/LightUpdateApp 集成方式 : 1 引入 ...

  • ionic2实现APP自动更新

    app版本更新可以在应用的设置里面检查版本进行更新或者进入app后自动检查更新。我这里实现的是自动检查更新。 第一...

  • ReactNative App更新下载(Android+iOS)

    APP涉及到版本更新(非热更新),版本检测下载App,Android和iOS实现方式不同 1.Android直接和...

  • APP版本更新的一个解决方案

    一、APP版本迭代更新问题 在处理APP版本迭代时,因为苹果手机上用户是可以手动关闭APP Store自动更新功能...

网友评论

      本文标题:App 版本更新

      本文链接:https://www.haomeiwen.com/subject/taijeftx.html