編輯:關於Android編程
因為在更新進度的時候,有點卡,所以想,減緩進度更新的間隔時間。使其不那麼頻繁。
直接看代碼分析下。
HttpHandler.java 實現了RequestCallBackHandler的下載進度監聽
private ResponseInfohandleResponse(HttpResponse response) throws HttpException, IOException { if (response == null) { throw new HttpException("response is null"); } if (isCancelled()) return null; StatusLine status = response.getStatusLine(); int statusCode = status.getStatusCode(); if (statusCode < 300) { Object result = null; HttpEntity entity = response.getEntity(); if (entity != null) { isUploading = false; if (isDownloadingFile) { autoResume = autoResume && OtherUtils.isSupportRange(response); String responseFileName = autoRename ? OtherUtils.getFileNameFromHttpResponse(response) : null; FileDownloadHandler downloadHandler = new FileDownloadHandler(); result = downloadHandler.handleEntity(entity, this, fileSavePath, autoResume, responseFileName); //在這裡我找到了,進度接口實現的實體了,我們點進去看看。 } else { StringDownloadHandler downloadHandler = new StringDownloadHandler(); result = downloadHandler.handleEntity(entity, this, charset); if (HttpUtils.sHttpCache.isEnabled(requestMethod)) { HttpUtils.sHttpCache.put(requestUrl, (String) result, expiry); } } } return new ResponseInfo (response, (T) result, false); } else if (statusCode == 301 || statusCode == 302) { if (httpRedirectHandler == null) { httpRedirectHandler = new DefaultHttpRedirectHandler(); } HttpRequestBase request = httpRedirectHandler.getDirectRequest(response); if (request != null) { return this.sendRequest(request); } } else if (statusCode == 416) { throw new HttpException(statusCode, "maybe the file has downloaded completely"); } else { throw new HttpException(statusCode, status.getReasonPhrase()); } return null; }
如果處於進度更新狀態,就讓其走間隔更新進度,其他狀態不進行間隔設置。
public class FileDownloadHandler {
public File handleEntity(HttpEntity entity,
RequestCallBackHandler callBackHandler,
String target,
boolean isResume,
String responseFileName) throws IOException {
if (entity == null || TextUtils.isEmpty(target)) {
return null;
}
File targetFile = new File(target);
if (!targetFile.exists()) {
File dir = targetFile.getParentFile();
if (dir.exists() || dir.mkdirs()) {
targetFile.createNewFile();
}
}
long current = 0;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
FileOutputStream fileOutputStream = null;
if (isResume) {
current = targetFile.length();
fileOutputStream = new FileOutputStream(target, true);
} else {
fileOutputStream = new FileOutputStream(target);
}
long total = entity.getContentLength() + current;
bis = new BufferedInputStream(entity.getContent());
bos = new BufferedOutputStream(fileOutputStream);
if (callBackHandler != null && !callBackHandler.updateProgress(total, current, true)) {
return targetFile;
}
byte[] tmp = new byte[4096];
int len;
while ((len = bis.read(tmp)) != -1) {
bos.write(tmp, 0, len);
current += len;
if (callBackHandler != null) {
if (!callBackHandler.updateProgress(total, current, false)) { //進度更新,有更新的頻率
return targetFile;
}
}
}
bos.flush();
if (callBackHandler != null) {
callBackHandler.updateProgress(total, current, true);//必須更新
}
} finally {
IOUtils.closeQuietly(bis);
IOUtils.closeQuietly(bos);
}
if (targetFile.exists() && !TextUtils.isEmpty(responseFileName)) {
File newFile = new File(targetFile.getParent(), responseFileName);
while (newFile.exists()) {
newFile = new File(targetFile.getParent(), System.currentTimeMillis() + responseFileName);
}
return targetFile.renameTo(newFile) ? newFile : targetFile;
} else {
return targetFile;
}
}
}
最後,我們回到HttpHandler.java裡面
private long lastUpdateTime;
@Override
public boolean updateProgress(long total, long current, boolean forceUpdateUI) {
if (callback != null && this.state != State.CANCELLED) {
if (forceUpdateUI) {
this.publishProgress(UPDATE_LOADING, total, current);
} else {
long currTime = SystemClock.uptimeMillis();
if (currTime - lastUpdateTime >= callback.getRate()) {
lastUpdateTime = currTime;
this.publishProgress(UPDATE_LOADING, total, current); //間隔一定時間進行更新 進度
}
}
}
return this.state != State.CANCELLED;
}最後,到哪裡設置呢。間隔時間呢。
到RequestCallBack.java
private static final int DEFAULT_RATE = 1000;
private static final int MIN_RATE = 200;
//修改這裡即可。完畢
public final int getRate() {
if (rate < MIN_RATE) {
return MIN_RATE;
}
return rate;
}
或者通過在 callback裡面去 setRate(2000)都可以。
Android實現局部圖片滑動指引效果示例
今天發布本文的原因是應一個網友要求,就是實現局部的圖片滑動指引效果。這種效果一般是在新聞客戶端上比較常見,其功能是:1、頂部單張圖片左右拖拉滑動;2、帶指引;3、僅滑動頂
Volley 源碼解析(一)
心情來這家公司也有差不多一年的時間了,項目中網絡請求部分用到的是Volley,之前都是從別人的博客中了解Volley的用法和他的工作原理。如今項目也寫的差不多了,回想起來
android上傳圖片(及普通參數)到服務器(j2ee後台服務器,ssh框架)
最近項目中需要客戶端往服務器傳輸圖片,並且還需要附帶一些普通參數,研究了幾天,把結果記錄下。 首先客戶端可服務端進行通信一般都是有http請求來發送和接收數據,這裡an
Android7.0 MessageQueue詳解
Android中的消息處理機制大量依賴於Handler。每個Handler都有對應的Looper,用於不斷地從對應的MessageQueue中取出消息處理。 一直以來,覺