简体   繁体   中英

DownloadManager not working on android 8.0

DownloadManager does not work on Android 8.0. I don't know why. Can somebody help me?

This is what I have tried:

val downloadBroadcastReceiver = DownloadBroadcastReceiver()
context.registerReceiver(downloadBroadcastReceiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
request = DownloadManager.Request(Uri.parse(url))
val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(folder + File.separator + fileName))
request.setMimeType(mimeType)
request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, fileName)
request.setTitle(title)
request.setDescription(description)
request.setNotificationVisibility(VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.allowScanningByMediaScanner()
id = downloadManager.enqueue(request)

Make sure you don't have any VPN applications enabled on the phone. In my case I had an ad-blocker application hat ran as VPN on the phone that interfered with the DownloadManager.

You can narrow down the type of problem you are having by checking the status inside of DownloadManager. If the download isn't starting, then it is probably paused.

 try (Cursor cursor = manager.query(query)) {
                if (cursor.moveToFirst()) {
                    int statusColumn = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
                    int downloadStatus = cursor.getInt(statusColumn);
                    if (DownloadManager.STATUS_PAUSED == downloadStatus) {
                        int reasonColumn =   cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
                        int reasonCode = cursor.getInt(reasonColumn);
                        Log.e(TAG, "Download paused: " + reasonCode);
                    } else if (DownloadManager.STATUS_SUCCESSFUL == downloadStatus
                            || DownloadManager.STATUS_FAILED == downloadStatus) {
                        Log.i(TAG, "Download Ended");
                    }
}

From there, the problem depends on what you're seeing in the above block, but here are some common cases you could be having:

If your url is using https://, you might be having this problem - Why are https downloads pausing with PAUSED_WAITING_TO_RETRY?

If you're url is using http:// but not on Android 9, you could be seeing this: Cleartext HTTP traffic to 192.168.1.2 not permitted

If your url is it http:// over Android 9, you're seeing this: How to solve Android P DownloadManager stopping with "Cleartext HTTP traffic to 127.0.0.1 not permitted"? Which unfortunately is caused by this google bug - https://issuetracker.google.com/issues/114143692 . And the only way around that is to provide a https: alternative.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM