我的进度条显示但不是水平移动。这是我的代码:
package com.net.pvr;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
public class HTTPLocation implements Runnable {
private static HTTPLocation instance;
private ProgressDialog myProgressDialog;
private ByteArrayBuffer baf;
private Activity activity;
public String url;
public String id;
private HTTPLocation()
{
}
public Activity getActivity() {
return activity;
}
public void setActivity(Activity activity) {
this.activity = activity;
}
private Handler handler;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Handler getHandler() {
return handler;
}
public void setHandler(Handler handler) {
this.handler = handler;
}
public static HTTPLocation getInstance() {
instance = new HTTPLocation();
return instance;
}
public void init() {
myProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
myProgressDialog.setProgress(0);
myProgressDialog.incrementProgressBy(5);
myProgressDialog.setMax(100);
Thread t = new Thread(this);
t.start();
}
@Override
public void run() {
try {
String url1 = this.getUrl();
System.out.println("url "+url1);
URL url = new URL(url1);
URLConnection urlConn = url.openConnection();
urlConn.setConnectTimeout(10000);
InputStream is = urlConn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
baf = new ByteArrayBuffer(1000);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
bis.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
Bundle bundle = new Bundle();
if(baf!=null){
bundle.putByteArray("data", baf.toByteArray());
}else{
bundle.putByteArray("data", null);
}
Message messagepush = Message.obtain();
messagepush.what = 200;
messagepush.setData(bundle);
messagepush.setTarget(handler);
myProgressDialog.setIndeterminate(false);
myProgressDialog.dismiss();
messagepush.sendToTarget();
}
}
}
答案 0 :(得分:2)
我有同样的问题尝试使用AsyncTask
线程可以通过它轻松处理,prorgress对话框将完美运行。
private class className extends AsyncTask<Void, Void, Void>{
@Override
protected Integer doInBackground(Void... arg0) {
// your function
}
@Override
protected void onPostExecute(Integer result){
loadingProgress.dismiss();
}
@Override
protected void onPreExecute(){
loadingProgress = ProgressDialog.show(getContext(), "", "Please wait...",true);
}
}
答案 1 :(得分:0)
以下是来自Google的代码:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
您应该在此处添加更新进度条的代码onProgressUpdate(Integer... progress)
。
您可以在此处AsyncTask和Painless Threading了解更多相关信息。
您应该只在UI线程上更新UI元素,或使用AsyncTask
类或Handler
。