Is it possible to use AsyncTask in a Service class? Is it possible to use AsyncTask in a Service class? multithreading multithreading

Is it possible to use AsyncTask in a Service class?


Also be sure to check out IntentService. If that class is sufficient for your needs, it will take care of a lot of little details involved in making that pattern work correctly.


I Think I found why it is not working in my case.

Here I am using this :

private final INetwork.Stub mBinder = new INetwork.Stub() {        @Override        public int doConnect(String addr, int port) throws RemoteException {            new ConnectTask().execute("test42");            return 0;        }    };

I am using this to do what so called IPC, Inter Process Communication, so I guess that my Service and my Activity are in two differents process, AsyncTask must be executed in the main UI thread according to the android doc, so why I was trying to do seems to me just impossible according to those facts.

If I am wrong please someone can correct me.


Maybe problem is not AsyncTask but something else. For example are you sure your onBind method works correctly? Please try this:

@Overridepublic IBinder onBind(Intent arg0) {    return null;}

You could also try that

public class NetworkService extends Service{  @Override  public void onStart(Intent intent, int startId) {    new ConnectTask().execute("test42");  }}