欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程中的消息機制實例詳解

 更新時間:2016年01月05日 10:52:04   作者:liuhe688  
這篇文章主要介紹了Android編程中的消息機制,結(jié)合實例形式較為詳細的分析了Android中消息機制的原理,注意事項與相關(guān)使用技巧,需要的朋友可以參考下

本文實例講述了Android編程中的消息機制。分享給大家供大家參考,具體如下:

在分析Android消息機制之前,我們先來看一段代碼:

public class MainActivity extends Activity implements View.OnClickListener {
  private TextView stateText;
  private Button btn;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    stateText = (TextView) findViewById(R.id.tv);
    btn = (Button) findViewById(R.id.btn);
    btn.setOnClickListener(this);
  }
  @Override
  public void onClick(View v) {
    new WorkThread().start();
  }
  //工作線程
  private class WorkThread extends Thread {
    @Override
    public void run() {
      //......處理比較耗時的操作
      //處理完成后改變狀態(tài)
      stateText.setText("completed");
    }
  }
}

這段代碼似乎看上去很正常,但是當你運行時就會發(fā)現(xiàn),它會報一個致命性的異常:

ERROR/AndroidRuntime(421): FATAL EXCEPTION: Thread-8
ERROR/AndroidRuntime(421): android.view.ViewRoot$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.

到底是怎么回事呢?原因在于,Android系統(tǒng)中的視圖組件并不是線程安全的,如果要更新視圖,必須在主線程中更新,不可以在子線程中執(zhí)行更新的操作。
既然這樣,我們就在子線程中通知主線程,讓主線程做更新操作吧。那么,我們?nèi)绾瓮ㄖ骶€程呢?我們需要使用到Handler對象。
我們稍微修改一下上面的代碼:

public class MainActivity extends Activity implements View.OnClickListener {
  private static final int COMPLETED = 0;
  private TextView stateText;
  private Button btn;
  private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      if (msg.what == COMPLETED) {
        stateText.setText("completed");
      }
    }
  };
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    stateText = (TextView) findViewById(R.id.tv);
    btn = (Button) findViewById(R.id.btn);
    btn.setOnClickListener(this);
  }
  @Override
  public void onClick(View v) {
    new WorkThread().start();
  }
  //工作線程
  private class WorkThread extends Thread {
    @Override
    public void run() {
      //......處理比較耗時的操作
      //處理完成后給handler發(fā)送消息
      Message msg = new Message();
      msg.what = COMPLETED;
      handler.sendMessage(msg);
    }
  }
}

通過上面這種方式,我們就可以解決線程安全的問題,把復雜的任務處理工作交給子線程去完成,然后子線程通過handler對象告知主線程,由主線程更新視圖,這個過程中消息機制起著重要的作用。

下面,我們就來分析一下Android中的消息機制。

熟悉Windows編程的朋友知道Windows程序是消息驅(qū)動的,并且有全局的消息循環(huán)系統(tǒng)。Google參考了Windows的消息循環(huán)機制,也在Android系統(tǒng)中實現(xiàn)了消息循環(huán)機制。Android通過Looper、Handler來實現(xiàn)消息循環(huán)機制。Android的消息循環(huán)是針對線程的,每個線程都可以有自己的消息隊列和消息循環(huán)。

Android系統(tǒng)中的Looper負責管理線程的消息隊列和消息循環(huán)。通過Looper.myLooper()得到當前線程的Looper對象,通過Looper.getMainLooper()得到當前進程的主線程的Looper對象。

前面提到,Android的消息隊列和消息循環(huán)都是針對具體線程的,一個線程可以存在一個消息隊列和消息循環(huán),特定線程的消息只能分發(fā)給本線程,不能跨線程和跨進程通訊。但是創(chuàng)建的工作線程默認是沒有消息隊列和消息循環(huán)的,如果想讓工作線程具有消息隊列和消息循環(huán),就需要在線程中先調(diào)用Looper.prepare()來創(chuàng)建消息隊列,然后調(diào)用Looper.loop()進入消息循環(huán)。下面是我們創(chuàng)建的工作線程:

class WorkThread extends Thread {
   public Handler mHandler;
   public void run() {
     Looper.prepare();
     mHandler = new Handler() {
       public void handleMessage(Message msg) {
         // 處理收到的消息
       }
     };
     Looper.loop();
   }
}

這樣一來,我們創(chuàng)建的工作線程就具有了消息處理機制了。
那么,為什么前邊的示例中,我們怎么沒有看到Looper.prepare()和Looper.loop()的調(diào)用呢?原因在于,我們的Activity是一個UI線程,運行在主線程中,Android系統(tǒng)會在Activity啟動時為其創(chuàng)建一個消息隊列和消息循環(huán)。
前面提到最多的是消息隊列(MessageQueue)和消息循環(huán)(Looper),但是我們看到每個消息處理的地方都有Handler的存在,它是做什么的呢?Handler的作用是把消息加入特定的Looper所管理的消息隊列中,并分發(fā)和處理該消息隊列中的消息。構(gòu)造Handler的時候可以指定一個Looper對象,如果不指定則利用當前線程的Looper對象創(chuàng)建。下面是Handler的兩個構(gòu)造方法:

/**
* Default constructor associates this handler with the queue for the
* current thread.
*
* If there isn't one, this handler won't be able to receive messages.
*/
public Handler() {
    if (FIND_POTENTIAL_LEAKS) {
      final Class<? extends Handler> klass = getClass();
      if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
          (klass.getModifiers() & Modifier.STATIC) == 0) {
        Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
          klass.getCanonicalName());
      }
    }
    mLooper = Looper.myLooper();
    if (mLooper == null) {
      throw new RuntimeException(
        "Can't create handler inside thread that has not called Looper.prepare()");
    }
    mQueue = mLooper.mQueue;
    mCallback = null;
}
/**
* Use the provided queue instead of the default one.
*/
public Handler(Looper looper) {
    mLooper = looper;
    mQueue = looper.mQueue;
    mCallback = null;
}

下面是消息機制中幾個重要成員的關(guān)系圖:

一個Activity中可以創(chuàng)建出多個工作線程,如果這些線程把他們消息放入Activity主線程的消息隊列中,那么消息就會在主線程中處理了。因為主線程一般負責視圖組件的更新操作,對于不是線程安全的視圖組件來說,這種方式能夠很好的實現(xiàn)視圖的更新。

那么,子線程如何把消息放入主線程的消息隊列中呢?只要Handler對象以主線程的Looper創(chuàng)建,那么當調(diào)用Handler的sendMessage方法,系統(tǒng)就會把消息主線程的消息隊列,并且將會在調(diào)用handleMessage方法時處理主線程消息隊列中的消息。

對于子線程訪問主線程的Handler對象,你可能會問,多個子線程都訪問主線程的Handler對象,發(fā)送消息和處理消息的過程中會不會出現(xiàn)數(shù)據(jù)的不一致呢?答案是Handler對象不會出現(xiàn)問題,因為Handler對象管理的Looper對象是線程安全的,不管是添加消息到消息隊列還是從消息隊列中讀取消息都是同步保護的,所以不會出現(xiàn)數(shù)據(jù)不一致現(xiàn)象。

深入理解Android消息處理機制對于應用程序開發(fā)非常重要,也可以讓我們對線程同步有更加深刻的認識,希望這篇文章可以對朋友們有所幫助。

希望本文所述對大家Android程序設計有所幫助。

相關(guān)文章

最新評論