Async task does not work properly (doInBackground not executing) when service runs in background, Android Async task does not work properly (doInBackground not executing) when service runs in background, Android multithreading multithreading

Async task does not work properly (doInBackground not executing) when service runs in background, Android


Sometimes you will want more control over your service's lifecycle than what IntentService gives you, in those cases you can just create a thread in the service and run your background code in that. Actually, to be more specific, create a HandlerThread which includes a Looper so you can use the standard android method for communication (messages) between your main thread and the background thread.

Answered here


I think the problem is starting another GetData AsyncTask before the previous one has been finished. Before executing another task make sure that previous one is complete. To do this use following code:

// make sure we don't collide with another pending AsyncTaskif (getDataTask == null || getDataTask.getStatus() == AsyncTask.Status.FINISHED || getDataTask.isCancelled()) {    getDataTask= new GetData();    getDataTask.execute();} 

Also make sure that you have a reference for running tasks. You can use subclass of Application class for doing this while your application is running or override onSaveInstanceState(Bundle outState)and receive a reference to it in onCreate(Bundle savedInstanceState).


Read all the problem and Answers which has been posted here. correct me if i am wrong your scenario is you are parsing the xml and getting the list of songs and when user select any song you want that to be played with service right?

if the Scenario is correct then we can implement it in the much simpler way.

  1. In the Activity, onResume() method parse the XML file and get the list of songs and update the list view(do not start anything related to service here)
  2. when user click on the song then pass the particular key/string to the service with intent and start the service
  3. In the service's OnStartCommand() method get the identifier and start the song as with normal media APIs

That will actually do the work for you.

Regarding the problem of

if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {    new Test().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);} else {    new Test().execute();}

This is for different behavior of the AsyncTask on Different version of the Android.Looking at your code what is being done is in the Activity you are initializing the service hence the service is running in the background without doing anything fruitful.and when user click on play you are calling play function of service which created problme here.

so to call the function of service from Activity you should right AIDL which you have not mentioned. and if you have wrote so it should be perfect.

but here recommendation is pass the song id to service and it should play from service should not call Service's function in activity.

if you want to update the Song List in the onResume of the activity then you must write AIDL and accomplish the scenario

Hope this will help.