Handler

94 阅读1分钟

Handler主要构成: Handler Lopper MessageQueue Message Handler的构造方法: 在使用Handler的时候,先创建一个Handler对象,在构造函数中如果传入了Looper的话,就使用传入的Looper,

在Handler的构造函数中 public Handler(Looper looper, Callback callback, boolean async) { mLooper = looper; mQueue = looper.mQueue; mCallback = callback; mAsynchronous = async; }

public Handler(Callback callback, boolean async) { 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()); } } //获取当前线程的Looper mLooper = Looper.myLooper(); if (mLooper == null) { throw new RuntimeException( "Can't create handler inside thread " + Thread.currentThread() + " that has not called Looper.prepare()"); } //给Handler中的成员变量初始化 mQueue = mLooper.mQueue; mCallback = callback; mAsynchronous = async; }