问题
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 IntentSerivce for every 100ms and the processing time will be 90 ms for that request.
So for every my request - startSerivce call, service will get invoked and after 90ms (once the processing is done), onDestroy of the IntentServicee will get invoked.
I would like to have this IntentServicee running till i say stop. Is that possible?
assume that i am going to do the below in my service
- Do configure it
- Send some request
- wait for response
- read the response
- Broadcast to activity that invoked
Steps 1 is common for all my requests, so i thought i can do them when service is started initially once and then do 3-6 in HandleIntent based on the requests.
回答1:
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);
}
回答2:
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. ;)
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/6841212/can-an-intentservice-run-indefinitely