views:

100

answers:

2

Elegant Solution

Hey People. I'm currently working on a Project and I am thinking of an elegant solution for the implementation, I'm tired of improvised solutions.

Let me try to explain my "Problem" to you:

The task of this part of my app is pretty simple:

I want my app to download some stuff and process that downloaded file in the background, displaying a ProgressDialog in the meantime. After that, the contents should be returned in a List of Strings which are displayed in some ListActivity. So far, not a big deal:

Downloader and the processing stuff is a subclassed AsyncTask and called in a class different from the main Activity. But now my Problem:

Where to call the Progressdialog? And how can the GUI-Thread "react" with a ProgressDialog? Should I call the ProgressDialog from the processing class or is it better to kind of "block" the main class, waiting for a notification?

Greetings

EnflamedSoul

A: 

Define your progess dialog as a global variable.

ProgressDialog pd;

Where you are going to fire off the asyncTask:

showYourProgressDialog;
thread = new aThread().execute();

In your class:

public class aThread extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... args) {
            try {
                //Do your downloading and stuff
                                asyncHandler.sendMessage(asyncHandler.obtainMessage(0));
            } catch (Exception e) {
                Log.e("1", "Error", e);
            }
            return null;
        }
    }

Handler asyncHandler = new Handler() {
        public void handleMessage(Message msg) {
            pd.dismiss();
            if (msg.what == 0) {
                //update what you need to               
            }           
        }
    };
blindstuff
mh okay, thanks, that gave me a good Idea of what to do!
EnflamedSoul
+1  A: 

blindstuffs anwser is going in the right direction, although I wouldn't use an Handler for handling the update progress. AsyncTask has his own functions to handle this, which are imho far easier to use and fit more into the AsyncTask class

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
    protected Long doInBackground(URL... urls) {
    int count = urls.length;
    long totalSize = 0;
    for (int i = 0; i < count; i++) {
        totalSize += Downloader.downloadFile(urls[i]);
        publishProgress((int) ((i / (float) count) * 100));
    }
    return totalSize;
    }

    protected void onProgressUpdate(Integer... progress) {
        setProgressPercent(progress[0]);
    }

    protected void onPostExecute(Long result) {
        showDialog("Downloaded " + result + " bytes");
    }
}

This example is taken from the official Android SDK documentation. The advantage of using publishProgress is that you can pass more than one value, depending on the number of arguments passed into doInBackground (i.e. if you're downloading more than 1 file).

Tseng
Thanks, that really helped me. I screwed up something before, but I think now I got a pretty good solution. Thanks!
EnflamedSoul