Автоматическая установка при загрузке APK - PullRequest
0 голосов
/ 05 декабря 2018

Я хотел бы автоматически устанавливать приложения, когда загрузка приложений завершена. Я использую sdk> 24, загрузка функции успешно завершена, но когда система, выполняющая автоматическую установку, когда загрузка приложений завершена, выдает ошибку, «Error receiving broadcast Intent» и «Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/Download/bakul.apk exposed beyond app through Intent.getData() "

public void Update() {
        String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
        String fileName = "bakul.apk";
        destination += fileName;
        final Uri uri = Uri.parse("file://" + destination);

        //Delete update file if exists
        File file = new File(destination);
        if (file.exists())
            //file.delete() - test this, I think sometimes it doesnt work
            file.delete();

        //get url of app on server
        final String url = "http://192.168.87.87.com/bakul.apk";

        //set downloadmanager
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

        //set destination
        request.setDestinationUri(uri);


        // get download service and enqueue file
        final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        final long downloadId = manager.enqueue(request);

        //set BroadcastReceiver to install app when .apk is downloaded
        BroadcastReceiver onComplete = new BroadcastReceiver() {
            public void onReceive(Context ctxt, Intent intent) {
                Intent install = new Intent(Intent.ACTION_VIEW);
                install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                install.setDataAndType(uri,"application/vnd.android.package-archive");
                startActivity(install);
                unregisterReceiver(this);
                finish();
            }
        };
        //register receiver for when .apk download is compete
        registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }
...