file-downloader(斷點續傳)
阿新 • • 發佈:2019-01-29
private OnFileDownloadStatusListener mOnFileDownloadStatusListener = new OnSimpleFileDownloadStatusListener() { @Override public void onFileDownloadStatusRetrying(DownloadFileInfo downloadFileInfo, int retryTimes) { // 正在重試下載(如果你配置了重試次數,當一旦下載失敗時會嘗試重試下載),retryTimes是當前第幾次重試 } @Overridepublic void onFileDownloadStatusWaiting(DownloadFileInfo downloadFileInfo) { // 等待下載(等待其它任務執行完成,或者FileDownloader在忙別的操作) } @Override public void onFileDownloadStatusPreparing(DownloadFileInfo downloadFileInfo) { // 準備中(即,正在連線資源) } @Override public void onFileDownloadStatusPrepared(DownloadFileInfo downloadFileInfo) {// 已準備好(即,已經連線到了資源) } @Override public void onFileDownloadStatusDownloading(DownloadFileInfo downloadFileInfo, float downloadSpeed, long remainingTime) { // 正在下載,downloadSpeed為當前下載速度,單位KB/s,remainingTime為預估的剩餘時間,單位秒 } @Override public void onFileDownloadStatusPaused(DownloadFileInfo downloadFileInfo) {// 下載已被暫停 } @Override public void onFileDownloadStatusCompleted(DownloadFileInfo downloadFileInfo) { // 下載完成(整個檔案已經全部下載完成) } @Override public void onFileDownloadStatusFailed(String url, DownloadFileInfo downloadFileInfo, FileDownloadStatusFailReason failReason) { // 下載失敗了,詳細檢視失敗原因failReason,有些失敗原因你可能必須關心 String failType = failReason.getType(); String failUrl = failReason.getUrl();// 或:failUrl = url,url和failReason.getType()會是一樣的 if(FileDownloadStatusFailReason.TYPE_URL_ILLEGAL.equals(failType)){ // 下載failUrl時出現url錯誤 }else if(FileDownloadStatusFailReason.TYPE_STORAGE_SPACE_IS_FULL.equals(failType)){ // 下載failUrl時出現本地儲存空間不足 }else if(FileDownloadStatusFailReason.TYPE_NETWORK_DENIED.equals(failType)){ // 下載failUrl時出現無法訪問網路 }else if(FileDownloadStatusFailReason.TYPE_NETWORK_TIMEOUT.equals(failType)){ // 下載failUrl時出現連線超時 }else{ // 更多錯誤.... } // 檢視詳細異常資訊 Throwable failCause = failReason.getCause();// 或:failReason.getOriginalCause() // 檢視異常描述資訊 String failMsg = failReason.getMessage();// 或:failReason.getOriginalCause().getMessage() } }; FileDownloader.registerDownloadStatusListener(mOnFileDownloadStatusListener);