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

Android消息機(jī)制Handler的工作過程詳解

 更新時(shí)間:2017年01月16日 11:23:52   作者:無嘴小呆子  
這篇文章主要為大家詳細(xì)介紹了Android消息機(jī)制Handler的工作過程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

綜述

  在Android系統(tǒng)中,出于對(duì)性能優(yōu)化的考慮,對(duì)于Android的UI操作并不是線程安全的。也就是說若是有多個(gè)線程來操作UI組件,就會(huì)有可能導(dǎo)致線程安全問題。所以在Android中規(guī)定只能在UI線程中對(duì)UI進(jìn)行操作。這個(gè)UI線程是在應(yīng)用第一次啟動(dòng)時(shí)開啟的,也稱之為主線程(Main Thread),該線程專門用來操作UI組件,在這個(gè)UI線程中我們不能進(jìn)行耗時(shí)操作,否則就會(huì)出現(xiàn)ANR(Application Not Responding)現(xiàn)象。如果我們?cè)谧泳€程中去操作UI,那么程序就回給我們拋出異常。這是因?yàn)樵赩iewRootImpl中對(duì)操作UI的線程進(jìn)行檢查。如果操作UI的線程不是主線程則拋出異常(對(duì)于在檢查線程之前在非UI線程已經(jīng)操作UI組件的情況除外)。所以這時(shí)候我們?nèi)羰窃谧泳€程中更新UI的話可以通過Handler來完成這一操作。

Handler用法簡(jiǎn)介

  在開發(fā)中,我們對(duì)Handler的使用也基本上算是家常便飯了。在這里我們就簡(jiǎn)單的說一下Handler的幾種用法示例,就不在具體給出Demo進(jìn)行演示。在這里我們只針對(duì)后面這一種情形來看一下Handler的使用:在子線程完成任務(wù)后通過Handler發(fā)送消息,然后在主線程中去操作UI。
  一般來說我們會(huì)在主線程中創(chuàng)建一個(gè)Handler的匿名內(nèi)部類,然后重寫它的handleMessage方法來處理我們的UI操作。代碼如下所示。

private Handler mHandler = new Handler(){
  @Override
  public void handleMessage(Message msg) {
    switch (msg.what){
      //根據(jù)msg.what的值來處理不同的UI操作
      case WHAT:
        break;
      default:
        super.handleMessage(msg);
        break;
    }

  }
};

  我們還可以不去創(chuàng)建一個(gè)Handler的子類對(duì)象,直接去實(shí)現(xiàn)Handler里的CallBack接口,Handler通過回調(diào)CallBack接口里的handleMessage方法從而實(shí)現(xiàn)對(duì)UI的操作。

private Handler mHandler = new Handler(new Handler.Callback() {
  @Override
  public boolean handleMessage(Message msg) {

    return false;
  }
});

  然后我們就可以在子線程中發(fā)送消息了。

new Thread(new Runnable() {
  @Override
  public void run() {
    //子線程任務(wù)
    ...
    //發(fā)送方式一 直接發(fā)送一個(gè)空的Message
    mHandler.sendEmptyMessage(WHAT);
    //發(fā)送方式二 通過sendToTarget發(fā)送
    mHandler.obtainMessage(WHAT,arg1,arg2,obj).sendToTarget();
    //發(fā)送方式三 創(chuàng)建一個(gè)Message 通過sendMessage發(fā)送
    Message message = mHandler.obtainMessage();
    message.what = WHAT;
    mHandler.sendMessage(message);
  }
}).start();

  在上面我們給出了三種不同的發(fā)送方式,當(dāng)然對(duì)于我們還可以通過sendMessageDelayed進(jìn)行延時(shí)發(fā)送等等。如果我們的Handler只需要處理一條消息的時(shí)候,我們可以通過post一系列方法進(jìn)行處理。

private Handler mHandler = new Handler();

new Thread(new Runnable() {
  @Override
  public void run() {
    mHandler.post(new Runnable() {
      @Override
      public void run() {
        //UI操作
        ...
      }
    });
  }
}).start();

  在Handler中處理UI操作時(shí),上面的Handler對(duì)象必須是在主線程創(chuàng)建的。如果我們想在子線程中去new一個(gè)Handler對(duì)象的話,就需要為Handler指定Looper。

