在我的应用程序中,我开始通过 DownloadManager 下载:
val downloadId: Long = downloadManager.enqueue(dr)
如果用户重新打开应用程序,我将通过以下方式检查下载状态:
fun isDownloadInProgress(): Boolean {
val dq: DownloadManager.Query = DownloadManager.Query()
dq.setFilterById(downloadId)
val cursor: Cursor? = downloadManager.query(dq)
if (cursor == null || !cursor.moveToFirst()) {
return false
}
val status: Int = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
if (status == DownloadManager.STATUS_RUNNING || status == DownloadManager.STATUS_PENDING) {
return true
}
return false
}
使用此代码,我在 Android 11 和 12 版本的小米上的 Crashlytics 破折号中发生了一些崩溃:
Caused by java.lang.IllegalArgumentException: column local_filename is not allowed in queries
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:172)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
at android.content.ContentProviderProxy.query(ContentProviderProxy.java:481)
at android.content.ContentResolver.query(ContentResolver.java:1219)
at android.content.ContentResolver.query(ContentResolver.java:1151)
at android.content.ContentResolver.query(ContentResolver.java:1107)
at android.app.DownloadManager$Query.runQuery(DownloadManager.java:1506)
at android.app.DownloadManager.query(DownloadManager.java:1736)
at android.app.DownloadManager.query(DownloadManager.java:1718)
当我调用
val cursor: Cursor? = downloadManager.query(dq)
时它会抛出,因为在幕后query()
方法DownloadManager
调用query(query, UNDERLYING_COLUMNS);
并且UNDERLYING_COLUMS
包含DownloadManager.COLUMN_LOCAL_FILENAME
列。
此列在 API 24 之后被弃用,但
DownloadManager
仍在后台使用此列。
我的问题是:
DownloadManager
不正确并且还有另一种检查下载状态的方法,有人可以建议热这样做吗?谢谢。