Android活动自动刷新/

时间:2012-04-25 14:56:45

标签: android android-activity

我对此很新,很抱歉,如果我的问题可能是微不足道的,我确信这是基本的东西,但实际上我找不到解决方案。

我想在活跃的活动中实现自动刷新。我有一个在后台运行的BT服务,需要通过mHandler确认一些收到的数据。如果收到预期的我想要更改textview的字符串,现在我正在使用一个额外的按钮,但这是最丑陋的方式。 所以我需要在活动中使用循环,但我应该使用什么?哪个动作听众?

2 个答案:

答案 0 :(得分:0)

好的,您可以在活动中创建广播接收器。在你的oncreate中写下这段代码。解决它实际上我在家里所以你需要解决它我给你的想法。

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.

答案 1 :(得分:-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();