2012-04-25 60 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