Android 更新UI的两种方法——handler和runOnUiThread()

来源:互联网 发布:敦煌古筝淘宝经销商 编辑:程序博客网 时间:2024/05/15 13:36

Android 更新UI的两种方法——handler和runOnUiThread()
在Android开发过程中,常需要更新界面的UI。而更新UI是要主线程来更新的,即UI线程更新。如果在主线线程之外的线程中直接更新页面显示常会报错。抛出异常:android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
只有原始创建这个视图层次(view hierachy)的线程才能修改它的视图(view)
话不多说,贴出下面的代码
方法一:
在Activity.onCreate(Bundle savedInstanceState)中创建一个Handler类的实例, 在这个Handler实例的handleMessage回调函数中调用更新界面显示的函数。

界面:
[html]
public class MainActivity extends Activity {
private EditText UITxt;
private Button updateUIBtn;
private UIHandler UIhandler;

@Override  public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);      UITxt = (EditText)findViewById(R.id.ui_txt);      updateUIBtn = (Button)findViewById(R.id.update_ui_btn);      updateUIBtn.setOnClickListener(new View.OnClickListener() {          public void onClick(View v) {              // TODO Auto-generated method stub              UIhandler = new UIHandler();              UIThread thread = new UIThread();              thread.start();          }      });  }  @Override  public boolean onCreateOptionsMenu(Menu menu) {      getMenuInflater().inflate(R.menu.activity_main, menu);      return true;  }  private class UIHandler extends Handler{      @Override      public void handleMessage(Message msg) {          // TODO Auto-generated method stub          super.handleMessage(msg);          Bundle bundle = msg.getData();          String color = bundle.getString("color");          UITxt.setText(color);      }  }  private class UIThread extends Thread{      @Override      public void run() {          try {              Thread.sleep(3000);          } catch (InterruptedException e) {              // TODO Auto-generated catch block              e.printStackTrace();          }          Message msg = new Message();          Bundle bundle = new Bundle();          bundle.putString("color", "黄色");          msg.setData(bundle);          MainActivity.this.UIhandler.sendMessage(msg);      }  }  

}
更新后:www.2cto.com
方法二:利用Activity.runOnUiThread(Runnable)把更新ui的代码创建在Runnable中,然后在需要更新ui时,把这个Runnable对象传给Activity.runOnUiThread(Runnable)。 这样Runnable对像就能在ui程序中被调用。如果当前线程是UI线程,那么行动是立即执行。如果当前线程不是UI线程,操作是发布到事件队列的UI线程
[html]
FusionField.currentActivity.runOnUiThread(new Runnable()
{
public void run()
{
Toast.makeText(getApplicationContext(), , “Update My UI”,
Toast.LENGTH_LONG).show();
}

    });    

invalidate()和postInvalidate() 的区别及使用


Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型:Android UI操作并不是线程安全的,并且这些操作必须在UI线程中调用。

invalidate()是用来刷新View的,必须是在UI线程中进行工作。比如在修改某个view的显示时,调用invalidate()才能看到重新绘制的界面。invalidate()的调用是把之前的旧的view从主UI线程队列中pop掉。 一个Android 程序默认情况下也只有一个进程,但一个进程下却可以有许多个线程。

在这么多线程当中,把主要是负责控制UI界面的显示、更新和控件交互的线程称为UI线程,由于onCreate()方法是由UI线程执行的,所以也可以把UI线程理解为主线程。其余的线程可以理解为工作者线程。

invalidate()得在UI线程中被调动,在工作者线程中可以通过Handler来通知UI线程进行界面更新。


而postInvalidate()在工作者线程中被调用。


利用invalidate()刷新界面

  实例化一个Handler对象,并重写handleMessage方法调用invalidate()实现界面刷新;而在线程中通过sendMessage发送界面更新消息。

// 在onCreate()中开启线程new Thread(new GameThread()).start();、// 实例化一个handlerHandler myHandler = new Handler() {// 接收到消息后处理public void handleMessage(Message msg) {switch (msg.what) {case Activity01.REFRESH:mGameView.invalidate(); // 刷新界面break;}super.handleMessage(msg);}};class GameThread implements Runnable {public void run() {while (!Thread.currentThread().isInterrupted()) {Message message = new Message();message.what = Activity01.REFRESH;// 发送消息Activity01.this.myHandler.sendMessage(message);try {Thread.sleep(100);} catch (InterruptedException e) {Thread.currentThread().interrupt();}}}}

使用postInvalidate()刷新界面

使用postInvalidate则比较简单,不需要handler,直接在线程中调用postInvalidate即可。 
class GameThread implements Runnable {public void run() {while (!Thread.currentThread().isInterrupted()) {try {Thread.sleep(100);} catch (InterruptedException e) {Thread.currentThread().interrupt();}// 使用postInvalidate可以直接在线程中更新界面mGameView.postInvalidate();}}}
0 0