我對此很新,很抱歉,如果我的問題可能是微不足道的,我確定這是基本的東西,但我真的找不到解決方案。Android活動Autorefresh/
我想在活動活動中實現autorefresh。我有一個在後臺運行的BT服務,需要通過mHandler確認一些接收到的數據。如果它收到預期的結果,我想改變一個textview的字符串,現在我正在使用一個額外的按鈕,但它是最醜陋的方式。 所以我需要一個循環內的活動,但我應該使用什麼?哪個動作監聽器?
我對此很新,很抱歉,如果我的問題可能是微不足道的,我確定這是基本的東西,但我真的找不到解決方案。Android活動Autorefresh/
我想在活動活動中實現autorefresh。我有一個在後臺運行的BT服務,需要通過mHandler確認一些接收到的數據。如果它收到預期的結果,我想改變一個textview的字符串,現在我正在使用一個額外的按鈕,但它是最醜陋的方式。 所以我需要一個循環內的活動,但我應該使用什麼?哪個動作監聽器?
也許你可以嘗試一個線程內循環
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();
確定你所能做的就是,你可以創建你的活動廣播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.
這不會工作,因爲TextView的是由活動線程 – BobbelKL 2012-04-25 15:28:56