Handler

Android提供了Handler来进行简单的线程间通信,通常我们在异步线程中做一些耗时的操作,然后通过Handler发送消息,在主线程中通过Handler的handleMessage方法去更新UI。我们先来看一下Handler的构造方法。


    public Handler() {
        this(null, false);
    }

    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());
            }
        }
//上面一段是检查是否有潜在的内存泄露,所以推荐的方式是使用静态内部类
        mLooper = Looper.myLooper();//初始化成员mLooper
        if (mLooper == null) {
            throw new RuntimeException(
                "Can't create handler inside thread that has not called Looper.prepare()");
        }
        mQueue = mLooper.mQueue;//初始化成员mQueue
        mCallback = callback;
        mAsynchronous = async;
    }

看一下Looper.java的部分代码


    public static @Nullable Looper myLooper() {
        return sThreadLocal.get();
    }

    static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();

Handler的构造方法通过Looper的static方法myLooper从一个类型为ThreadLocal的static变量sThreadLocal中get一个Looper实例。

ThreadLocal.java


    public void set(T value) {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
    }

    ThreadLocalMap getMap(Thread t) {
        return t.threadLocals;//第一次返回null
    }

    void createMap(Thread t, T firstValue) {
        t.threadLocals = new ThreadLocalMap(this, firstValue);
    }

        ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
            table = new Entry[INITIAL_CAPACITY];
            int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);//计算桶的位置
            table[i] = new Entry(firstKey, firstValue);//将value包装为Entry放入table
            size = 1;
            setThreshold(INITIAL_CAPACITY);
        }

        static class Entry extends WeakReference<ThreadLocal<?>> {
            /** The value associated with this ThreadLocal. */
            Object value;

            Entry(ThreadLocal<?> k, Object v) {
                super(k);
                value = v;
            }
        }

    public T get() {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null) {
            ThreadLocalMap.Entry e = map.getEntry(this);
            if (e != null) {
                @SuppressWarnings("unchecked")
                T result = (T)e.value;
                return result;
            }
        }
        return setInitialValue();
    }

ThreadLocal的get和set方法都是通过一个ThreadLocalMap进行的,这个ThreadLocalMap与当前Thread关联在一起,通过当前Thread可以get到这个Map,这个Map的内部实际是一个数组,数组元素是Entry类型,Entry继承WeakReference,它有一个成员value,这样Entry就相当于一个键值对。

