2013-03-13 63 views
3

我的用例是,無論何時用戶鍵入某個EditText,輸入數據都會用於在後臺執行操作。這些操作可能需要足夠長的時間才能導致ANR。如果操作足夠準確,@TextChange和@Background一起工作正常。但是操作需要足夠長的時間,以便用戶輸入更多的數據,我將遇到線程問題,因爲將有多個後臺任務將命令更新相同的UI組件。如何使用AndroidAnnotations取消@Background任務

我想我通過AsyncTask API實現了想要的行爲,但也想尋找基於AndroidAnnotations的解決方案,因爲它簡化了代碼。順便提一下,

下面是一些代碼片段,希望能夠說明我的觀點。感謝您至少閱讀,評論/回答感謝:)

@TextChange 
void onUserInput(...) { 
    // This will start a new thread on each text change event 
    // thus leading to a situation that the thread finishing 
    // last will update the ui 
    // the operation time is not fixed so the last event is 
    // not necessary the last thread that finished 
    doOperation() 
} 

@Background 
void doOperation() { 
    // Sleep to simulate long taking operation 
    Thread.sleep(6000); 
    updateUi() 
} 

@UiThread 
void updateUi() { 
    // Update text field etc content based on operations 
} 

UPDATE:這是不可能的那一刻,見下文天」的答案。

回答

4

從AA 3.0開始可能。閱讀thread

+0

感謝關於AA進度的更新。我會將你的答案設定爲接受。 – kaskelotti 2014-02-04 10:02:20

+0

歡迎。只是爲了完整性,最後一個版本是3.0.1。在android上保持搖滾! – mircobabini 2014-02-05 11:42:20

1

已經有this kind of request on Android Annotations但由於沒有提出解決方案而被關閉。但是,如果您對此有任何瞭解,請繼續並重新開放此問題;)

+0

感謝您的回覆。 API的內部工作對我來說不是很清楚,因此目前無法提出解決方案。我會考慮它:)無論如何,在STO中記錄這個問題也很好。 – kaskelotti 2013-03-13 11:55:48

+0

我標記了你的回答是正確的,因爲這表明目前這是不可能的。 – kaskelotti 2013-03-13 11:58:27