即使由于空间不足而失败,我也总是以COLUMN_STATUS
作为STATUS_SUCCESSFUL
:
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null) {
Utils.log(TAG, "Intent is NULL");
return;
}
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
DownloadManager downloadManager = (DownloadManager)
context.getSystemService(Context.DOWNLOAD_SERVICE);
long referenceId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, -1L);
if (downloadManager != null) {
DownloadManager.Query dmQuery = new DownloadManager.Query();
dmQuery.setFilterById(referenceId);
try (Cursor cursor = downloadManager.query(dmQuery)) {
if (cursor != null && cursor.getCount() > 0) {
// The error is here
int columnIndex =cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (cursor.moveToFirst() && cursor.getInt(columnIndex) == DownloadManager.STATUS_SUCCESSFUL) {
} else {
Log.e(TAG, "fail");
}
}
} catch (Exception exception) {
Log.e(TAG, exception);
}
}
}
}
最佳答案
如果您的连接超时,您将失败。
如果由于空间不足而失败,请检查查询列COLUMN_TOTAL_SIZE_BYTES和COLUMN_BYTES_DOWNLOADED_SO_FAR以检查是否相等。
int bytes_total = cur.getInt(cur.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
int bytes_downloaded = cur.getInt(cur.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));