我在手动结束IntentService onHandleIntent方法时遇到了一些麻烦。我正在捕捉一个异常,这对于线程的其他部分的执行至关重要,并且我想在捕获异常时停止线程。我试着调用stopSelf()或直接调用onDestroy(),我确信这可能是非常糟糕的做法,而且它们不起作用。线程调用它们然后继续。手动结束IntentService工作线程
任何人都有如何做到这一点好主意?
我在手动结束IntentService onHandleIntent方法时遇到了一些麻烦。我正在捕捉一个异常,这对于线程的其他部分的执行至关重要,并且我想在捕获异常时停止线程。我试着调用stopSelf()或直接调用onDestroy(),我确信这可能是非常糟糕的做法,而且它们不起作用。线程调用它们然后继续。手动结束IntentService工作线程
任何人都有如何做到这一点好主意?
正如sonykuba说,整理onHandleIntent方法停止服务。所以你可以这样做:
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在完成OnHandleIntent后自动停止。没有必要手动进行。方法的
读取的描述onHandleIntent()
谢谢,我认为这应该工作。 – gtdevel