xamarin學習筆記A13(安卓Handler異步消息處理)

(每次學習一點xamarin就做個學習筆記和視頻來加深記憶鞏固知識)
如有不正確的地方,請幫我指正。
安卓異步消息處理簡介
  有時候需要執行一些耗時的操作,例如從遠程服務器讀取數據,讀取的時間的長度由很多因素決定,我們不希望主線程被阻塞程序無法進行其它工作,而且Android中只能在主線程進行UI操作,不能在子線程操作,如果要根據子線程執行的結果來更新UI時,這時就需要用到安卓異步消息處理機制。

異步消息原理
  安卓異步消息處理主要有四個類:Message、Handler、Looper和MessageQueue
我們直接打交道的是前兩個類,後面兩個類在內部自動處理的。

說明
Message 用於攜帶數據
MessageQueue 是一個存放消息的隊列
Looper 用於不斷的從隊列中取消息,交給消息關聯的Handler對象去處理
Handler 用於處理消息

異步消息處理圖

  如圖所示,首先在主線程中創建一個Handler實例對象並重寫了HandlerMessage方法,然後在子線程中執行run方法並創建Message對象攜帶好數據,通過消息對象關聯的hander對象的SendMessage方法將Message對象發送到消息隊列,Looper會自動去從隊列中取消息,取到後自動交給Handler的HandlerMessage方法處理。

下面我提取了Android中這四個類的源碼的主要屬性和方法,配上個人加的註釋(如有註釋不對的地方,請大家指正)

Message類

//用於攜帶數據
public final class Message implements Parcelable {
    public int what;//一個整型標識
    public int arg1;//可保存一個整數
    public int arg2;//可保存一個整數
    public Object obj;//可保存任意數據

    long when;//消息應該被處理的時間
    Handler target;//保存與它關聯的Handlder對象的引用
    Runnable callback;//保存處理消息的回調方法的引用
    Message next;//指向下一個消息,因爲消息隊列是一個單鏈表的數據結構

    private static final Object sPoolSync = new Object();//用於同步鎖的對象
    private static Message sPool;//緩存對象
    private static int sPoolSize = 0;
    private static final int MAX_POOL_SIZE = 50;

    public Message() { }
    public static Message obtain() {//比上面構造方法效率高,因爲用了緩存對象
        synchronized (sPoolSync) {
            if (sPool != null) {
                Message m = sPool;
                sPool = m.next;
                m.next = null;
                m.flags = 0; // clear in-use flag
                sPoolSize--;
                return m;
            }
        }
        return new Message();
    }

    public long getWhen() {return when;}
    public void setTarget(Handler target) {this.target = target;}
    public Handler getTarget() {return target;}
    public Runnable getCallback() {return callback;}
    public void sendToTarget() {target.sendMessage(this);}
}

MessageQueue類

//消息隊列(以Message的when值來排序的鏈表結構)
public final class MessageQueue {
    private final boolean mQuitAllowed;
    Message mMessages;//鏈表中最前頭的Message對象(when值最小)
    private boolean mQuitting;

    // Indicates whether next() is blocked waiting in pollOnce() with a non-zero timeout.
    private boolean mBlocked;
    //一些本地方法
    private native static long nativeInit();
    private native static void nativeDestroy(long ptr);
    private native void nativePollOnce(long ptr, int timeoutMillis);
    private native static void nativeWake(long ptr);

    MessageQueue(boolean quitAllowed) {
        mQuitAllowed = quitAllowed;
    }

