Download status:
1.STATUS_FAILED:(download has failed (and will not be retried)下载失败且不会再尝试下载
1)ERROR_CANNOT_RESUME
some possibly transient error occurred but we can't resume the download
2)ERROR_DEVICE_NOT_FOUND
no external storage device was found
3)ERROR_FILE_ALREADY_EXISTS
the requested destination file already exists (the download manager will not overwrite an existing file)目标文件已存在,且不会覆盖文件
4)ERROR_FILE_ERROR
a storage issue arises which doesn't fit under any other error code
5)ERROR_HTTP_DATA_ERROR
an error receiving or processing data occurred at the HTTP level
6)ERROR_INSUFFICIENT_SPACE
there was insufficient storage space
7)ERROR_TOO_MANY_REDIRECTS
there were too many redirects
8)ERROR_UNHANDLED_HTTP_CODE
an HTTP code was received that download manager can't handle
9)ERROR_UNKNOWN
the download has completed with an error that doesn't fit under any other error code
2.STATUS_PAUSED:the download is waiting to retry or resume
1)PAUSED_QUEUED_FOR_WIFI
the download exceeds a size limit for downloads over the mobile network and the download manager is waiting for a Wi-Fi connection to proceed
2)PAUSED_UNKNOWN
the download is paused for some other reason
3)PAUSED_WAITING_FOR_NETWORK
the download is waiting for network connectivity to proceed
4)PAUSED_WAITING_TO_RETRY
the download is paused because some network error occurred and the download manager is waiting before retrying the request
3.STATUS_PENDING:the download is waiting to start
4.STATUS_RUNNING:the download is currently running
STATUS_SUCCESSFUL:the download has successfully completed
private static boolean isNeedDownload(long id, DownloadManager downloadManager){
boolean isNeedDownloadAgain = true;
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(id);
Cursor cursor = downloadManager.query(query);
if(cursor != null && cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnIndex);
int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = cursor.getInt(columnReason);
switch(status){
case DownloadManager.STATUS_FAILED:
switch(reason){
case DownloadManager.ERROR_CANNOT_RESUME:
//some possibly transient error occurred but we can't resume the download
break;
case DownloadManager.ERROR_DEVICE_NOT_FOUND:
//no external storage device was found. Typically, this is because the SD card is not mounted
break;
case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
//the requested destination file already exists (the download manager will not overwrite an existing file)
break;
case DownloadManager.ERROR_FILE_ERROR:
//a storage issue arises which doesn't fit under any other error code
break;
case DownloadManager.ERROR_HTTP_DATA_ERROR:
//an error receiving or processing data occurred at the HTTP level
break;
case DownloadManager.ERROR_INSUFFICIENT_SPACE://sd卡满了
//here was insufficient storage space. Typically, this is because the SD card is full
break;
case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
//there were too many redirects
break;
case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
//an HTTP code was received that download manager can't handle
break;
case DownloadManager.ERROR_UNKNOWN:
//he download has completed with an error that doesn't fit under any other error code
break;
}
isNeedDownloadAgain = true;
AlertUtil.alert("开始重新下载更新!", mContext);
break;
case DownloadManager.STATUS_PAUSED:
switch(reason){
case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
//the download exceeds a size limit for downloads over the mobile network and the download manager is waiting for a Wi-Fi connection to proceed
break;
case DownloadManager.PAUSED_UNKNOWN:
//the download is paused for some other reason
break;
case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
//the download is waiting for network connectivity to proceed
break;
case DownloadManager.PAUSED_WAITING_TO_RETRY:
//the download is paused because some network error occurred and the download manager is waiting before retrying the request
break;
}
isNeedDownloadAgain = false;
AlertUtil.alert("下载已暂停,请继续下载!", mContext);
break;
case DownloadManager.STATUS_PENDING:
//the download is waiting to start
isNeedDownloadAgain = false;
AlertUtil.alert("更新正在下载!", mContext);
break;
case DownloadManager.STATUS_RUNNING:
//the download is currently running
isNeedDownloadAgain = false;
AlertUtil.alert("更新正在下载!", mContext);
break;
case DownloadManager.STATUS_SUCCESSFUL:
//the download has successfully completed
isNeedDownloadAgain = false;
installApk(id, downloadManager, mContext);
break;
}
}
return isNeedDownloadAgain;
}