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?

有帮助吗?

解决方案

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
}

其他提示

IntentService stops automaticlly after finishing OnHandleIntent. There is no need to do it manually.

Read description of method onHandleIntent()

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top