gpt4 book ai didi

android - DownloadManager 在 INSUFFICIENT_SPACE_ERROR 后不发送广播

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:41:38 24 4
gpt4 key购买 nike

问题

如果 cache 目录已满,尝试执行一个简单的请求将失败,而不会发送 DownloadManager.ACTION_DOWNLOAD_COMPLETE 广播。

注意:这个问题是普遍存在的,但大多数情况下可以在缓存有限的低端设备上重现(/data/data/com.android.providers.downloads/cache) 尺寸。

代码

接收器配置正确,因为当操作成功和由于其他原因失败时我仍然收到广播。

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse("http://www.apkmirror.com/wp-content/themes/APKMirror/download.php?id=44753"));

request.setTitle("Facebook");

DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

downloadManager.enqueue(request);

所需的解决方案

我对特定问题的解决方案很感兴趣,如果您也遇到过,我想了解更多信息。
正在寻找一种解决方案,要求我停止使用DownloadManager 或添加WRITE_EXTERNAL_STORAGE 权限。

日志

当缓存越来越满,最后当它不能再容纳时,您可以观察到以下日志入口(使用 downloadmanager 过滤)

11-08 08:47:06.079 830-14261/? I/DownloadManager: Download 135 starting
11-08 08:47:06.989 830-14261/? W/DownloadManager: Downloads data dir: /data/data/com.android.providers.downloads/cache is running low on space. space available (in bytes): -6994124
11-08 08:47:06.999 830-14261/? I/DownloadManager: discardPurgeableFiles: destination = 2, targetBytes = 10485760
11-08 08:47:06.999 830-14261/? I/DownloadManager: Purged files, freed 0 for 10485760 requested
11-08 08:47:07.309 830-14261/? W/DownloadManager: Aborting request for download 135: not enough free space in the filesystem rooted at: /data/data/com.android.providers.downloads/cache and unable to free any more
11-08 08:47:07.319 830-14261/? I/DownloadManager: Download 135 finished with status INSUFFICIENT_SPACE_ERROR

这是一个DEMO PROJECT可以证明这个问题。请记住,到那时缓存目录必须已满(不可清除的项目,根据我的经验,这基本上意味着中止下载)

最佳答案

由于DownloadManager本质上是一个系统ContentProvider,你可以向它注册自己的ContentObserver,所以当下载提供者更新时,它会选择在 INSUFFICIENT_SPACE 的情况下通知观察者。

final DownloadManager downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
context.getContentResolver().registerContentObserver(Uri.parse("content://downloads/my_downloads"),
true, new ContentObserver(null) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Cursor localCursor = downloadManager.query(
new DownloadManager.Query());
if (localCursor.getCount() == 0) {
localCursor.close();
}
localCursor.moveToFirst();
do {
if ((localCursor.getInt(localCursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) & DownloadManager.STATUS_FAILED )!=0) {
// Download failed, go see why
if (localCursor.getInt(localCursor.getColumnIndex(DownloadManager.COLUMN_REASON)) == DownloadManager.ERROR_INSUFFICIENT_SPACE){
Log.w("DownloadStatus", " Download failed with ERROR_INSUFFICIENT_SPACE");
}
}
}while (localCursor.moveToNext());
}
});

注意不要设置状态为 DownloadManager.STATUS_FAILED 的查询过滤器,因为 DownloadManager 奇怪地只将 400 到 600 之间的状态视为失败状态,但是 INSUFFICIENT_SPACE 有错误代码 198...

android.app.DownloadManager.Request:

Cursor runQuery(ContentResolver resolver, String[] projection, Uri baseUri) {
.....
if ((mStatusFlags & STATUS_FAILED) != 0) {
parts.add("(" + statusClause(">=", 400)
+ " AND " + statusClause("<", 600) + ")");
}
}

关于android - DownloadManager 在 INSUFFICIENT_SPACE_ERROR 后不发送广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33595541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com