DownloadManager 在 MOTO G6 上不起作用的通知



我正在使用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" />

但没有奏效

试试这个,它在所有版本上都对我有用:-

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);

最新更新