我想下载文件,更新Progressbar而不是ProgressDialog.I使用AsyncTask下载文件。在onProgressUpdate()方法我想更新进度条进度但没有成功。在日志进度是打印但progresbar没有更新。我的代码是在适配器。
class DownloadFileFromURL extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected String doInBackground(String... f_url)
{
int count;
try
{
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream("/sdcard/downloadedfile.mp3");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1)
{
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
}
catch (Exception e)
{
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress)
{
ViewHolder.progressbar.setProgress(Integer.parseInt(progress[0]));
}
}
请帮我。
试试这个
class DownloadFileFromURL extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(getContext());
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... f_url)
{
int count;
try
{
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream("/sdcard/downloadedfile.mp3");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1)
{
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}
catch (Exception e)
{
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress)
{
ViewHolder.progressbar.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String file_url) {
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Dismiss();
}
}, 1000L);
}
}
public void Dismiss(){
pDialog.dismiss();
}
下面的代码正在运行,尝试一下,也许它可以帮助你。
public class Demo extends AppCompatActivity {
private Button start;
private Button stop;
private static ProgressBar pb;
private static AsyncTask task;
private static int i = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
hookButtons();
}
private void hookButtons() {
start = (Button) findViewById(R.id.btn_start);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
task = new Async().execute();
start.setVisibility(View.GONE);
stop.setVisibility(View.VISIBLE);
}
});
stop = (Button) findViewById(R.id.btn_stop);
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(task != null){
task.cancel(true);
stop.setVisibility(View.GONE);
start.setVisibility(View.VISIBLE);
}
}
});
pb = (ProgressBar) findViewById(R.id.pb);
}
private static class Async extends AsyncTask<Void, String, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d("Async", "Started");
i=0;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Log.d("Async", "Finished");
}
@Override
protected Void doInBackground(Void... voids) {
while (i <= 10000 && !isCancelled()) {
Thread timer = new Thread() {
@Override
public void run() {
super.run();
try {
sleep(1000);
publishProgress(String.valueOf((i / 1000) * 10));
i++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
timer.start();
}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.e("Progress", progress[0]);
pb.setProgress(Integer.parseInt(progress[0]));
if(pb.getProgress() >=100){
task.cancel(true);
task = new Async().execute();
}
}
@Override
protected void onCancelled(Void aVoid) {
super.onCancelled(aVoid);
Log.d("Async", "Canceled");
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (task != null) {
task.cancel(true);
}
}}
您可以在下面找到demo.xml中的代码(Demo Activity的布局):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
/>
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/pb"
android:layout_width="80dp"
android:layout_height="80dp"
android:indeterminate="false"
android:layout_marginBottom="10dp"
android:max="100"
android:progress="0"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:visibility="visible"/>
另外,不要忘记将此活动添加到清单中。