Note that there are some explanatory texts on larger screens.

plurals
  1. POHave external class call back to AsyncTask for progress update
    text
    copied!<p>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.</p> <p>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</p> <p>The AsyncTask subclass currently looks like this:</p> <pre><code>private class StartShowImage extends AsyncTask&lt;String, Integer, String&gt; { 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, Toast.LENGTH_SHORT).show(); else { final Intent i = new Intent(ShowDeaddrop.this, ShowImage.class); i.putExtra("imageName", imageName); startActivityForResult(i, SHOW_IMAGE); } } } </code></pre> <p>isActive is a boolean that keeps track of whether this activity is active - it's set in onCreate and onResume and unset in onPause.</p> <p>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.</p> <p>Edit restating the question, hoping to get answers that address my question.</p> <p>AsyncTask sets up progress dialog.</p> <p>The onExecute() thread can directly update the progress dialog. No problem there.</p> <p>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.</p> <p>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.</p> <p>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()?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload