2011-08-24 30 views
2

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

任何人都有如何做到这一点好主意?

回答

2

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

谢谢,我认为这应该工作。 – gtdevel

1

IntentService在完成OnHandleIntent后自动停止。没有必要手动进行。方法的

读取的描述onHandleIntent()

+1

谢谢,我知道这一点。但我想知道我是否可以通过手动方法过早阻止它。你知道这可能吗? – gtdevel

+0

尝试尝试调用stopService(),但这可用于启动服务的线程。 –

+0

这不是做任何事情,而是调用onDestroy – urSus