Уведомление для DownloadManager не работает на MOTO G6 - PullRequest
0 голосов
/ 24 апреля 2019

Я использую DownloadManager для загрузки файлов из веб-просмотра, и он работает почти во всех случаях, кроме MOTO G6 Play. Есть кто-нибудь, кто знает, как я могу заставить его работать?

Мой код:

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

        request.setMimeType(mimeType);
        //------------------------COOKIE!!------------------------
        String cookies = CookieManager.getInstance().getCookie(url);
        request.addRequestHeader("cookie", cookies);
        //------------------------COOKIE!!------------------------
        request.addRequestHeader("User-Agent", userAgent);

        request.setDescription(getString(R.string.download_start));
        request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType));
        DownloadManager dm = (DownloadManager) getContext().getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);

Я пытался поставить

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

Но не сработало

1 Ответ

0 голосов
/ 25 апреля 2019

Попробуйте, у меня работает на всех версиях: -

DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
            Uri downloadUri = Uri.parse(uRl);
            final DownloadManager.Request request = new DownloadManager.Request(
                    downloadUri);
            request.setAllowedNetworkTypes(
                    DownloadManager.Request.NETWORK_WIFI
                            | DownloadManager.Request.NETWORK_MOBILE)
                    .setVisibleInDownloadsUi(true)
                    .setTitle(filename)
                    .setAllowedOverRoaming(false)
                    .setTitle(filename)
                    .setDescription("Downloading File")
                    .setDestinationInExternalPublicDir("/FolderName/", filename);
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            assert mgr != null;
            mgr.enqueue(request);
...