AsyncTask does not update UI thread

I have AsyncTask updating the implementation of the ActionBarSherlock progress. Somehow onProgressUpdate throws a streaming error, although it claims to be executing on a user interface thread.

protected void onProgressUpdate(Integer... values)
{
    setSupportProgress(values[0]);
}

Error:

03-06 00: 13: 11.672: E / AndroidRuntime (4183): at com.anthonymandra.framework.GalleryActivity $ ShareTask.onProgressUpdate (GalleryActivity.java:476)

Only the source stream that created the view hierarchy can touch its views.

As far as I can tell, I have to access the UI thread for this ...

I have many working AsyncTasks in my application, but as requested here doInBackground (simplified):

for (MediaObject image : toShare)
{
    BufferedInputStream imageData = image.getThumbStream();
    File swapFile = getSwapFile(...);
    write(swapFile, imageData);
    ++completed;
    float progress = (float) completed / toShare.size();
    int progressLocation = (int) Math.ceil(progressRange * progress);
    onProgressUpdate(progressLocation);
}
+5
source
3

, , onProgressUpdate, publishProgress. , , / , . , AsyncTasks, .


AsyncTask ? , . onProgressUpdate , AsyncTask.

: ( API 15)!

protected final void publishProgress(Progress... values) {
    if (!isCancelled()) {
        sHandler.obtainMessage(MESSAGE_POST_PROGRESS,
                new AsyncTaskResult<Progress>(this, values)).sendToTarget();
    }
}

Handler sHandler. :

/ , - .

Bruno Mateus :

, : Threading - , : - AsyncTask . JELLY_BEAN. - . execute (Params...) . - onPreExecute(), onPostExecute (), doInBackground (Params...), onProgressUpdate (...) . - ( , .)

+4

AsyncTask :

public void onClick(View v) {
   new DownloadImageTask().execute("http://example.com/image.png");
}

 private class DownloadImageTask extends AsyncTask {
      protected Bitmap doInBackground(String... urls) {
          return loadImageFromNetwork(urls[0]);
     }

     protected void onPostExecute(Bitmap result) {
          mImageView.setImageBitmap(result);
     }
 }
+1

:

onProgressUpdate

publishProgress

, . , , . !

+1

All Articles