update ui from another thread in android

11,128

Solution 1

You can't call UI methods from threads other than the main thread. You should use Activity#runOnUiThread() method.

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

        tv = (TextView) findViewById(R.id.textView1);
        tv.setText("new world");
        MyFileObserver myFileObserver = new MyFileObserver("/sdcard/", this);
        myFileObserver.startWatching();
    }

    String mySTR = "";
    TextView tv ;

    public void event(String absolutePath,String path,int event)
    {
        runOnUiThread(action);
    }

    private Runnable action = new Runnable() {
        @Override
        public void run() {
            mySTR = absolutePath+path+"\t"+event;
            tv.setText(mySTR);
        }
    };
}

public class MyFileObserver extends FileObserver 
{
    public String absolutePath;
    FileObserverActivity fileobserveractivity;

    public MyFileObserver(String path,FileObserverActivity foa) 
    {
        super(path, FileObserver.ALL_EVENTS);
        absolutePath = path;
        fileobserveractivity = foa;
    }

    @Override
    public void onEvent(int event, String path) 
    {
        if (path == null) 
        {
            return;
        }
        else if(event!=0)
        {
            fileobserveractivity.event(absolutePath, path, event);
        }
        else
        {
            return;
        }
    }
}

Solution 2

Try as follows

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

        tv = (TextView) findViewById(R.id.textView1);
        tv.setText("new world");
        MyFileObserver myFileObserver = new MyFileObserver("/sdcard/", this);
        myFileObserver.startWatching();
        registerReceiver(onBroadcast,new IntentFilter("abcd"));

    }

    String mySTR = "";
    TextView tv ;

    public void event(String absolutePath,String path,int event)
    {
        mySTR = absolutePath+path+"\t"+event;
            tv.setText(mySTR);  // program crash here!
    }
    @Override
    protected void onDestroy() {

        super.onDestroy();
        unregisterReceiver(onBroadcast);
    }

      private final BroadcastReceiver onBroadcast = new BroadcastReceiver() {


        @Override
        public void onReceive(Context ctxt, Intent i) {
            // do stuff to the UI

            event(absolutePath, path, event);
        }
    };
}


public class MyFileObserver extends FileObserver 
{
    public String absolutePath;
    FileObserverActivity fileobserveractivity;

    public MyFileObserver(String path,FileObserverActivity foa) 
    {
        super(path, FileObserver.ALL_EVENTS);
        absolutePath = path;
        fileobserveractivity = foa;
    }

    @Override
    public void onEvent(int event, String path) 
    {
        if (path == null) 
        {
            return;
        }
        else if(event!=0)
        {
             context.sendBroadcast(new Intent("abcd"));
            //fileobserveractivity.event(absolutePath, path, event);
        }
        else
        {
            return;
        }
    }
}
Share:
11,128
M.Movaffagh
Author by

M.Movaffagh

Updated on August 12, 2022

Comments

  • M.Movaffagh
    M.Movaffagh over 1 year

    i want to change UI in android.

    my main class make a second class then second class call a method of main class.method in main class should update UI but program crash in runtime.

    what should i do?

    my main class :

    public class FileObserverActivity extends Activity 
    {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            tv = (TextView) findViewById(R.id.textView1);
            tv.setText("new world");
            MyFileObserver myFileObserver = new MyFileObserver("/sdcard/", this);
            myFileObserver.startWatching();
        }
    
        String mySTR = "";
        TextView tv ;
    
        public void event(String absolutePath,String path,int event)
        {
            mySTR = absolutePath+path+"\t"+event;
                tv.setText(mySTR);  // program crash here!
        }
    }
    

    and my second class :

    public class MyFileObserver extends FileObserver 
    {
        public String absolutePath;
        FileObserverActivity fileobserveractivity;
    
        public MyFileObserver(String path,FileObserverActivity foa) 
        {
            super(path, FileObserver.ALL_EVENTS);
            absolutePath = path;
            fileobserveractivity = foa;
        }
    
        @Override
        public void onEvent(int event, String path) 
        {
            if (path == null) 
            {
                return;
            }
            else if(event!=0)
            {
                fileobserveractivity.event(absolutePath, path, event);
            }
            else
            {
                return;
            }
        }
    }
    
  • M.Movaffagh
    M.Movaffagh over 12 years
    whats context ? 9 line to end?
  • M.Movaffagh
    M.Movaffagh over 12 years
    thansk...it s worked.with ContextWrapper context = new ContextWrapper(fileobserveractivity);