我研究了这个并没有找到一个明确的答案,它结合了进度条+状态栏+服务?我的问题是文件上传的实现方式(由于服务器支持它的唯一方式),我无法跟踪正在写入的字节数。因此,我无法正确递增进度条以反映准确的文件上载状态。 当前实现增加进度条,上传并在进度条达到100时删除进度条。但是进度不能准确反映上载。上传发生得更早,但酒吧保持开启状态。我希望在发出此请求/响应时显示进度。 以下是我的代码段:
public class UploadFileService extends IntentService {
public UploadFileService() {
super("UploadFileService");
}
@Override
protected void onHandleIntent(Intent intent) {
fName = intent.getExtras().getString(Constants.EXTRA_K_FILENAME);
path = intent.getExtras().getString(Constants.EXTRA_K_FILEPATH);
attribute = intent.getExtras().getString(Constants.EXTRA_K_ATTRIBUTE);
//Start thread to doFilepUpload
uploadthread = new UploadThread(fName, path, attribute);
uploadthread.start();
}
//定义从线程接收消息并更新进度的处理程序
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
Integer total = msg.arg1;
notification.contentView.setProgressBar(R.id.status_progress, 100, total, false);
nm.notify(NOTIFICATION_ID, notification);
if ( total.equals(100) && getResponse() != null ){
// inform the progress bar of updates in progress
uploadthread.setState(UploadThread.STATE_DONE);
Intent broadcastIntent = prepareBroadcast();
// remove the notification (we're done)
nm.cancel(NOTIFICATION_ID);
sendBroadcast(broadcastIntent);
}
}
};
public class UploadThread extends Thread {
private String name;
private String path;
private String attribute;
final static int STATE_DONE = 0;
final static int STATE_RUNNING = 1;
int mState;
int total;
public UploadThread(String fName, String path, String attribute) {
// TODO Auto-generated constructor stub
this.name = fName;
this.path = path;
this.attribute = attribute;
}
@Override
public void run() {
// TODO Auto-generated method stub
setResponse(doFileUpload(name, path, attribute));
if( getResponse() != null){
// house keeping for the application
if(response.result.equalsIgnoreCase("success")){
saveFileName = response.data.savedFileName;
responseMessage = response.message;
}else if (response.result.equalsIgnoreCase("error") ||
response == null ){
responseMessage = response.message;
saveFileName = null;
originalName = fName;
}
}
mState = STATE_RUNNING;
total = 0;
while (mState == STATE_RUNNING && total <=100) {
try {
Log.d(TAG, "^^ Thread.sleep");
Thread.sleep(1000);
} catch (InterruptedException e) {
Logger.e("ERROR", "Thread Interrupted");
}
Message msg = handler.obtainMessage();
msg.arg1 = total;
handler.sendMessage(msg);
total++;
}
}
/* sets the current state for the thread,
* used to stop the thread */
public void setState(int state) {
mState = state;
}
}
有没有人对如何更新进度条以显示正确的上传进度有任何建议/想法? 文件上传过程:
paramString = p.getName()+"="+URLEncoder.encode(p.getValue(), HTTP.UTF_8);
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "unsupported encoding -->
name="+p.getName()+"; value="+p.getValue(), e);
throw e;
}
if (combinedParams.length() > 1) {
combinedParams += "&" + paramString;
} else {
combinedParams += paramString;
}
}
}
request = new HttpPost(url + combinedParams);
MultipartEntity entity = new MultipartEntity();
// add headers
for (NameValuePair h : headers) {
request.addHeader(h.getName(), h.getValue());
}
// add cookies
for(NameValuePair c : cookies) {
request.addHeader(H_COOKIE,
c.getName()+"="+c.getValue());
}
entity.addPart(uploadFile.getName(), new FileBody(uploadFile));
request.setEntity(entity)
答案 0 :(得分:0)
我找到了解决这个问题的方法。它不是一个理想的解决方案,但由于不可能计算字节数,我采用了这种方法。在我创建的上传服务中,我有2个不同的线程。线程#1,上传和线程#2,维护通知区域中的进度条。线程#2取决于#1。当#1处于活动状态时,线程#2递增1并且休眠1秒,并且随着每个进度增量,休眠间隔增加0.2秒。如果上传完成,例如45%,则进度条跳至100%。