    //從隊列中取消息
    Message next() {
        final long ptr = mPtr;
        if (ptr == 0) {return null;}

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

            nativePollOnce(ptr, nextPollTimeoutMillis);

            synchronized (this) {//以同步的方式取,確保同一時刻只有一個線程訪問
                final long now = SystemClock.uptimeMillis();
                Message prevMsg = null;
                Message msg = mMessages;
                if (msg != null && msg.target == null) {//如果消息關聯的Handlder對象沒有
                    do {
                        prevMsg = msg;
                        msg = msg.next;
                    } while (msg != null && !msg.isAsynchronous());//那麼只取異步消息
                }
                if (msg != null) {
                    if (now < msg.when) {//如果沒到消息應該被處理的時間
                        nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);// 計算一個超時時間來等待
                    } else {//到達消息應該被處理的時間就返回消息
                        mBlocked = false;
                        if (prevMsg != null) {prevMsg.next = msg.next;}//取出一個消息後整理好鏈表
                        else {mMessages = msg.next;}

                        msg.next = null;
                        msg.markInUse();
                        return msg;
                    }
                } else {
                    nextPollTimeoutMillis = -1;
                }
            }
            nextPollTimeoutMillis = 0;
        }
    }

    //添加一個新的消息到隊列中
    boolean enqueueMessage(Message msg, long when) {
        if (msg.target == null) {throw new IllegalArgumentException("Message must have a target.");}
        if (msg.isInUse()) {throw new IllegalStateException(msg + " This message is already in use.");}

        synchronized (this) {//以同步方式添加消息
            if (mQuitting) {
                IllegalStateException e = new IllegalStateException(
                        msg.target + " sending message to a Handler on a dead thread");
                msg.recycle();
                return false;
            }

            msg.markInUse();
            msg.when = when;
            Message p = mMessages;
            boolean needWake;
            if (p == null || when == 0 || when < p.when) {//如果鏈表爲空或者新來消息比隊列中所有消息的優先級更高
                msg.next = p;//把原先鏈表中最前頭的消息放到新的消息後面
                mMessages = msg;//設置新消息爲鏈表中最前頭的消息
                needWake = mBlocked;
            } else {
                needWake = mBlocked && p.target == null && msg.isAsynchronous();
                Message prev;
                for (;;) {
                    prev = p;
                    p = p.next;//從鏈表第二個開始找合適的位置(第一個已經在上面的when < p.when判斷過了)
                    if (p == null || when < p.when) {
                        break;//找到合適鏈表位置p後退出循環
                    }
                    if (needWake && p.isAsynchronous()) {
                        needWake = false;
                    }
                }
                msg.next = p;
                prev.next = msg;//這兩行代碼是把新消息插到prev與p之間
            }

            if (needWake) {
                nativeWake(mPtr);
            }
        }
        return true;
    }
}

Handler類

//用於處理消息
public class Handler {
    final Looper mLooper;   //保存着Looper對象的引用
    final MessageQueue mQueue;  //保存着Looper對象中MessageQueue對象的引用
    final Handler.Callback mCallback;  //保存着構造方法傳入的回調方法的引用

    public interface Callback { //一個接口,規定了消息處理的方法的規範
        public boolean handleMessage(Message msg);
    }
    public Handler() {
        this(null, false);
    } //構造方法

    public Handler(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;
    }

    public void handleMessage(Message msg)
    {
        //如果dispatchMessage方法中的msg.callback==null 或 mCallback==null
        //那麼子類應該重寫此方法來處理消息
    }

    public void dispatchMessage(Message msg) {//分發消息給回調方法處理
        if (msg.callback != null) {
            handleCallback(msg);
        } else {
            if (mCallback != null) {
                if (mCallback.handleMessage(msg)) {
                    return;
                }
            }
            handleMessage(msg);
        }
    }

    public final boolean sendMessage(Message msg) { //發送一個消息
        return sendMessageDelayed(msg, 0);
    }
    public final boolean sendMessageDelayed(Message msg, long delayMillis)//以指定的延遲時間發送消息
    {
        if (delayMillis < 0) {delayMillis = 0;}
        //當前時間加上延遲時間就是消息應該被處理的時間
        return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
    }
    public boolean sendMessageAtTime(Message msg, long uptimeMillis) {//在指定的時間發送消息
        MessageQueue queue = mQueue;
        if (queue == null) { return false;}
        return enqueueMessage(queue, msg, uptimeMillis);
    }
    //將消息發送到消息隊列中
    private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        msg.target = this;
        //通過MessageQueue對象的enqueueMessage方法把消息添加到消息隊列
        return queue.enqueueMessage(msg, uptimeMillis);
    }
}

Looper類

//用於不斷的從隊列中取消息,交給消息關聯的Handler對象去處理
public final class Looper {
    static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();//以鍵值對形式存儲(key:線程 value:此線程的Looper對象)
    private static Looper sMainLooper;

    final MessageQueue mQueue;//自己持有的消息隊列
    final Thread mThread;//自己所在的線程

    private Looper(boolean quitAllowed) {//私有構造方法
        mQueue = new MessageQueue(quitAllowed);
        mThread = Thread.currentThread();
    }

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

