为Android睡眠命令 [英] Sleep command for Android
问题描述
我写一个小程序来定期轮询WIFI连接的RSSI。
我使用 SystemClock.Sleep(2000)
在程序中。
I am writing a small program to periodically poll the RSSI of the WIFI connection.
I am using SystemClock.Sleep(2000)
in the program.
问题,我想显示RSSI每2秒。但是,目前,尽管它轮询每2秒,只在循环结束时显示的结果。
The problem, I would like to display the RSSI every 2 seconds. But, currently, even though it polls every 2 seconds, the result is displayed only at the end of the loop.
下面是code片断:
for(int i=0;i<10;i++)
{
Date dt=new Date();
WifiInfo info = wifi.getConnectionInfo();
int rssi = info.getRssi();
textStatus.append("\n\nRSSI :" +Integer.toString(rssi));
SystemClock.sleep(2000);
}
会很高兴,如果你有一些建议。
Would be glad, if you have some suggestion.
问候
基兰
推荐答案
不要在UI线程使用的睡眠。
Don't use sleep in the UI thread.
执行以下操作来代替:
- 创建一个的MessageHandler(android.os.Handler)处理显示消息(
textStatus.append(...)
) - 创建一个运行的循环包含睡眠工作线程
- 现在,工作线程不能直接更新textStatus。代替从工作线程消息处理程序发送一个消息。
新增:
下面是一个有用的链接,可以帮助你:
Here is a useful link that might help you:
请参见在UI线程处理昂贵的操作
See section "Handling Expensive Operations in the UI Thread"
http://developer.android.com/guide/appendix/常见问题/ commontasks.html#线程
这篇关于为Android睡眠命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!