我对此很新,很抱歉,如果我的问题可能是微不足道的,我确定这是基本的东西,但我真的找不到解决方案。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