您好我正在尝试从后台线程更新UI。我正在设置一个单选按钮的选择,但奇怪的是,当我运行应用程序时它不会更新UI。但是当我逐步调试应用程序时,UI会自动更新,这对我来说非常奇怪。以下是我用来做的代码。
由于post runnable返回一个布尔值,它似乎对更新返回true但它不会更新非常奇怪的UNI。
主题
_internetConenctivityThread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try
{
CheckInternetConnectivity(conManager);
Thread.sleep(30000);
}
catch(Exception ex)
{
Log.d("_internetConnectivityThread", "Exception while checking connectivity: " + ex.getMessage());
}
}
});
_internetConenctivityThread.start();
检查方法
private void CheckInternetConnectivity(ConnectivityManager cm)
{
try
{
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
mParent.connectivity.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// Updating Status Monitor for connectivity
mParent.connectivity.setChecked(true);
}
});
}
else
{
mParent.connectivity.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// Updating Status Monitor for connectivity
mParent.connectivity.setChecked(false);
}
});
}
}
catch(Exception ex)
{
ex.getMessage();
}
}
答案 0 :(得分:0)
试试这个:
runOnUiThread(new Runnable() {
public void run() {
mParent.connectivity.setChecked(false);
}
});