Android Sdk Asynctask Doinbackground Not Running (Subclass)

Android SDK AsyncTask doInBackground not running (subclass)

Matthieu's solution will work fine for most, but some can face problem; unless digging in many links provided here or from web, like Anders Göransson's explanation.
I am trying to summarize some other reads right here and quickly explain solution if executeOnExecutor is still working in single thread...

Behavior of AsyncTask().execute(); has changed through Android versions. Before Donut (Android:1.6 API:4) tasks were executed serially, from Donut to Gingerbread (Android:2.3 API:9) tasks executed paralleled; since Honeycomb (Android:3.0 API:11) execution was switched back to sequential; a new method AsyncTask().executeOnExecutor(Executor) however, was added for parallel execution.

In sequential processing all Async tasks run in a single thread and thus have to wait before the previous task ends. If you need to execute code immediately, you need tasks to be processed in parallel in separate threads.

With AsyncTask serial execution is not available between Donut and Honeycomb versions, while parallel execution is not available before Donut.

For parallel processing after Donut: Check the Build version and based on that use .execute() or .executeOnExecutor() method. Following code can help...

AsyncTask<Void,Void,Void> myTask = new AsyncTask<Void,Void,Void>() { ... }; // ... your AsyncTask code goes here
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
myTask.execute();

NOTE: Function .executeOnExecutor() has checks if targetSdkVersion of project is less than or equal to HONEYCOMB_MR1 (Android:2.1 API:7) then it forces the executor to be THREAD_POOL_EXECUTOR (which runs Tasks sequentially in post Honeycomb).

If you have not defined a targetSdkVersion then minSdkVersion is automatically considered to be the targetSdkVersion.

Hence for running your AsyncTask in parallel on post Honeycomb you cannot leave targetSdkVersion empty.

AsyncTask doInBackground does not run

Removing the AsyncTask and using a traditional Thread instead of combining it with runOnUiThread seems to work. But I still have not found the reason why the AsyncTask is so "unstable".

Here is the code that works for me:

public class ImageActivity extends Activity {

private Thread worker;

public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main);

worker = new Thread(new Runnable(){

private void updateUI(final List<Object> list)
{
if(worker.isInterrupted()){
return;
}
runOnUiThread(new Runnable(){

@Override
public void run()
{
// Update view and remove loading spinner etc...
}
});
}

private List<Object> download()
{
// Simulate download
SystemClock.sleep(1000);
return new ArrayList<Object>();
}

@Override
public void run()
{
Log.d(TAG, "Thread run()");
updateUI(download());
}

});
worker.start(); }

@Override
protected void onDestroy()
{
super.onDestroy();
worker.interrupt();
}
}

Android AsyncTask's doInBackground not started

I solved the problem, I had an other Asynctask in the background which were executed like mAsyncTask2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); also. This AsyncTask has never finished it's doInBackground for special reasons and this caused the problem.

So I replaced the other AsyncTask with a new Thread and it's working now.
Thank you for your suggestions!

AsyncTask called from Handler will not execute doInBackground

Thanks to a helpful discussion with Waqas (thank you!) I finally uncovered the error in my code. In fact all above written is correct and works as is. Issue with my case was that the second task blocked the first one and vice versa.

It was perhaps a coincidence to find this post on Google Groups: http://groups.google.com/group/android-developers/browse_thread/thread/f0cd114c57ceefe3?tvc=2&q=AsyncTask+in+Android+4.0 . Recommend that everyone involved in threading reads this discussion carefully.

AsyncTask switched the Threading model to a serial executor (again), which is not compatible with my approach of having 2 AsyncTasks it seems.

Finally I switched the handling of the "Download" to a classic Thread and used the Handler to post messages to cancel the Slideshow if neccessary. Using the Handlers sendEmptyMessageDelayed I will simple recreate the Download Thread after a while to refresh the data.

Thanks to all comments & answers.

AsyncTask never executing doInBackground (but does in debug) (Update: Fixed)

If it seems to not be executing, something might be throwing an Exception in doInBackground, in which case the LogCat will have a stacktrace from the background thread (maybe something like siteRoot is null?)



Related Topics



Leave a reply



Submit