1

我对此很陌生,如果我的问题可能微不足道,我很抱歉,我敢肯定这是基本的东西,但我真的找不到解决方案。

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

4

2 回答 2

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.
于 2012-04-25T15:21:15.817 回答
-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();
于 2012-04-25T15:08:54.230 回答