Android Intent Service implementing SensorEventLis

2019-08-11 06:19发布

问题:

I have a question about IntentService in Android. I define my own service as below:

public class ABC extends IntentService implements SensorEventListener {

@Override protected void onHandleIntent(Intent intent) { }

@Override public void onSensorChanged(SensorEvent event) { } }

Now if i start the service from other activity, onHandleIntent() is invoked. Documentation says it launches a worker thread to process the request. However, when onSensorChanged() method is invoked, which thread will be executing the code defined in onSensorChanged() method. Will it be the worker thread or the main application thread???

回答1:

Per the IntentService source code, IntentServices stop themselves when they no longer have any messages (the stopSelf call). Therefore if you want a long running Service (which I assume would be appropriate for your SensorEventListener), then you should use a regular Service.