没有进度条更新...异步

时间:2013-12-06 12:35:26

标签: android

我只是通过网址点击按钮点击图片,我想要进度条显示已完成下载,0-100现在我已经更改了代码没有显示进度更新但只有进度条 善意的帮助。 XML有一个按钮[downloadbtn],一个进度条,图像视图和一个退出按钮

mainfest.xml具有acsess permission internet

代码:

*
package com.example.t4;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    ProgressBar prgs;
    ProgressTask task = new ProgressTask();
    Button showProgressBtn;
    Button stopProgressBtn;
    ImageView img;
    Bitmap bmp ;
    TextView tv1;
    int filesize,filedyn;

    public void startdownload(){

        try { URL url;
        url = new URL("http://whywedoit.files.wordpress.com/2009/04/smile.jpg");
        bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        URLConnection urlConnection = url.openConnection();
        urlConnection.connect();

        filesize = urlConnection.getContentLength();

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1=(TextView)findViewById(R.id.tv1);
        img = (ImageView) findViewById(R.id.imageView1) ;
        stopProgressBtn = (Button) findViewById(R.id.btnCancel);
        prgs  = (ProgressBar) findViewById(R.id.progress);
        showProgressBtn = (Button) findViewById(R.id.btn);
        prgs.setVisibility(View.INVISIBLE);
        img.setVisibility(View.INVISIBLE);
        tv1.setVisibility(View.INVISIBLE);

        final ProgressTask pgt = new ProgressTask();


        showProgressBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                if(pgt.getStatus()==Status.RUNNING){Toast.makeText(getApplicationContext(), "Status"+pgt.getStatus(), Toast.LENGTH_SHORT).show();}
                if(pgt.getStatus()==Status.FINISHED||pgt.getStatus()==Status.PENDING){
                Toast.makeText(getApplicationContext(), "Status"+pgt.getStatus(), Toast.LENGTH_SHORT).show();
                prgs.setVisibility(View.VISIBLE);
                task.execute(10);
                tv1.setVisibility(View.VISIBLE);
            }}
        });

        stopProgressBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                stopProgress();

            }
        });

    }

    private class ProgressTask extends AsyncTask<Integer,Integer,Void>{

        protected void onPreExecute() {
            super.onPreExecute(); 
            prgs.setMax(100); // set maximum progress to 100.

        }
        protected void onCancelled() {
            prgs.setMax(0); // stop the progress
            Log.v("Progress","Cancelled");
            finish();
        }
        protected Void doInBackground(Integer... params) {
            startdownload();
          //for(int j=0;j<=filesize;j++){
            publishProgress((int) ((filedyn / (float) filesize) * 100));
            //}
            Log.v("Progress","Downloading");

            return null;
        }
        protected void onProgressUpdate(Integer... values) {


            prgs.setProgress(0);

            Log.v("Progress","Updating");
        }
        protected void onPostExecute(Void result) {
            img.setVisibility(View.VISIBLE);
            tv1.setVisibility(View.INVISIBLE);
            img.setImageBitmap(bmp);
            prgs.setVisibility(View.INVISIBLE);
            Log.v("Progress", "Finished");

        }

    }



    public void stopProgress() {
        task.cancel(true);  
    }

}


*

3 个答案:

答案 0 :(得分:0)

使用此代码并使用progressdialog而不是progressbar

  @Override
        protected void onProgressUpdate(Integer... progress) {
            //Set the pertange done in the progress dialog
            pd.setProgress((int) (progress[0]));
        }

答案 1 :(得分:0)

您需要设置ProgressBar最大值,并且需要更新onProgressUpdate

中的进度
protected void onProgressUpdate(Integer... values) {
     prgs.setProgress(values[0]);
     Log.v("Progress","Once");
}

并致电

publishProgress(progress) 

在ProgressTask的doInBackground中更新进度

Check for more ref

答案 2 :(得分:0)

抱歉,我认为您只有进度条更新问题。有太多的点需要修复。

在你的OnClickListener中

第一 ,你正在做task.execute(10); showProgress(); AsnycTask是异步任务,你不能把你的showProgress()放在那里。用你的代码做点什么。

private boolean init = true;
private Void doInBackground(Integer... params) {
    init = true;
    startdownload();
    Log.v("Progress", "Downloading");
    return null;
}
protected void onProgressUpdate(Integer... values) {
    if (init){
        showProgress();
        init = false;
    }else{
        prgs.setProgress(5);
        Log.v("Progress", "Once");
    }   
}

第二 startdownload方法,你确定你的startdownload方法正在下载吗?或者只是获取文件大小的长度。说清楚,否则你会让自己感到困惑。

3nd 要调用onProgressUpdate,您需要调用publishProgress(progressValue);这是样本。

private Void doInBackground(Integer... params) {
    init = true;
    startdownload();
    publishProgress(5);   // onProgressUpdate invoked

    //
    // doing download
    //

    Log.v("Progress", "Downloading");
    return null;
}