Manually ending the IntentService worker thread

我只是一个虾纸丫 提交于 2020-01-03 18:36:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!