private Handler mHandler;

new Thread(new Runnable() {
  @Override
  public void run() {
    mHandler = new Handler(Looper.getMainLooper()){
      @Override
      public void handleMessage(Message msg) {
        super.handleMessage(msg);
        //UI操作
        ...
      }
    };

  }
}).start();

  對(duì)于這個(gè)Looper是什么,下面我們會(huì)詳細(xì)介紹。對(duì)于Handler的使用依然存在一個(gè)問題,由于我們創(chuàng)建的Handler是一個(gè)匿名內(nèi)部類,他會(huì)隱式的持有外部類的一個(gè)對(duì)象(當(dāng)然內(nèi)部類也是一樣的),而往往在子線程中是一個(gè)耗時(shí)的操作,而這個(gè)線程也持有Handler的引用,所以這個(gè)子線程間接的持有這個(gè)外部類的對(duì)象。我們假設(shè)這個(gè)外部類是一個(gè)Activity,而有一種情況就是我們的Activity已經(jīng)銷毀,而子線程仍在運(yùn)行。由于這個(gè)線程持有Activity的對(duì)象,所以,在Handler中消息處理完之前,這個(gè)Activity就一直得不到回收,從而導(dǎo)致了內(nèi)存泄露。如果內(nèi)存泄露過多,則會(huì)導(dǎo)致OOM(OutOfMemory),也就是內(nèi)存溢出。那么有沒有什么好的解決辦法呢?
  我們可以通過兩種方案來解決,第一種方法我們?cè)贏ctivity銷毀的同時(shí)也殺死這個(gè)子線程,并且將相對(duì)應(yīng)的Message從消息隊(duì)列中移除;第二種方案則是我們創(chuàng)建一個(gè)繼承自Handler的靜態(tài)內(nèi)部類。因?yàn)殪o態(tài)內(nèi)部類不會(huì)持有外部類的對(duì)象??墒沁@時(shí)候我們無法去訪問外部類的非靜態(tài)的成員變量,也就無法對(duì)UI進(jìn)行操作。這時(shí)候我們就需要在這個(gè)靜態(tài)內(nèi)部類中使用弱引用的方式去指向這個(gè)Activity對(duì)象。下面我們看一下示例代碼。

static class MyHandler extends Handler{
  private final WeakReference<MyActivity> mActivity;

  public MyHandler(MyActivity activity){
    super();
    mActivity = new WeakReference<MyActivity>(activity);
  }

  @Override
  public void handleMessage(Message msg) {
    MyActivity myActivity = mActivity.get();
    if (myActivity!=null){
      myActivity.textView.setText("123456789");
    }
  }
}

Handler工作過程

  在上面我們簡(jiǎn)單的說明了Handler是如何使用的。那么現(xiàn)在我們就來看一下這個(gè)Handler是如何工作的。在Android的消息機(jī)制中主要是由Handler,Looper,MessageQueue,Message等組成。而Handler得運(yùn)行依賴后三者。那么我們就來看一下它們是如何聯(lián)系在一起的。

Looper

  在一個(gè)Android應(yīng)用啟動(dòng)的時(shí)候,會(huì)創(chuàng)建一個(gè)主線程,也就是UI線程。而這個(gè)主線程也就是ActivityThread。在ActivityThread中有一個(gè)靜態(tài)的main方法。這個(gè)main方法也就是我們應(yīng)用程序的入口點(diǎn)。我們來看一下這個(gè)main方法。

public static void main(String[] args) {

  ......

  Looper.prepareMainLooper();

  ActivityThread thread = new ActivityThread();
  thread.attach(false);

  ......

  Looper.loop();

  throw new RuntimeException("Main thread loop unexpectedly exited");
}

  在上面代碼中通過prepareMainLooper方法為主線程創(chuàng)建一個(gè)Looper,而loop則是開啟消息循環(huán)。從上面代碼我們可以猜想到在loop方法中應(yīng)該存在一個(gè)死循環(huán),否則給我們拋出RuntimeException。也就是說主線程的消息循環(huán)是不允許被退出的。下面我們就來看一下這個(gè)Looper類。
  首先我們看一下Looper的構(gòu)造方法。

