DownloadManager在Android 8.0上不起作用。我不知道为什么。有人可以帮帮我吗?
这就是我的尝试:
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)
答案 0 :(得分:0)
确保手机上未启用任何VPN应用程序。在我的情况下,我在手机上运行了一个广告拦截器应用程序帽,干扰了DownloadManager。
答案 1 :(得分:0)
您可以通过检查DownloadManager内部的状态来缩小问题的范围。如果下载没有开始,则可能已暂停。
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");
}
}
从那里开始,问题取决于您在上面的代码段中看到的内容,但是以下是一些常见的情况:
如果您的网址使用的是https://,则可能存在此问题-Why are https downloads pausing with PAUSED_WAITING_TO_RETRY?
如果您的网址使用的是http://,但未在Android 9上使用,则可能会看到以下内容:Cleartext HTTP traffic to 192.168.1.2 not permitted
如果您的网址是Android 9上的http://,那么您会看到:How to solve Android P DownloadManager stopping with "Cleartext HTTP traffic to 127.0.0.1 not permitted"? 不幸的是,这是由此Google错误-https://issuetracker.google.com/issues/114143692引起的。唯一的解决方法是提供https:替代方案。