2011-08-24 98 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