Android 手动结束IntentService工作线程
手动结束IntentService onHandleIntent方法时遇到一些问题。我正在捕获一个异常,它对线程其余部分的执行至关重要,我想在捕获异常时停止线程。我尝试调用stopSelf()或直接调用onDestroy(),我确信这可能是非常糟糕的方法,但它们不起作用。线程调用它们,然后继续Android 手动结束IntentService工作线程,android,multithreading,intentservice,Android,Multithreading,Intentservice,手动结束IntentService onHandleIntent方法时遇到一些问题。我正在捕获一个异常,它对线程其余部分的执行至关重要,我想在捕获异常时停止线程。我尝试调用stopSelf()或直接调用onDestroy(),我确信这可能是非常糟糕的方法,但它们不起作用。线程调用它们,然后继续 有人知道如何做吗?完成手头的内容后,IntentService会自动停止。不需要手动操作 阅读方法的描述,正如sonykuba所说,完成OnHandleContent方法会停止服务。所以你可以这样做: p
有人知道如何做吗?完成手头的内容后,IntentService会自动停止。不需要手动操作
阅读方法的描述,正如sonykuba所说,完成OnHandleContent方法会停止服务。所以你可以这样做:
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
}
谢谢,我知道这一点。但我想知道我是否可以通过手动方法提前停止它。您知道这是否可行吗?请尝试调用stopService(),但这可以从启动服务的线程中使用。这除了调用onDestroy之外什么都不做