-1

根据每个经过的条件,我有多个异步类在后台运行。

示例:不完全是代码,它只是一个演示流程,如果有任何疑问,请告诉我!!

new BB().execute();
{
    //in background method
    if(condition){
        new CC().execute();
        ....
    }
}

它进入 AA 类内部,根据条件,它进入另一个异步类,并且该过程继续进行,直到条件结束或为假。

现在我的问题是,是否有可能有进度条,其中包含正在获取的数据百分比和剩余时间..

4

1 回答 1

1

试试这个代码..它会有 % 的下载

 public class MyAndroidAppActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    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.. Please wait");
            progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressBar.setProgress(0);
            progressBar.setMax(100);
            progressBar.show();

            //resetting progress bar

            progressBarStatus=0;

            //reset filesize
            fileSize=0;

            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (progressBarStatus < 100)
                    {
                       progressBarStatus=doSomeTasks();

                        try
                        {
                            Thread.sleep(1000);
                        }
                        catch(InterruptedException e)
                        {
                            e.printStackTrace();
                        }

                        //updating progress bar
                        progressBarHandler.post(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                progressBar.setProgress(progressBarStatus);
                            }
                        });
                    }
                        //file is downloaded
                    if (progressBarStatus>=100)
                    {
                        //sleep 2 seconds
                        try
                        {
                            Thread.sleep(2000);
                        }
                        catch (InterruptedException e)
                        {
                            e.printStackTrace();
                        }

                        //close progress bar dialog
                        progressBar.dismiss();
                    }
                }
            }).start();

          }
      });
    }

//file download

public int doSomeTasks()
{
    while (fileSize<=1000000)
    {
        fileSize++;

        if (fileSize == 100000)
        {
            return 10;
        }
        else if (fileSize == 250000)
        {
            return 25;
        }
        else if (fileSize == 380000)
        {
            return 38;
        }
        else if (fileSize == 470000)
        {
            return 47;
        }
        else if (fileSize == 580000)
        {
            return 58;
        }
        else if (fileSize == 700000)
        {
            return 70;
        }
        else if (fileSize == 770000)
        {
            return 77;
        }
        else if (fileSize == 860000)
        {
            return 86;
        }
        else if (fileSize == 940000)
        {
            return 94;
        }
        else if (fileSize == 990000)
        {
            return 99;
        }
    }
    return 100;
}
}
于 2013-09-19T09:32:14.547 回答