runOnUiThread和在子线程运行

来源:互联网 发布:网络环境搭建 编辑:程序博客网 时间:2024/06/05 19:43
// A instance of Activity 
       .... 
       . 
    runOnUiThread(new Runnable() { 
        public void run() 
        { 
            TextView textBox= (TextView)getViewById(R.id.textbox); 
            textBox.setText(R.string.login_failed); 
        } 

    }); 



内部实现其实就是调用的hander.post();


// Reference from Activity.java 
/** 
* Runs the specified action on the UI thread. If the current thread is the UI 
* thread, then the action is executed immediately. If the current thread is 
* not the UI thread, the action is posted to the event queue of the UI thread. 

* @param action the action to run on the UI thread 
*/ 
public final void runOnUiThread(Runnable action) { 
    if (Thread.currentThread() != mUiThread) { 
        mHandler.post(action); 
    } else { 
        action.run(); 
    } 
}






0 0