private Looper(boolean quitAllowed) {
  mQueue = new MessageQueue(quitAllowed);
  mThread = Thread.currentThread();
}

  在這個(gè)構(gòu)造方法中創(chuàng)建了一個(gè)消息隊(duì)列。并且保存當(dāng)前線程的對(duì)象。其中quitAllowed參數(shù)表示是否允許退出消息循環(huán)。但是我們注意到這個(gè)構(gòu)造方法是private,也就是說我們自己不能手動(dòng)new一個(gè)Looper對(duì)象。那么我們就來看一下如何創(chuàng)建一個(gè)Looper對(duì)象。之后在Looper類中我們找到下面這個(gè)方法。

private static void prepare(boolean quitAllowed) {
  if (sThreadLocal.get() != null) {
    throw new RuntimeException("Only one Looper may be created per thread");
  }
  sThreadLocal.set(new Looper(quitAllowed));
}

  在這里新建了一個(gè)Looper對(duì)象,然后將這個(gè)對(duì)象保存在ThreadLocal中,當(dāng)我們下次需要用到Looper的之后直接從這個(gè)sThreadLocal中取出即可。在這里簡(jiǎn)單說明一下ThreadLocal這個(gè)類,ThreadLocal它實(shí)現(xiàn)了本地變量存儲(chǔ),我們將當(dāng)前線程的數(shù)據(jù)存放在ThreadLocal中,若是有多個(gè)變量共用一個(gè)ThreadLocal對(duì)象,這時(shí)候在當(dāng)前線程只能獲取該線程所存儲(chǔ)的變量,而無法獲取其他線程的數(shù)據(jù)。在Looper這個(gè)類中為我們提供了myLooper來獲取當(dāng)前線程的Looper對(duì)象。從上面的方法還能夠看出,一個(gè)線程只能創(chuàng)建一次Looper對(duì)象。然后我們?cè)诳匆幌逻@個(gè)prepare在哪里被使用的。

public static void prepare() {
  prepare(true);
}

public static void prepareMainLooper() {
  prepare(false);
  synchronized (Looper.class) {
    if (sMainLooper != null) {
      throw new IllegalStateException("The main Looper has already been prepared.");
    }
    sMainLooper = myLooper();
  }
}

    prepare方法:這個(gè)是用于在子線程中創(chuàng)建一個(gè)Looper對(duì)象,在子線程中是可以退出消息循環(huán)的。
    prepareMainLooper方法:這個(gè)方法在上面的ActivityThread中的main方法中我們就已經(jīng)見到過了。它是為主線程創(chuàng)建一個(gè)Looper,在主線程創(chuàng)建Looper對(duì)象中,就設(shè)置了不允許退出消息循環(huán)。并且將主線程的Looper保存在sMainLooper中,我們可以通過getMainLooper方法來獲取主線程的Looper。
  在ActivityThread中的main方法中除了創(chuàng)建一個(gè)Looper對(duì)象外,還做了另外一件事,那就是通過loop方法開啟消息循環(huán)。那么我們就來看一下這個(gè)loop方法做了什么事情。

