Search code examples
javaandroidintentservice

AsynTask Never Runs in IntentService


I am trying to use following code:

public class SeparateProcessService extends IntentService implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener
{
public SeparateProcessService() {
        super("IntentService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

        new UploadData().execute();


    }

}

When the line of asynctask is executed it is either skipped by compiler or ignored.

any one guide me what could be the issue? i am actually trying to upload data to api through intentservice.

any help would be appreciated.


Solution

  • AsyncTask.execute() runs asynchronously. If you want to block onHandleIntent() until the AsyncTask completes, you can use new UploadData().get(). Hopefully you're not doing anything in onPreExecute or onPostExecute, because those won't get called.

    I'd recommend avoiding AsyncTask in general and follow the user137021's advice and put the AsyncTask.doInBackground code directly in the IntentService.