HandlerThread vs IntentService
So, after checking the source code of both the HandlerThread and the IntentService I found the following:
- IntentServie has a HandlerThread instance inside (this is the separated working thread)
- IntentService calling selfStop() after onHandleIntent() method executed, to shut down itself (since it's extending the service class).
- IntentService is extended from Service class itself, so if you want you can start it in a separated process if you wish.
IntentService onCreate() method, creating the working thread:
@Override
public void onCreate() {
super.onCreate();
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
IntentService own handler, needed to kill the service after the work is done:
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
onHandleIntent((Intent)msg.obj);
stopSelf(msg.arg1);
}
}
In my reading IntentService is the combination of HandlerThread and Service.
Any further answers and solutions are more than welcome !