从android中的另一个线程更新ui

从android中的另一个线程更新ui,第1张

从android中的另一个线程更新ui

您不能从主线程以外的线程调用UI方法。您应该使用Activity#runOnUiThread()方法。

public class FileObserverActivity extends Activity {        @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;        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5033939.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存