2011-08-22 109 views
5

有誰知道是否有一種停止IntentService的方式,如果沒有它完成其工作線程並停止本身?停止IntentService

簡單的問題,但我找不到文檔中的答案。有沒有簡單的方法來阻止它?

謝謝

+0

'stopService(intent);'? – binnyb

回答

3

bevor消息到服務排隊onStartCommand被調用。它將消息轉發給排隊。所以你可以只覆蓋onStartCommand,這樣的事情:

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    if (intent.getAction().equals("Stop")) 
     stopSelf(); 
    onStart(intent, startId); 
    return START_NOT_STICKY; 
} 

歡呼

1

目前我偶然發現了這requierement爲應用我的工作。我將嘗試使用onStartCommand向Intent Service發送消息以停止工作(例如,設置布爾標誌stopWork = true),並在工作作業期間或下一個排隊的任務之前對其進行評估。 IntentService不會立即停止,但會跳過所有未完成的任務。希望能幫助到你。也可以自己嘗試一下。