android多线程与界面交互以及runOnUiThread简单应用

转自:http://blog.csdn.net/keyboard_workers/article/details/7620566

首先runOnUiThread是Activity内部的方法,在运用的时候最好指明当前环境变(Context).

new Thread(new Runnable() {

   @Override
   public void run() {
    System.out.println(Thread.currentThread().getId());
    runOnUiThread(new Runnable() {

     @Override
     public void run() {
      Toast.makeText(RunOnUIThreadActivity.this, "UI操作...", 1000).show(); 
     }
    });
   }

  }).start();

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
        new Thread(new Runnable() {

      @Override
      public void run() {
       if(isClose)
        return;
       if(i!=0)
       Toast.makeText(RunOnUIThreadActivity.this, i+"", 1000).show(); 
       i++;
       handler.postDelayed(this, 2000);
      }

     }).start(); 
       }

上面两个其实原理一样,runOnUiThread这个会调用父类中的

public final void runOnUiThread(Runnable action) {
        if (Thread.currentThread() != mUiThread) {
            mHandler.post(action);
        } else {
            action.run();
        }
    }

首先判断是否是UI线程,不是的话就post,如果是的话就正常运行该线程.

只要经过主线程中的Handler.post或者postDelayed处理线程runnable则都可以将其转为UI主线程.再说Handler的机制就是来处理线程与UI通讯的.



三、Android多线程与界面交互的办法


1.Activity.runOnUIThread(Runnable)


 2.View.post(Runnable),View.postDelay(Runnable,long)


 3、Handler


 4、AsyncTask


 下面用一个工程实现了上方四种交互办法




public class MainActivity extends Activity {  

private TextView txView;
private Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i("RootyInfo", "oncreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

txView=(TextView)findViewById(R.id.textView1);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

//创建一个用于显现前三种后台线程和UI线程交互的线程
new TestThread(MainActivity.this).start();

//创建一个用于显现AsyncTask实现交互的TestAsyncTask
new TestAsyncTask().execute("Test"," AsyncTask");
}
});
}


class TestAsyncTask extends AsyncTask<String, Integer, String>
{
//TestAsyncTask被后台线程履行后,被UI线程被调用,一般用于初始化界面控件,如进度条
@Override
protected void onPreute() {
// TODO Auto-generated method stub
super.onPreute();
}

//doInBackground履行完后由UI线程调用,用于更新界面操纵
@Override
protected void onPostute(String result) {
// TODO Auto-generated method stub
txView.setText(result);
super.onPostute(result);
}

//在PreExcute履行后被启动AysncTask的后台线程调用,将成果返回给UI线程
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
StringBuffer sb=new StringBuffer();
for (String string : params) {
sb.append(string);
}
return sb.toString();
}

}
//用于线程间通信的Handler
class TestHandler extends Handler
{

public TestHandler(Looper looper) {
super(looper);
// TODO Auto-generated constructor stub
}

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
System.out.println("123");
txView.setText((String)msg.getData().get("tag"));
super.handleMessage(msg);
}

}
//后台线程类
class TestThread extends Thread
{
Activity activity;
public TestThread(Activity activity) {

this.activity = activity;
}
@Override
public void run() {

//下面代码用来演示Activity.runOnUIThread(Runnable)办法的实现
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
txView.setText("Test runOnUIThread");
}
});

//下面代码用来演示Activity.runOnUIThread(Runnable)办法的实现
txView.post(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
txView.setText("Test View.post(Runnable)");
}
});

//下面代码用来演示Activity.runOnUIThread(Runnable)办法的实现
txView.postDelayed(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
txView.setText("Test View.postDelay(Runnable,long)");
}
}, 1000);

//下面代码用来演示Handler办法的实现
Message msg=new Message();
Bundle bundle=new Bundle();
bundle.putString("tag", "Test Handler");
msg.setData(bundle);
new TestHandler(Looper.getMainLooper()).sendMessage(msg);

super.run();
}

}

}


你可能感兴趣的:(android多线程与界面交互以及runOnUiThread简单应用)