Java Android:在Android中使用水平进度条显示下载过程
我有一个在android中创建进度条的类Java Android:在Android中使用水平进度条显示下载过程,java,android,jsoup,android-progressbar,Java,Android,Jsoup,Android Progressbar,我有一个在android中创建进度条的类 import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.widget.Button; import android.view.View; import android.view.View.OnClickListener; public class
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MyAndroidAppActivity extends Activity {
Button btnStartProgress;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
private long fileSize = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
btnStartProgress = (Button) findViewById(R.id.btnStartProgress);
btnStartProgress.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("File downloading ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
progressBarStatus = 0;
fileSize = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
progressBarStatus = doSomeTasks();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
if (progressBarStatus >= 100) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBar.dismiss();
}
}
}).start();
}
});
}
public int doSomeTasks() {
while (fileSize <= 1000000) {
fileSize++;
if (fileSize == 100000) {
return 10;
} else if (fileSize == 200000) {
return 20;
} else if (fileSize == 300000) {
return 30;
}
}
return 100;
}
}
那么,如何混合此代码片段以显示在下载过程进度条上呢
提前感谢。请提供您的代码的最简单示例。例如,导入是不相关的。@Lal我有一个类,它在android中创建了一个进度条,这是第一个代码段,所以我将把第二个代码段放在哪里,以显示在下载过程进度条上
Document doc;
try {
String url = "http://it.tdt.edu.vn/kind/of/posts/ho%E1%BA%A1t-d%E1%BB%99ng";
// need http protocol
doc = Jsoup.connect("http://it.tdt.edu.vn/kind/of/posts/tin-tuc-su-kien").get();
// Document doc = Jsoup.parse(new URL(url).openStream(), "UTF-8", url);
// get page title
String title = doc.title();
System.out.println("title : " + title);
// get all links
Elements links = doc.select("article section p a[href]");
for (Element link : links) {
// get the value from href attribute
System.out.println("\nlink : " + link.attr("href"));
System.out.println("----------------------------------------------------------------");
Document document = Jsoup.connect(link.attr("href")).get();
System.out.println(document.select("section > article section").first());
}
} catch (IOException e) {
e.printStackTrace();
}