开发者

Elegant Solution?

开发者 https://www.devze.com 2023-01-22 09:12 出处:网络
Elegant Solution Hey People. I\'m currently working on a Project and I am thinking of an elegant soluti开发者_如何转开发on for the implementation, I\'m tired of improvised solutions.

Elegant Solution

Hey People. I'm currently working on a Project and I am thinking of an elegant soluti开发者_如何转开发on 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


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).


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号