public static void loop() {
  final Looper me = myLooper();
  if (me == null) {
    throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
  }
  final MessageQueue queue = me.mQueue;

  // Make sure the identity of this thread is that of the local process,
  // and keep track of what that identity token actually is.
  Binder.clearCallingIdentity();
  final long ident = Binder.clearCallingIdentity();

  for (;;) {
    Message msg = queue.next(); // might block
    if (msg == null) {
      // No message indicates that the message queue is quitting.
      return;
    }

    // This must be in a local variable, in case a UI event sets the logger
    Printer logging = me.mLogging;
    if (logging != null) {
      logging.println(">>>>> Dispatching to " + msg.target + " " +
          msg.callback + ": " + msg.what);
    }

    msg.target.dispatchMessage(msg);

    if (logging != null) {
      logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
    }

    // Make sure that during the course of dispatching the
    // identity of the thread wasn't corrupted.
    final long newIdent = Binder.clearCallingIdentity();
    if (ident != newIdent) {
      Log.wtf(TAG, "Thread identity changed from 0x"
          + Long.toHexString(ident) + " to 0x"
          + Long.toHexString(newIdent) + " while dispatching to "
          + msg.target.getClass().getName() + " "
          + msg.callback + " what=" + msg.what);
    }

    msg.recycleUnchecked();
  }
}

  第2~6行:獲取當(dāng)前線程中的Looper,并從Looper中獲得消息隊(duì)列。
  第10~11行:確保當(dāng)前線程屬于當(dāng)前進(jìn)程,并且記錄真實(shí)的token。clearCallingIdentity的實(shí)現(xiàn)是在native層,對(duì)于具體是如何實(shí)現(xiàn)的就不在進(jìn)行分析。
  第14~18行:從消息隊(duì)列中取出消息,并且只有當(dāng)取出的消息為空的時(shí)候才會(huì)跳出循環(huán)。
  第27行:將消息重新交由Handler處理。
  第35~42行:確保調(diào)用過程中線程沒有被銷毀。
  第44行:對(duì)消息進(jìn)行回收處理。
  和我們剛才猜想的一樣,在loop中確實(shí)存在一個(gè)死循環(huán),而唯一退出該循環(huán)的方式就是消息隊(duì)列返回的消息為空。然后我們通過消息隊(duì)列的next()方法獲得消息。msg.target是發(fā)送消息的Handler,通過Handler中的dispatchMessage方法又將消息交由Handler處理。消息處理完成之后便對(duì)消息進(jìn)行回收處理。在這里我們也能夠通過quit和quitSafely退出消息循環(huán)。

public void quit() {
  mQueue.quit(false);
}

public void quitSafely() {
  mQueue.quit(true);
}

  我們可以看出對(duì)于消息循環(huán)的退出,實(shí)際上就是調(diào)用消息隊(duì)列的quit方法。這時(shí)候從MessageQueue的next方法中取出的消息也就是null了。下面我們來看一下這個(gè)MessageQueue。

MessageQueue

  MessageQueue翻譯為消息隊(duì)里,在這個(gè)消息隊(duì)列中是采用單鏈表的方式實(shí)現(xiàn)的,提高插入刪除的效率。對(duì)于MessageQueue在這里我們也只看一下它的入隊(duì)和出隊(duì)操作。
  MessageQueue入隊(duì)方法。

boolean enqueueMessage(Message msg, long when) {

  ......

  synchronized (this) {

    ......

    msg.markInUse();
    msg.when = when;
    Message p = mMessages;
    boolean needWake;
    if (p == null || when == 0 || when < p.when) {
      // New head, wake up the event queue if blocked.
      msg.next = p;
      mMessages = msg;
      needWake = mBlocked;
    } else {
      // Inserted within the middle of the queue. Usually we don't have to wake
      // up the event queue unless there is a barrier at the head of the queue
      // and the message is the earliest asynchronous message in the queue.
      needWake = mBlocked && p.target == null && msg.isAsynchronous();
      Message prev;
      for (;;) {
        prev = p;
        p = p.next;
        if (p == null || when < p.when) {
          break;
        }
        if (needWake && p.isAsynchronous()) {
          needWake = false;
        }
      }
      msg.next = p; // invariant: p == prev.next
      prev.next = msg;
    }

    // We can assume mPtr != 0 because mQuitting is false.
    if (needWake) {
      nativeWake(mPtr);
    }
  }
  return true;
}

  在這里我們簡(jiǎn)單說一下這個(gè)入隊(duì)的方法。消息的插入過程是在第13~36行完成了。在這里首先判斷首先判斷消息隊(duì)列里有沒有消息,沒有的話則將當(dāng)前插入的消息作為隊(duì)頭,并且這時(shí)消息隊(duì)列如果處于等待狀態(tài)的話則將其喚醒。若是在中間插入,則根據(jù)Message創(chuàng)建的時(shí)間進(jìn)行插入。
  MessageQueue出隊(duì)方法。

