问题
I am having some trouble manually ending the IntentService onHandleIntent method. I am catching an exception which is crucial to the execution of the rest of the thread and I want to stop the thread as i catch the exception. I tried calling stopSelf() or directly calling onDestroy() which I'm sure are probably very bad ways of doing it, and they aren't working. The thread calls them and then continues.
Anyone have any good ideas of how to do this?
回答1:
As sonykuba said, finishing the onHandleIntent method stops the Service. So you could do something like this:
public void onHandleIntent {
try {
// ... your code here
} catch(YourException e) {
// do something with the exception
return; // this prevents the rest of the method from execution
// and thereby stops the service
}
// rest of your code
}
回答2:
IntentService stops automaticlly after finishing OnHandleIntent. There is no need to do it manually.
Read description of method onHandleIntent()
来源:https://stackoverflow.com/questions/7173306/manually-ending-the-intentservice-worker-thread