开发者

Have external class call back to AsyncTask for progress update

开发者 https://www.devze.com 2023-02-27 02:56 出处:网络
I\'m running an activity, which has to download a fairly large image from the Internet, and then display it. This works: the download is done via an AsyncTask, and a progress dialog is shown. And when

I'm running an activity, which has to download a fairly large image from the Internet, and then display it. This works: the download is done via an AsyncTask, and a progress dialog is shown. And when the download is finished, the activity showing the image is called.

The problem I have is that the real work is done in an external class. This as other activities call the same routine to fetch an image. So I can not immediately call postUpdate() to set the update as this call would be done in another class. Now I wonder how I can get the progress updates back to my AsyncTask so my progress dialog can show the actual progress made

The AsyncTask subclass currently looks like this:

private class StartShowImage extends AsyncTask<String, Integer, String> {

    private ProgressDialog dialog;

    @Override
    protected void onPreExecute() {

        // Toon een dialog met draaiend wiel terwijl we de foto
        // aan het ophalen zijn.
        dialog = new ProgressDialog(ShowDeaddrop.this);
        dialog.setTitle(R.string.progress_dialog_title);
        dialog.setMessage(getResources().getString(
                R.string.progress_dialog_fetching_image));
        dialog.show();
    }

    /**
     * De hoofdroutine; haalt de foto op.
     */
    @Override
    protected String doInBackground(final String... params) {
        final String imageName = params[0];
        String result = null;
        try {
            result = DeaddropUtil.getImage(context, imageName, ""
                    + deaddropID, true);
        } catch (final IOException e) {
            Log.v(TAG, "Failed to download image " + imageName);
            Log.v(TAG, "" + e);
        }
        return result;
    }

    /**
     * Als we de foto hebben, start ShowImage.
     */
    @Override
    protected void onPostExecute(final String imageName) {
        dialog.dismiss();
        if (isActive)
            if (imageName == null)
                Toast.makeText(context, R.string.toast_show_image_failed,
        开发者_StackOverflow                Toast.LENGTH_SHORT).show();
            else {
                final Intent i = new Intent(ShowDeaddrop.this,
                        ShowImage.class);
                i.putExtra("imageName", imageName);
                startActivityForResult(i, SHOW_IMAGE);
            }
    }
     }

isActive is a boolean that keeps track of whether this activity is active - it's set in onCreate and onResume and unset in onPause.

I've been looking into a broadcast intent - I've seen an example on how to send back such an intent to the main activity, but the problem is that the listener has to be registered/unregstered in onResume/onPause - and AsyncTask is a separate thread. So it seems this method can not be used safely that way.

Edit restating the question, hoping to get answers that address my question.

AsyncTask sets up progress dialog.

The onExecute() thread can directly update the progress dialog. No problem there.

The actual work is done in an external class, so the progress information is known by that external class, which has to communicate it back to the AsyncTask one way or another.

AsyncTask will have to have some kind of listener, or handler, or something that the external class can call back to, in order to give progress updates.

The question is: how to perform this last part of the process? What is a suitable listener? How to implement such a listener? Which thread does the listener end up in - the UI thread like .onPreExecute() and .onPostExecute(), or the work thread from .doInBackground()?


Solved by using a BroadcastIntent.

Main activity creates BroadcastReceiver, IntentFilter and progress dialog; AsyncTask registers/unregisters this receiver and shows/dismisses the dialog in onPreExecute/onPostExecute respectively.

With extra bits in onResume and onPause to not have active receivers when the activity itself is inactive.

It took me a while to understand this intent broadcast, but after that it was a very quick and easy implementation.


If I understand correctly you are wanting to update the progress bar from the async task. I would probably look at the onProgressUpdate of async task. http://developer.android.com/reference/android/os/AsyncTask.html


Your class StartShowImage doesn't have member. This object you have used to create toast also. if you make member Activity and construction declaration for it you can initialize it by constructor like this before you call it's public method which you want to do :

private Activity context;
public StartShowImage(Activity context){
    this.context=context;
}

StartShowImage object=new StartShowImage(this)
object.postUpdate();

This should work and your calling class should extent Activity

0

精彩评论

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