2012-04-25 63 views
1

我對此很新,很抱歉,如果我的問題可能是微不足道的,我確定這是基本的東西,但我真的找不到解決方案。Android活動Autorefresh/

我想在活動活動中實現autorefresh。我有一個在後臺運行的BT服務,需要通過mHandler確認一些接收到的數據。如果它收到預期的結果,我想改變一個textview的字符串,現在我正在使用一個額外的按鈕,但它是最醜陋的方式。 所以我需要一個循環內的活動,但我應該使用什麼?哪個動作監聽器?

回答

-1

也許你可以嘗試一個線程內循環

boolean update = false; // control the state to update textview 
    new Thread(new Runnable(){ 
     void run(){ 
      while(true){ 
       if(!update){ 
        ... 
        textview.setText("something."); 
        ... 
        update = true; 
       } 
      } 
     } 
    }.start(); 
+0

這不會工作,因爲TextView的是由活動線程 – BobbelKL 2012-04-25 15:28:56

0

確定你所能做的就是,你可以創建你的活動廣播reciever。在你寫的代碼中創建。修復它實際上我在家,所以你需要修復它我給你的想法。

BroadcastReciever broadcast_obj = new BroadcastReciever (
         @overrieded 
         onRecieve(Intent intent) { 
           String action = intent.getAction(); 
           if(action == "my_bt_action") { 
           //UPDATE YOUR TEXTVIEW AND DO WHATEVER WORK YOU WANT. 
           } 
          }); 


Now you need to register your broadcast for that just put these line in your oncreate after creating Broadcast reciever which we have just done. 

registerREciever(broadcast_obj, new IntentFilter("my_bt_action"); 

now you need to send your broadcast when your service will perform your calculation or your task for that it is simple. 

Intent intent = new Intent (getApplictionContext,"my_bt_action"); 
sendBroadcast(intent); 

from above code you can easily communicate between your activity and service. 

hope it will work. 
+0

擁有這似乎是正確的,我以後會嘗試一下。 onReceive是方法:) – BobbelKL 2012-04-25 15:29:44

+0

如果它是正確的,那麼標記爲答案,以便其他人不應該浪費時間在它上面,這對其他正在搜索的人有幫助 – 2012-04-25 15:33:39

+0

我再次觀看。 我的活動是設置服務,所以他們已經在溝通。 我的活動開始了,連接到設備,我發送了一些東西並等待答案,如果答案進來了,它應該刷新並顯示一個已更改的文本視圖 – BobbelKL 2012-04-25 16:34:46