Message next() {

  ......

  int nextPollTimeoutMillis = 0;
  for (;;) {
    if (nextPollTimeoutMillis != 0) {
      Binder.flushPendingCommands();
    }

    nativePollOnce(ptr, nextPollTimeoutMillis);

    synchronized (this) {
      // Try to retrieve the next message. Return if found.
      final long now = SystemClock.uptimeMillis();
      Message prevMsg = null;
      Message msg = mMessages;
      if (msg != null && msg.target == null) {
        // Stalled by a barrier. Find the next asynchronous message in the queue.
        do {
          prevMsg = msg;
          msg = msg.next;
        } while (msg != null && !msg.isAsynchronous());
      }
      if (msg != null) {
        if (now < msg.when) {
          // Next message is not ready. Set a timeout to wake up when it is ready.
          nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
        } else {
          // Got a message.
          mBlocked = false;
          if (prevMsg != null) {
            prevMsg.next = msg.next;
          } else {
            mMessages = msg.next;
          }
          msg.next = null;
          if (DEBUG) Log.v(TAG, "Returning message: " + msg);
          msg.markInUse();
          return msg;
        }
      } else {
        // No more messages.
        nextPollTimeoutMillis = -1;
      }

      // Process the quit message now that all pending messages have been handled.
      if (mQuitting) {
        dispose();
        return null;
      }

      ......
    }

    .....
  }
}

  第11行:nativePollOnce方法在native層,若是nextPollTimeoutMillis為-1,這時(shí)候消息隊(duì)列處于等待狀態(tài)。
  第25~42行:按照我們?cè)O(shè)置的時(shí)間取出消息。
  第43~45行:這時(shí)候消息隊(duì)列中沒有消息,將nextPollTimeoutMillis設(shè)為-1,下次循環(huán)消息隊(duì)列則處于等待狀態(tài)。
  第48~52行:退出消息隊(duì)列,返回null,這時(shí)候Looper中的消息循環(huán)也會(huì)終止。
  最后我們?cè)诳匆幌峦顺鱿㈥?duì)列的方法:

void quit(boolean safe) {
  if (!mQuitAllowed) {
    throw new IllegalStateException("Main thread not allowed to quit.");
  }

  synchronized (this) {
    if (mQuitting) {
      return;
    }
    mQuitting = true;

    if (safe) {
      removeAllFutureMessagesLocked();
    } else {
      removeAllMessagesLocked();
    }

    // We can assume mPtr != 0 because mQuitting was previously false.
    nativeWake(mPtr);
  }
}

  從上面我們可以看到主線程的消息隊(duì)列是不允許被退出的。并且在這里通過將mQuitting設(shè)為true從而退出消息隊(duì)列。也使得消息循環(huán)被退出。到這里我們介紹了Looper和MessageQueue,就來看一下二者在Handler中的作用。

Handler

  在這里我們首先看一下Handler的構(gòu)造方法。

public Handler(Callback callback, boolean async) {

  ......

  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 = callback;
  mAsynchronous = async;
}

  從這個(gè)構(gòu)造方法中我們可以看出在一個(gè)沒有創(chuàng)建Looper的線程中是無法創(chuàng)建一個(gè)Handler對(duì)象的。所以說我們?cè)谧泳€程中創(chuàng)建一個(gè)Handler時(shí)首先需要?jiǎng)?chuàng)建Looper,并且開啟消息循環(huán)才能夠使用這個(gè)Handler。但是在上面的例子中我們確實(shí)在子線程中new了一個(gè)Handler對(duì)象。我們?cè)賮砜匆幌律厦婺莻€(gè)例子的構(gòu)造方法。

public Handler(Looper looper, Callback callback, boolean async) {
  mLooper = looper;
  mQueue = looper.mQueue;
  mCallback = callback;
  mAsynchronous = async;
}

  在這個(gè)構(gòu)造方法中我們?yōu)镠andler指定了一個(gè)Looper對(duì)象。也就說在上面的例子中我們?cè)谧泳€程創(chuàng)建的Handler中為其指定了主線程的Looper,也就等價(jià)于在主線程中創(chuàng)建Handler對(duì)象。下面我們就來看一下Handler是如何發(fā)送消息的。
  對(duì)于Handler的發(fā)送方式可以分為post和send兩種方式。我們先來看一下這個(gè)post的發(fā)送方式。

