-1

I have got multiple asyn Classes to run on background depending on the conditions each passes by..

Example: Not exactly the code, it's just a demo flow, if any doubt, let me know!!

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

It enters inside the Class AA and depending on the condition, it enters into another async class and the process goes on till the condition gets over or false.

Now my question is, Is it possible to have Progress Bar with Percentage of data being fetched and the remaining time..

Stephane Delcroix
  • 16,134
  • 5
  • 57
  • 85
Exceptional
  • 2,994
  • 1
  • 18
  • 25

1 Answers1

1

try this code..it ll have % in downloading

 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;
}
}