android - 手动结束 IntentService 工作线程

标签 android multithreading intentservice

我在手动结束 IntentService onHandleIntent 方法时遇到了一些问题。我正在捕获一个对线程其余部分的执行至关重要的异常,我想在捕获异常时停止线程。我尝试调用 stopSelf() 或直接调用 onDestroy(),我确信这可能是非常糟糕的方法,而且它们不起作用。线程调用它们然后继续。

有人知道如何做到这一点吗?

最佳答案

正如 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
}

关于android - 手动结束 IntentService 工作线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7173306/

相关文章:

java - 使用 queueEvent() 在渲染器和另一个类之间传递变量

java - 为什么 if 语句之后的语句在同步方法中没有按预期运行

android - 应用程序被杀死后保持 Activity Intent 服务

android - IntentService 未触发

android - Android 上的 OpenCV - 标题;没有那个文件/目录

java - 如何防止整个类被 proguard 修改?

android - 加载带有图像的 gridview 时出现 OutOfMemoryError

android - Paypal Android SDK 2.3.1 集成错误 APP 挂起

multithreading - "mutex"和 "atomic operation"之间有什么区别吗?

android - IntentService onHandleEvent() 未启动