2011-06-21 22 views
0

我有一個客戶端軟件(在Android上),它監聽傳入的消息。消息在等待消息到來的while循環中被接收。當找到消息時,它會更新GUI。 [由於在Android中,GUI不能直接更新]調用線程來執行此操作。我的問題是,如果有許多消息,結果在許多線程!它造成了一個笨拙的情況。我的抽象代碼是,如何正確清潔未使用的線程?

My_Client() 
{ 
    send_text_function() // My question is not about it 
    in_a_thread_Call_receive_from_others_function() [see 1] 
} 

receiving_funtion() // [this function is mentioned above as (see 1), called in a thread] 
{ 
    while(waiting for new message) 
    { 
    >>A new message found >> create a thread to update the GUI. // << Here is my question. see 2 
    //android.os.Handler.thread type thread! 
    } 
} 

標籤2:現在每次有消息時都會創建此線程。我怎麼才能創建一個線程,並一次又一次地使用它?任何想法?

回答

1
  1. 創建新的Thread
  2. 在線程的run()方法中創建一個新的Handler
  3. 當您想要在目標線程上執行某些操作時,請使用處理程序的post()方法。

您可以在主線程上創建一個Handler來更新GUI的回退操作。

另請考慮使用AsyncTask<>