首页 > 解决方案 > Android DownloadManager 失败,原因为 470

问题描述

片段中的代码是这样的:

long downloadId = viewModel.getDownloadId(); // the id downloaded last time.
DownloadManager manager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query().setFilterById(downloadId);
Cursor c = null;
int downloadStatus = -1;
try {
    c = manager.query(query);
    if (c != null && c.moveToFirst()) {
        downloadStatus = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
    }
} finally {
    if (c != null) {
        c.close();
    }
}
switch (downloadStatus) {
    case DownloadManager.STATUS_SUCCESSFUL:
        //do something
        break;
    case DownloadManager.STATUS_RUNNING:
    case DownloadManager.STATUS_PAUSED:
    case DownloadManager.STATUS_PENDING:
        // register ContentObserver to receive download progress, I query the status in `onChage` methord.
        getContext().getContentResolver().registerContentObserver(
             Uri.parse("content://downloads/my_downloads"), true, mDownLoadChangeObserver);
        break;
    default:
        //show a button to download
}

//button click listener
button.setOnClickListener(v -> {
    long id = manager.enqueue(request);
    // save the download id
    ...
    // register ContentObserver to receive download progress, I query the status in `onChage` methord.
    getContext().getContentResolver().registerContentObserver(
             Uri.parse("content://downloads/my_downloads"), true, mDownLoadChangeObserver);
});

我的问题是:

留在片段中,它工作正常。下载成功后可查询状态;
退出片段,下载仍然可以正常工作;
但是如果我在DownloadManager下载的时候输入片段,它会去查询下载状态。查询的状态是正确的—— DownloadManager.STATUS_RUNNING。但它立即转向DownloadManager.STATUS_FAILEDCOLUMN_REASON 470。

如何解决?

标签: androidandroid-download-manager

解决方案


推荐阅读