public final boolean post(Runnable r)
{
  return sendMessageDelayed(getPostMessage(r), 0);
}

  在這里很明顯可以看出來,將post參數(shù)中的Runnable轉(zhuǎn)換成了Message對(duì)象,然后還是通過send方式發(fā)出消息。我們就來看一下這個(gè)getPostMessage方法。

private static Message getPostMessage(Runnable r) {
  Message m = Message.obtain();
  m.callback = r;
  return m;
}

  在這里也是將我們實(shí)現(xiàn)的Runnable交給了Message對(duì)象的callback屬性。并返回該Message對(duì)象。
  既然post發(fā)送也是由send發(fā)送方式進(jìn)行的,那么我們一路找下去,最終消息的發(fā)送交由sendMessageAtTime方法進(jìn)行處理。我們就來看一下這個(gè)sendMessageAtTime方法。

public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
  MessageQueue queue = mQueue;
  if (queue == null) {
    RuntimeException e = new RuntimeException(
        this + " sendMessageAtTime() called with no mQueue");
    Log.w("Looper", e.getMessage(), e);
    return false;
  }
  return enqueueMessage(queue, msg, uptimeMillis);
}

  然后再來看一下enqueueMessage方法。

private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
  msg.target = this;
  if (mAsynchronous) {
    msg.setAsynchronous(true);
  }
  return queue.enqueueMessage(msg, uptimeMillis);
}

  到這里我們可以看出來了所謂通過Handler發(fā)送消息只不過是在Looper創(chuàng)建的消息隊(duì)列中插入一條消息而已。而在Looper中只不過通過loop取出消息,然后交由Handler中的dispatchMessage方發(fā)進(jìn)行消息分發(fā)處理。下面我們來看一下dispatchMessage方法。

public void dispatchMessage(Message msg) {
  if (msg.callback != null) {
    handleCallback(msg);
  } else {
    if (mCallback != null) {
      if (mCallback.handleMessage(msg)) {
        return;
      }
    }
    handleMessage(msg);
  }
}

  這里面的邏輯也是非常的簡(jiǎn)單,msg.callback就是我們通過post里的Runnable對(duì)象。而handleCallback也就是去執(zhí)行Runnable中的run方法。

private static void handleCallback(Message message) {
  message.callback.run();
}

  mCallback就是我們所實(shí)現(xiàn)的回調(diào)接口。最后才是對(duì)我們繼承Handler類中重寫的handleMessage進(jìn)行執(zhí)行。可見其中的優(yōu)先級(jí)順序?yàn)閜ost>CallBack>send;
  到這里我們對(duì)整個(gè)Handler的工作過程也就分析完了?,F(xiàn)在我們想要通過主線程發(fā)送消息給子線程,然后由子線程接收消息并進(jìn)行處理。這樣一種操作也就很容易實(shí)現(xiàn)了。我們來看一下怎么實(shí)現(xiàn)。

package com.example.ljd.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;


public class MyActivity extends AppCompatActivity {

  private final String TAG = "MyActivity";
  public Handler mHandler;
  public Button button;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.send_btn);
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (mHandler != null){
          mHandler.obtainMessage(0,"你好,我是從主線程過來的").sendToTarget();
        }
      }
    });
    new Thread(new Runnable() {
      @Override
      public void run() {
        //在子線程中創(chuàng)建一個(gè)Looper對(duì)象
        Looper.prepare();
        mHandler = new Handler(){
          @Override
          public void handleMessage(Message msg) {
            if (msg.what == 0){
              Log.d(TAG,(String)msg.obj);
            }
          }
        };
        //開啟消息循環(huán)
        Looper.loop();
      }
    }).start();

  }
}

  點(diǎn)擊按鈕我們看一下運(yùn)行結(jié)果。


