安卓Handler机制

Handler机制运用在安卓线程之间的消息传递。Handler之所以能够运行,得力于与Handler紧密相联的Looper,每一个线程都只能有一个Looper,一个Looper就可以看成一个事件执行者,Handler决定对事件做什么具体操作,所以一般继承了Handler就要重写其中的handleMessage方法,那么事件从哪里来呢?从消息队列MessageQueue中得来。
Handler的使用方法:

protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//创建handler对象
		handler = new Handler() {
		//重载handler的handlemessage方法
			@Override
			public void handleMessage(Message msg) {
				Log.i("TAG", "处理了事件");
				super.handleMessage(msg);
			}
		};

		Thread t1 = new Thread() {
			@Override
			public void run() {
			//新开一个线程,向主线程发送消息
				Message obtain = Message.obtain();
				handler.sendMessage(obtain);
				super.run();
			}
		};
		t1.start();
	}

程序执行起来之后,就会打印“处理了事件”

让我们一探其中的原理:
从上面的代码来看,没看到什么Looper和MessageQueue,只看到了new一个Handler和handler发送了一个消息.
让我们看一看new一个Handler时,系统为我们做了什么,首先看Handler的一个构造函数:

public Handler(Callback callback, boolean async) {
        if (FIND_POTENTIAL_LEAKS) {
            final Class 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());
            }
        }
		//获取当前线程的Looper,如果Looper为null,那么抛出异常
        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;
    }

上面的代码有一句判断当前线程的Looper有没有创建出来,如果没有,就直接报错。好像上面的例子中,我们并没有创建Looper啊,为什么我们的例子没有报异常。其实上面的例子是在主线程中创建的Handler的,在创建主线程的时候,系统已经默认为我们创建Looper了,因此不会报异常,但在其他线程中就要先创建Looper了,具体例子下面有。

有了Looper之后,MessageQueue也就有了,是在Looper的构造函数里面绑定了一个MessageQueue,先假设都有了,先看handler.sendMessage(obtain)里面干了什么,这里面其实是向MessageQueue插入了一条消息,最后调用到了MessageQueue的enqueueMessage方法

final boolean enqueueMessage(Message msg, long when) {
        if (msg.isInUse()) {
            throw new AndroidRuntimeException(msg + " This message is already in use.");
        }
        if (msg.target == null) {
            throw new AndroidRuntimeException("Message must have a target.");
        }

        boolean needWake;
        synchronized (this) {
            if (mQuiting) {
                RuntimeException e = new RuntimeException(
                        msg.target + " sending message to a Handler on a dead thread");
                Log.w("MessageQueue", e.getMessage(), e);
                return false;
            }

            msg.when = when;
            Message p = mMessages;
            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;
            }
        }
        if (needWake) {
            nativeWake(mPtr);
        }
        return true;
    }

如果needWake为true,就会唤醒与之相关联的Looper,Looper会在loop方法中调用MessageQueue的next方法,next方法会堵塞,正好在这个enqueueMessage方法中唤醒,唤醒之后Looper又可以接着处理消息了.

下面来看一下在非主线程中创建Handler的例子:

Thread thread = new Thread() {
			public void run() {
			//在非主线程中使用handler要首先创建Looper
				Looper looper = Looper.myLooper();
				//将Looper作为线程私有数据保存起来
				looper.prepare();
				final Handler handler = new Handler() {
					@Override
					public void handleMessage(Message msg) {
						String TAG = "abc";
						Log.i(TAG, "handle message");
						super.handleMessage(msg);
					}
				};
			//新建一个线程,用来发送Message
				Thread thread = new Thread() {
					public void run() {
						Message obtain = Message.obtain();
						handler.sendMessage(obtain);
					};

				};
				thread.start();
				//最后一定要调用此方法,looper才算真正运行起来了,因为这个方法是无限循环,后面的方法可能不会得到执行,除非其他地方调用了quit方法
				looper.loop();

			};

		};
		thread.start();

你可能感兴趣的:(Android)