0

Hi i'm trying to update the UI from a background thread. I'm setting the selection of a Radio Button programetically but strangely it wouldnt update the UI when I run the app. But when I debug the application step by step the UI updates itself which is very strange to me. following is the code I am using to do it.

As post runnable returns a boolean and it seems to return true about the update but it doesnt update the UNI which is very strange.

Thread

_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();

Method for the Check

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();
        }
    }
Mr.Noob
  • 1,005
  • 3
  • 24
  • 58
  • How can u update UI from a background Thread? are you not getting any exceptions? – SKK Feb 08 '13 at 12:50
  • No. Well im calling post runnable in the background thread which is a UI thread and that does the update generally and it had worked for me. – Mr.Noob Feb 08 '13 at 12:54

1 Answers1

0

try this:

runOnUiThread(new Runnable() {
    public void run() {
        mParent.connectivity.setChecked(false);
    }
});
Alex
  • 591
  • 5
  • 8