總結(jié)

  在這里我們重新整理一下我們的思路,看一下這個(gè)Handler的整個(gè)工作流程。在主線程創(chuàng)建的時(shí)候?yàn)橹骶€程創(chuàng)建一個(gè)Looper,創(chuàng)建Looper的同時(shí)在Looper內(nèi)部創(chuàng)建一個(gè)消息隊(duì)列。而在創(chuàng)鍵Handler的時(shí)候取出當(dāng)前線程的Looper,并通過該Looper對(duì)象獲得消息隊(duì)列,然后Handler在子線程中發(fā)送消息也就是在該消息隊(duì)列中添加一條Message。最后通過Looper中的消息循環(huán)取得這條Message并且交由Handler處理。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android酷炫動(dòng)畫效果之3D星體旋轉(zhuǎn)效果

    Android酷炫動(dòng)畫效果之3D星體旋轉(zhuǎn)效果

    本文要實(shí)現(xiàn)的3D星體旋轉(zhuǎn)效果是從CoverFlow演繹而來,不過CoverFlow只是對(duì)圖像進(jìn)行轉(zhuǎn)動(dòng),我這里要實(shí)現(xiàn)的效果是要對(duì)所有的View進(jìn)行類似旋轉(zhuǎn)木馬的轉(zhuǎn)動(dòng)
    2018-05-05
  • 淺談Android中使用異步線程更新UI視圖的幾種方法

    淺談Android中使用異步線程更新UI視圖的幾種方法

    本篇文章主要介紹了淺談Android中使用異步線程更新UI視圖的幾種方法,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-08-08
  • Android 中的危險(xiǎn)權(quán)限詳細(xì)整理

    Android 中的危險(xiǎn)權(quán)限詳細(xì)整理

    這篇文章主要介紹了Android 中的危險(xiǎn)權(quán)限詳細(xì)整理的相關(guān)資料,Android 中有上百種權(quán)限,現(xiàn)在將所有的權(quán)限歸為兩類,一類是普通權(quán)限,一類的危險(xiǎn)權(quán)限,危險(xiǎn)權(quán)限則表示那些可能會(huì)觸及到用戶安全隱私或者對(duì)設(shè)備安全造成影響的權(quán)限,需要的朋友可以參考下
    2017-07-07
  • Flutter?Ping檢查服務(wù)器通訊信號(hào)強(qiáng)度實(shí)現(xiàn)步驟

    Flutter?Ping檢查服務(wù)器通訊信號(hào)強(qiáng)度實(shí)現(xiàn)步驟

    這篇文章主要為大家介紹了Flutter?Ping檢查服務(wù)器通訊信號(hào)強(qiáng)度實(shí)現(xiàn)步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • Android Studio進(jìn)行APP圖標(biāo)更改的兩種方式總結(jié)

    Android Studio進(jìn)行APP圖標(biāo)更改的兩種方式總結(jié)

    這篇文章主要介紹了Android Studio進(jìn)行APP圖標(biāo)更改的兩種方式總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Android軟鍵盤擋住輸入框的終極解決方案

    Android軟鍵盤擋住輸入框的終極解決方案

    最近在開發(fā)android的項(xiàng)目,在項(xiàng)目開發(fā)中遇到各種坑,今天小編就給大家分享一個(gè)關(guān)于軟鍵盤擋住輸入框的問題,下面給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,對(duì)軟鍵盤擋住輸入框的知識(shí)感興趣的朋友一起看看吧
    2016-10-10
  • 詳解Android中實(shí)現(xiàn)熱更新的原理

    詳解Android中實(shí)現(xiàn)熱更新的原理

    這篇文章主要為大家介紹了Android 熱更新實(shí)現(xiàn)原理,及代碼分析,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Android編程實(shí)現(xiàn)監(jiān)聽EditText變化的方法

    Android編程實(shí)現(xiàn)監(jiān)聽EditText變化的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)監(jiān)聽EditText變化的方法,涉及Android針對(duì)EditText的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • Android中banner的使用步驟

    Android中banner的使用步驟

    本文分步驟給大詳細(xì)介紹了Android中banner的使用,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧
    2017-06-06
  • Android依據(jù)名字通過反射獲取在drawable中的圖片

    Android依據(jù)名字通過反射獲取在drawable中的圖片

    依據(jù)圖片的名字,通過反射獲取其在drawable中的ID,在根據(jù)此ID顯示圖片,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈
    2013-06-06

最新評(píng)論