    private static void prepare(boolean quitAllowed) {
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("每個線程只能有一個Looper");
        }
        sThreadLocal.set(new Looper(quitAllowed));
    }

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

    public static Looper getMainLooper() {
        synchronized (Looper.class) {
            return sMainLooper;
        }
    }

    //開啓循環
    public static void loop() {
        final Looper me = myLooper();
        final MessageQueue queue = me.mQueue;

        for (;;) {
            Message msg = queue.next(); //從消息隊列中取出一個消息
            if (msg == null) {
                return;
            }

            try {
                msg.target.dispatchMessage(msg);//將消息交給關聯的Handlder對象去處理
            } finally {
            }
            msg.recycleUnchecked();
        }
    }

    public static @Nullable Looper myLooper() {
        return sThreadLocal.get();
    }//取出Looper
    public static @NonNull MessageQueue myQueue() {
        return myLooper().mQueue;
    }
    public boolean isCurrentThread() {return Thread.currentThread() == mThread;}
    public void quit() {mQueue.quit(false);}
    public @NonNull Thread getThread() {return mThread;}
    public @NonNull MessageQueue getQueue() {return mQueue;}
}

  我們直接打交道的是Message類和Handlder類,那麼MessageQueue和Looper類是怎麼來的,這就要看ActivityThread類程序的入口方法main,貼上部分代碼如下:

public final class ActivityThread {
    //..........省略其它代碼

    public static void main(String[] args) {
        //..........省略其它代碼

        Looper.prepareMainLooper();//創建好Looper對象,它構造方法裏同時也創建了MessageQueue對象

        ActivityThread thread = new ActivityThread();//創建好主線程
        thread.attach(false);

        if (sMainThreadHandler == null) {
            sMainThreadHandler = thread.getHandler();
        }
        Looper.loop(); //開啓無限循環,不斷從MessageQueue中取消息交給消息關聯的Handler處理
        throw new RuntimeException("Main thread loop unexpectedly exited");
    }
}

  大概瞭解了這四大類和它們的機制後,接下來就開始做個簡單的異步消息處理程序。

//處理消息的類
    public class MyHandler : Handler
    {
        private Action<Message> _action;//指向一個回調方法的委託
        public MyHandler(Action<Message> action)
        {
            _action = action;//構造方法中傳入處理消息的回調方法
        }
        //重寫父類的HandleMessage方法
        public override void HandleMessage(Message msg)
        {
            //if (_action != null)
            //{
            //    _action.Invoke(msg);
            //}
            _action?.Invoke(msg); //C#6.0新增判斷空值的方式
        }
}
//模擬從服務器讀取數據(實現Java.Lang.IRunnable接口可讓線程使用)
    public class ReadDataTask : Java.Lang.Object, Java.Lang.IRunnable
    {
        private Handler _handler;

        public ReadDataTask(Handler handler)
        {
            _handler = handler;
        }

        public void Run()//子線程要運行的任務
        {
            try
            {
                Java.Lang.Thread.Sleep(4000);// 模擬任務,比如從遠程服務器讀取數據

                Message msg = _handler.ObtainMessage();//創建一個消息對象
                msg.What = 123; //設置消息對象的標識
                msg.Obj = "已讀取到數據";//消息對象攜帶一個字符串數據

                _handler.SendMessage(msg);// 將消息對象發送到主線程的消息隊列(MessageQueue)中
            }
            catch (Java.Lang.InterruptedException e)
            {
                Log.Debug("ReadInfoTask", e.Message);
            }
        }
    }

  上面兩個類準備好後,接下來就在Activity中使用了

public class MainActivity : AppCompatActivity,IOnClickListener
    {
        private MyHandler _myHandler;
        private EditText _editText;

        public void OnClick(View v)
        {
            switch (v.Id)
            {
                case Resource.Id.button1:
                    Java.Lang.Thread thread = new Java.Lang.Thread(new ReadDataTask(_myHandler));
                    thread.Start();//啓動一個子線程去執行耗時任務
                    break;            
}
        }

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Main);

            Button btn1 = this.FindViewById<Button>(Resource.Id.button1);
            btn1.SetOnClickListener(this);

            _editText = this.FindViewById<EditText>(Resource.Id.editText1);

            _myHandler = new MyHandler(RefreshUI); //在主線程實例化一個Handler對象

        }

        private void RefreshUI(Message msg)//在主線程更新UI
        {
            switch (msg.What)
            {
                case 123:
                    string s = Convert.ToString(msg.Obj);
                    _editText.Text = s;
                    break;
                case 456:
                    //……………………
                    break;
            }
        }
    }

代碼和視頻在我上傳的CSDN資源中http://download.csdn.net/download/junshangshui/10022514

發佈了74 篇原創文章 · 獲贊 50 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章