回来看Looper的prepare方法


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

    private static void prepare(boolean quitAllowed) {
        if (sThreadLocal.get() != null) {//第一次调用get会crateMap,并返回null
            //第二次调用get将返回非null,这里会抛出一个异常,所以prepare方法每个线程只能调用一次
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        sThreadLocal.set(new Looper(quitAllowed));//set方法将sTreadLocal作为key,Looper实例作为value放入ThreadLocalMap
    }

    private Looper(boolean quitAllowed) {
        mQueue = new MessageQueue(quitAllowed);//new一个MessageQueue实例
        mThread = Thread.currentThread();//将成员mThread初始化为当前Thread
    }

从以上分析可以得出结论,在某个线程中new一个Handler之前必须调用Looper的prepare方法,初始化一个与当前线程绑定的Looper实例。

通常我们在使用Handler时是直接在主线程new一个Handler,那么是什么时候调用了Looper的prepare方法呢?

我们经常看到如下的堆栈信息,Android应用进程启动时会调用ActivityThread的main方法,这也是普通java程序的入口。

  at android.os.Looper.loop(Looper.java:154)
  at android.app.ActivityThread.main(ActivityThread.java:6121)
  at java.lang.reflect.Method.invoke!(Native method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

ActivityThread.java

    public static void main(String[] args) {
        //忽略了一些代码
        Looper.prepareMainLooper();

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

        if (sMainThreadHandler == null) {
            sMainThreadHandler = thread.getHandler();
        }
        Looper.loop();
    }
    public static void prepareMainLooper() {
        prepare(false);//这里调用了prepare方法
        synchronized (Looper.class) {
            if (sMainLooper != null) {
                throw new IllegalStateException("The main Looper has already been prepared.");
            }
            sMainLooper = myLooper();//初始化静态变量sMainLooper
        }
    }
    private static Looper sMainLooper;

    private void attach(boolean system) {
        //忽略了部分代码
        sCurrentActivityThread = this;
        mSystemThread = system;
        if (!system) {
            RuntimeInit.setApplicationObject(mAppThread.asBinder());
            final IActivityManager mgr = ActivityManagerNative.getDefault();
            try {
                mgr.attachApplication(mAppThread);
            } catch (RemoteException ex) {
                throw ex.rethrowFromSystemServer();
            }
        } else {
            // Don't set application object here -- if the system crashes,
            // we can't display an alert, we just want to die die die.
            try {
                mInstrumentation = new Instrumentation();
                ContextImpl context = ContextImpl.createAppContext(
                        this, getSystemContext().mPackageInfo);
                mInitialApplication = context.mPackageInfo.makeApplication(true, null);
                mInitialApplication.onCreate();
            } catch (Exception e) {
                throw new RuntimeException(
                        "Unable to instantiate Application():" + e.toString(), e);
            }
        }
    }

因为system传进来的是false我们暂时只看上面一部分。
RuntimeInit.java

    public static final void setApplicationObject(IBinder app) {
        mApplicationObject = app;
    }
    private static IBinder mApplicationObject;
final ApplicationThread mAppThread = new ApplicationThread();//ActivityThread有一个ApplicationThread类型的成员mAppThread

    private class ApplicationThread extends ApplicationThreadNative//ApplicationThread继承自ApplicationThreadNative
    public abstract class ApplicationThreadNative extends Binder
        implements IApplicationThread//ApplicationThreadNative继承自Binder并实现了IApplicationThread接口

    public IBinder asBinder()
    {
        return this;
    }

RuntimeInit.setApplicationObject(mAppThread.asBinder());是将mAppThread作为Binder保存在RuntimeInit中。

    static public IActivityManager getDefault() {
        return gDefault.get();
    }
    private static final Singleton<IActivityManager> gDefault = new Singleton<IActivityManager>() {
        protected IActivityManager create() {
            IBinder b = ServiceManager.getService("activity");
            IActivityManager am = asInterface(b);
            return am;
        }
    };

public abstract class Singleton<T> {
    private T mInstance;

    protected abstract T create();

    public final T get() {
        synchronized (this) {
            if (mInstance == null) {
                mInstance = create();
            }
            return mInstance;
        }
    }
}

public abstract class ActivityManagerNative extends Binder implements IActivityManager//ActivityManagerNative同样也是继承自Binder并实现了IActivityManager接口

    static public IActivityManager asInterface(IBinder obj) {
        if (obj == null) {
            return null;
        }
        IActivityManager in =
            (IActivityManager)obj.queryLocalInterface(descriptor);
        if (in != null) {
            return in;
        }

        return new ActivityManagerProxy(obj);
    }

    public IInterface queryLocalInterface(String descriptor) {
        if (mDescriptor.equals(descriptor)) {
            return mOwner;
        }
        return null;
    }
    private IInterface mOwner;
    private String mDescriptor;

    String descriptor = "android.app.IActivityManager";//这是IActivityManager中的descriptor的定义

ServiceManager.getService("activity")通过ServiceManager的getService方法返回了一个IBinder对象,然后通过它的queryLocalInterface方法返回一个IInterface对象并强转为IActivityManager,如果这个IActivityManager为null则new一个ActivityManagerProxy对象,并把代表ActivityManagerService的IBinder对象传给ActivityManagerProxy的构造方法。

class ActivityManagerProxy implements IActivityManager
{
    public ActivityManagerProxy(IBinder remote)
    {
        mRemote = remote;
    }

    public IBinder asBinder()
    {
        return mRemote;
    }
}

final IActivityManager mgr = ActivityManagerNative.getDefault();通过ServiceManager获取ActivityManagerService的IBinder对象,并Cast为IActivityManager。

下面我们看一下ServiceManager的代码。

    public static IBinder getService(String name) {
        try {
            IBinder service = sCache.get(name);//先从缓存中获取
            if (service != null) {
                return service;
            } else {//如果缓存中没有
                return getIServiceManager().getService(name);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "error in getService", e);
        }
        return null;
    }
    private static HashMap<String, IBinder> sCache = new HashMap<String, IBinder>();

    private static IServiceManager getIServiceManager() {
        if (sServiceManager != null) {
            return sServiceManager;
        }

        // Find the service manager
        sServiceManager = ServiceManagerNative.asInterface(BinderInternal.getContextObject());
        return sServiceManager;
    }
    private static IServiceManager sServiceManager;

public abstract class ServiceManagerNative extends Binder implements IServiceManager//ServiceManagerNative也是继承自Binder并实现了IServiceManager接口

    static public IServiceManager asInterface(IBinder obj)
    {
        if (obj == null) {
            return null;
        }
        IServiceManager in =
            (IServiceManager)obj.queryLocalInterface(descriptor);
        if (in != null) {
            return in;
        }

        return new ServiceManagerProxy(obj);
    }

//ServiceManagerNative与ActivityManagerNative类似

代表ActivityManagerService的IBinder对象是通过ServiceManager的get方法得到的,而代表'ServiceManagerService'的IBinder对象是通过BinderInternal.getContextObject()得到。

    public static final native IBinder getContextObject();//这是一个native方法

关于Binder的分析暂时不继续深入,我们直接跨过IPC,看ActivityManagerService.java

    @Override
    public final void attachApplication(IApplicationThread thread) {
        synchronized (this) {
            int callingPid = Binder.getCallingPid();
            final long origId = Binder.clearCallingIdentity();
            attachApplicationLocked(thread, callingPid);
            Binder.restoreCallingIdentity(origId);
        }
    }

    /**
     * Return the ID of the process that sent you the current transaction
     * that is being processed.  This pid can be used with higher-level
     * system services to determine its identity and check permissions.
     * If the current thread is not currently executing an incoming transaction,
     * then its own pid is returned.
     */
    public static final native int getCallingPid();
    /**
     * Reset the identity of the incoming IPC on the current thread.  This can
     * be useful if, while handling an incoming call, you will be calling
     * on interfaces of other objects that may be local to your process and
     * need to do permission checks on the calls coming into them (so they
     * will check the permission of your own local process, and not whatever
     * process originally called you).
     *
     * @return Returns an opaque token that can be used to restore the
     * original calling identity by passing it to
     * {@link #restoreCallingIdentity(long)}.
     *
     * @see #getCallingPid()
     * @see #getCallingUid()
     * @see #restoreCallingIdentity(long)
     */
    public static final native long clearCallingIdentity();
     * Restore the identity of the incoming IPC on the current thread
     * back to a previously identity that was returned by {@link
     * #clearCallingIdentity}.
     *
     * @param token The opaque token that was previously returned by
     * {@link #clearCallingIdentity}.
     *
     * @see #clearCallingIdentity
     */
    public static final native void restoreCallingIdentity(long token);
//在clearCallingIdentity和restoreCallingIdentity之间如果检查权限,将会检查这个Servie进程的权限,而不会检查调用进程的权限。(不知道这样理解对不对)

attachApplicationLocked(thread, callingPid);非常复杂,暂时不管。
回到ActivityThread的main方法。

    if (sMainThreadHandler == null) {
        sMainThreadHandler = thread.getHandler();
    }

    final Handler getHandler() {
        return mH;
    }
    final H mH = new H();
    private class H extends Handler;
    static volatile Handler sMainThreadHandler;

然后调用Looper.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,从MessageQueue中获取Message
            if (msg == null) {
                // No message indicates that the message queue is quitting.
                return;
            }

            try {
                msg.target.dispatchMessage(msg);//msg.target就是一个Handler
            } finally {
            }

            msg.recycleUnchecked();//放入消息池回收
        }
    }

    public void dispatchMessage(Message msg) {
        if (msg.callback != null) {
            handleCallback(msg);//如果Message带有一个Callback,实际是一个Runnable,则直接执行它的run方法。
        } else {
            if (mCallback != null) {
                if (mCallback.handleMessage(msg)) {
                    return;
                }
            }
            handleMessage(msg);//子类复写这个方法并处理自己的业务逻辑
        }
    }
    private static void handleCallback(Message message) {
        message.callback.run();
    }

Handler的sendMessage方法最终会调用enqueueMessage

    private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        msg.target = this;//将Message与Handler关联
        if (mAsynchronous) {
            msg.setAsynchronous(true);
        }
        return queue.enqueueMessage(msg, uptimeMillis);//调用MessageQueue的enqueueMessage方法
    }

MessageQueue简单来说就是一个消息队列,队列有先进先出的特性,它通过enqueueMessage方法来使消息入队,在Looper的loop循环中通过next方法获取队头的消息。消息队列的入队和出队比较复杂,暂时不做分析。

最后给出Handler的典型用法

private static class MyHandler extends Handler {
        private final WeakReference<OuterActivity> mOuter;

        public MyHandler(OuterActivity outer) {
            mOuter = new WeakReference<OuterActivity>(outer);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            OuterActivity outer = mOuter.get();
            if (outer != null) {
                switch (msg.what) {
                   //业务逻辑
                }
            }
        }
    }
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容