Не могу установить apk после завершения загрузки - PullRequest
0 голосов
/ 26 апреля 2020
 public void onClick(DialogInterface dialog, int which) {
    final String appPackageName = getApplication().getPackageName();
    File file = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "app-release.apk");
    /*  Create a DownloadManager.Request with all the information necessary to start the download*/
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse("http://adminpanel.esy.es/app-release.apk"))
            .setTitle("app-release.apk")// Title of the Download Notification
            .setDescription("מוריד אנחנו עשירים!")// Description of the Download Notification
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)// Visibility of the download Notification
            .setDestinationUri(Uri.fromFile(file))// Uri of the destination file
            //.setRequiresCharging(false)// Set if charging is required to begin the download
            .setAllowedOverMetered(true)// Set if download is allowed on Mobile network
            .setAllowedOverRoaming(true);// Set if download is allowed on roaming network

    DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    downloadID = downloadManager.enqueue(request);// enqueue puts the download request in the queue.
    BroadcastReceiver onComplete = new BroadcastReceiver() {
        public void onReceive(Context Context, Intent intent) {

            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                intent = new Intent(Intent.ACTION_VIEW);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                Uri apkURI = FileProvider.getUriForFile(
                        context,
                        context.getApplicationContext()
                                .getPackageName() + ".provider", file);
                intent.setDataAndType(apkURI,
                        downloadManager.getMimeTypeForDownloadedFile(downloadID));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                startActivity(intent);
                context.unregisterReceiver(this);
                //finish();
            } else {


                intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
                Uri data = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
                intent.setDataAndType(data, "/*");
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                startActivity(intent);
            }

        }
    };
    registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));


}                                         

После того, как я добавляю BroadcastReceiver, установщик открывает apk как html, но без этого он работал нормально.

Я хочу установить скачанный apk, поэтому я underdsrand, что мне нужно добавить BroadcastReceiver, потому что он сообщает мне, если APK загружен, установка работает без BroadcastReceiver (я помещаю файл apk в папку для его тестирования), но после добавления BroadcastReceiver установщик открывает apk в html открывашка. также BroadcastReceiver работает нормально, потому что установщик запускается только после загрузки apk

Установщик просто открывает apk вот так https://imgur.com/lNehdlr

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...