Android 检查更新的实现

private void downLoadPackage() {
        String url = "https://qd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk";

        if (VERSION.SDK_INT >= VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("download", "下载进度", IMPORTANCE_DEFAULT);
            channel.setImportance(NotificationManager.IMPORTANCE_LOW);

            //初始化通知ui
            nb = new Notification.Builder(this, "download");
            nb.setSmallIcon(R.drawable.ic_launcher_background).setContentTitle("下载中");
            nb.setProgress(100, 0, false).setAutoCancel(true);

            manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            //创建通道,注意这一步很重要
            manager.createNotificationChannel(channel);

            //使用okhttp发送get请求
            OkHttpClient client = new OkHttpClient();
            Request.Builder builder = new Request.Builder();
            Request request = builder.url(url).get().build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {

                    InputStream inputStream = response.body().byteStream();
                    File espd = Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS);
                    //缓存的路径以及文件名
                    String apkPath = espd.getPath() + "/" + "BLiao" + System.currentTimeMillis() + ".apk";
                    FileOutputStream fos = new FileOutputStream(new File(apkPath));

                    //目标文件的总大小
                    long contentLength = response.body().contentLength();

                    int length = 0;
                    //字节数组缓冲区要设置为足够大,过小会导致io速度慢
                    byte[] bytes = new byte[10240];
                    while ((length = inputStream.read(bytes)) != -1) {
                        fos.write(bytes, 0, length);

                        //已经写入的文件内容所占百分数
                        double percent = fos.getChannel().position() * 100.0 / contentLength;
                        //手动刷新进度条
                        nb.setProgress(100, (int) percent, false);
                        manager.notify(1, nb.build());
                    }

                    fos.flush();
                    fos.close();
                    response.body().close();
                    //下载完毕关闭要隐藏通知
                    manager.cancel(1);

                    //调出系统安装应用页面
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    //fileprovider要在清单文件中声明
                    Uri contentUri = FileProvider.getUriForFile(getApplication(), "com.caikeng.app.fileProvider", new File(apkPath));
                    intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
                    startActivity(intent);
                }
            });
        }
    }

本来这个功能应该是轻松加愉快的,但是因为7.0,8.0的各种适配问题,如通知api,系统安装页api的更新,导致各种问题

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

free5156

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值