Based on my understanding, an IntentService will get stopped when its current request is done.
Consider the below scenario, i will be triggering a request to the Int
The IntentService
is actually a pretty small class which wraps a Handler, the problem being that after an Intent has been handled it calls stopSelf()
.
Removing that single line gives you an IntentService which needs to be explicitly stopped:
public abstract class NonStopIntentService extends Service {
private String mName;
private volatile Looper mServiceLooper;
private volatile ServiceHandler mServiceHandler;
public NonStopIntentService(String name) {
super();
mName = name;
}
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); <-- Removed
}
}
@Override
public void onCreate() {
super.onCreate();
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override
public void onStart(Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
onStart(intent, startId);
return START_STICKY;
}
@Override
public void onDestroy() {
mServiceLooper.quit();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
/**
* This method is invoked on the worker thread with a request to process.
* Only one Intent is processed at a time, but the processing happens on a
* worker thread that runs independently from other application logic.
* So, if this code takes a long time, it will hold up other requests to
* the same IntentService, but it will not hold up anything else.
*
* @param intent The value passed to {@link
* android.content.Context#startService(Intent)}.
*/
protected abstract void onHandleIntent(Intent intent);
}
IntentService
is extended from standard Service
class, so I don't see why it shouldn't be done this way. In fact I will do it this way too. ;)
You need to create a Service
and bind
to your service for preventing it to stop. See the docs.
The service that will be started with bindService()
will run until no Activity is still bound to it.
If you don't have much work to do in the service you could just extend a regular service. Return null in the onBind() and receive commands in onStartCommand() which returns START_STICKY.