3

I have a method in my activity to download a set of files. This downloading is taking place when I start a new activity. I have used threads, because it downloads completely whereas AsyncTask may sometimes fail to download all files, it may get stuck in between.

Now, a black screen is shown when the downloading takes place. I want to show it within a ProgressDialog so that user may feel that something is getting downloaded.

I have added a ProgressDialog, but its not showing. Can anyone tell where did I go wrong?

Below is my code:

Inside onCreate() I have written:

downloadFiles();

private boolean downloadFiles() {
    showProgressDialog();
    for(int i = 0; i < filesList.size();i++) {
        Thread thread = new Thread(new Runnable() {    
            @Override
            public void run() {
                //downloading code
         });
         thread.start();
         thread.run();
    }
    dismissProgressDialog();
    return true;
}

//ProgressDialog progressDialog; I have declared earlier.
private void showProgressDialog() { 
    progressDialog = new ProgressDialog(N12ReadScreenActivity.this);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setMessage("Downloading files...");
    progressDialog.show();
}

private void dismissProgressDialog() {
    if(progressDialog != null)
        progressDialog.dismiss();
}
Anju
  • 9,379
  • 14
  • 55
  • 94
  • 1
    Maybe this won't resolve the question, but why do you call thread.run()? You can just call thread.start() and the thread will be created. I've always been using AsyncTask for downloading multiple files and never had any problems with using it, and updating the UI thread is not a hard task too. – Egor Jun 23 '11 at 09:24

4 Answers4

1

Try this .. it's simple

ProgressDialog progress = new ProgressDialog(this);
progress.Indeterminate = true;
progress.SetProgressStyle(ProgressDialogStyle.Spinner);
progress.SetMessage("Downloading Files...");
progress.SetCancelable(false);

RunOnUiThread(() =>
{
    progress.Show();
});
Task.Run(()=>
//downloading code here...
).ContinueWith(Result=>RunOnUiThread(()=>progress.Hide()));
0

Try the following code and let me know how it goes:

private Handler mHandler = new Handler(){
    public void handleMessage(Message msg)  
    {
        dismissProgressDialog() 
    }
};

private boolean downloadFiles() {
    showProgressDialog();
    for(int i = 0; i < filesList.size();i++) {
        Thread thread = new Thread(new Runnable() {    
            @Override
            public void run() {
                //downloading code
         });
         thread.start();
         thread.run();
    }
    mHandler.sendEmptyMessage(0);
    return true;
}

//ProgressDialog progressDialog; I have declared earlier.
private void showProgressDialog() { 
    progressDialog = new ProgressDialog(N12ReadScreenActivity.this);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setMessage("Downloading files...");
    progressDialog.show();
}

private void dismissProgressDialog() {
    if(progressDialog != null)
        progressDialog.dismiss();
}
Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114
0

Please try Below Code .

private Handler responseHandler=null;
downloadFiles();

private boolean downloadFiles() {
    showProgressDialog();
    for(int i = 0; i < filesList.size();i++) {
        Thread thread = new Thread(new Runnable() {    
            @Override
            public void run() {
                //downloading code
                responseHandler.sendEmptyMessage(0);
         });
         thread.start();
    }

    responseHandler = new Handler() 
        {                               
            public void handleMessage(Message msg)  
            {
                super.handleMessage(msg);
                try 
                {
                    dismissProgressDialog() 
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }                       
            }
        };
}

Here in this code when ever your dowload will completed it called response handler and your progress dialog will dismiss.

Chirag
  • 56,621
  • 29
  • 151
  • 198
  • Correct me if I'm wrong, but this will dismiss the dialog after the first thread has finished. – Flo Jun 23 '11 at 09:25
  • yes its dismiss the dialog after first thread finished . you have to modify your code like it will call again your function... – Chirag Jun 23 '11 at 09:27
0

In downloadFiles() you show the dialog, then start a number of threads and after they've been started the dialog got dismissed. I don't think this is what you want as the dialog gets closed right after the last thread is started and not after the last thread has finished.

The dismissProgressDialog() method must be called after the last thread has finished its work. So at the end of the code run in the thread you have to check whether other threads are still running or whether you can dismiss the dialog as no other threads are running.

Flo
  • 27,355
  • 15
  • 87
  • 125