DownloadManager的通知不适用于MOTO G6

问题描述 投票:0回答:1

我正在使用DownloadManager从webview下载文件,它几乎适用于所有情况,除了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" />

但没有奏效

android android-download-manager motorola
1个回答
0
投票

试试这个,它适用于我的所有版本: -

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);
© www.soinside.com 2019 - 2024. All rights reserved.