UI不会实时更新

时间:2012-02-02 00:51:44

标签: android user-interface real-time

我试图测量wifi信号强度5次(每秒后)&在TextView中显示它。我同时将它写入外部存储器。 一切运行正常,但我无法实时查看结果。应用程序将以空白屏幕运行5秒,然后显示结果(这是正确的btw,即5种不同每秒后的读数)。 我想在for循环的每次迭代中计算新值后立即看到结果更新。 谢谢 这是代码

public class WifiDemo extends Activity implements OnClickListener {
private static final String TAG = "WiFiDemo";
WifiManager wifi;
TextView textStatus;
Button buttonScan;

/** Called when the activity is first created. */
/*
 * (non-Javadoc)
 * 
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Setup UI
    textStatus = (TextView) findViewById(R.id.textStatus);
    buttonScan = (Button) findViewById(R.id.buttonScan);
    buttonScan.setOnClickListener(this);

    // Setup WiFi
    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
            // Get WiFi status
            runOnUiThread(new Runnable() {
                public void run() {
                    try {
                    FileWriter fw = new FileWriter(
                            Environment.getExternalStorageDirectory()
                                    + "/bluetooth/wifi.txt");

                    for (int i = 0; i < 5; i++) {
                        WifiInfo info = wifi.getConnectionInfo();
                        Date d = new Date(System.currentTimeMillis());
                        String stat = "\n\nWiFi Status: " + info.getRssi()
                                + " " + d.getHours() + ":" + d.getMinutes()
                                + ":" + d.getSeconds();
                        textStatus.append(stat);
                        fw.write(stat);
                        fw.flush();
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }


                    fw.close();

                }
                     catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                            textStatus.append("something wrong");
                        }

            }
        });
    }
}
}

2 个答案:

答案 0 :(得分:1)

您可以尝试创建处理程序来处理主线程中的UI更新任务。不要更新线程中的UI,而是通过传递处理程序消息来确保在主线程中处理此作业。这对我来说可以。我在这里修改了一些代码(我删除了写文件部分),

public class WifiDemo extends Activity implements OnClickListener {

private static final String TAG = "WiFiDemo";
private static final int WifiDetectStart = 0;
private static final int WifiDetectStop = 1;
private String stat;
WifiManager wifi;
TextView textStatus;
Button buttonScan;
Handler handler;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

 // Setup UI
    textStatus = (TextView) findViewById(R.id.textStatus);
    buttonScan = (Button) findViewById(R.id.buttonScan);
    buttonScan.setOnClickListener(this);

 //setup handler

    handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {

            if(msg.what == WifiDetectStart)
            {
                textStatus.append(stat);
            }

            if(msg.what == WifiDetectStop)
            {
                Thread.currentThread().interrupt();
            }

            super.handleMessage(msg);
        }  
       };

 // Setup WiFi
    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
            // Get WiFi status

            Thread myThread = new Thread(new Runnable() {
                public void run() {
                    for (int i = 0; i < 5; i++) {
                        WifiInfo info = wifi.getConnectionInfo();
                        Date d = new Date(System.currentTimeMillis());
                        stat = "\n\nWiFi Status: " + info.getRssi()
                                + " " + d.getHours() + ":" + d.getMinutes()
                                + ":" + d.getSeconds();
                        Message msg = new Message();
                        msg.what = WifiDetectStart;
                        handler.sendMessage(msg);
//                      textStatus.append(stat);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                    //finish this operation
                    Message msg = new Message();
                    msg.what = WifiDetectStop;
                    handler.sendMessage(msg);
                }
        });
            myThread.start();
    }

}

答案 1 :(得分:1)

问题是你正在尝试在单独的Runnable中进行更新......但是,你的Runnable在UI线程中运行,因此导致UI线程处于循环中(包括{ {1}})。您没有收到更新,因为您导致用户界面等待您。

如果您的处理相当繁重,您可能希望将其分解为单独的线程并将消息发送给处理程序。否则,最简单的方法是执行以下操作(未经测试,但类似的东西):

Thread.sleep()