[Android]Handler的消息机制

本文详细介绍了Android应用中Handler消息机制的核心概念,包括Looper、MessageQueue和Handler的作用,并通过实例展示了如何在子线程中使用Handler进行消息传递。重点讨论了Handler在不同线程间的交互过程,以及如何避免潜在的内存泄露问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 最经面试中,技术面试中有一个是Handler的消息机制,细细想想,我经常用到的Handler无非是在主线程(或者说Activity)新建一个Handler对象,另外一个Thread是异步加载数据,同时当他加载完数据后就send到主线程中的那个Handler对象,接着Handler来处理,刚才发送的一些消息。

          

 View Code

 

如图所示,每个Thread都一个Looper,这个Looper类是用于管理其中的消息队列(MessageQueue)的,那Handler是干嘛的呢,他是用来传递消息队列的。

那下面就分析Looper、Hanlder方法吧。

Looper方法是用来处理消息队列的,注意了,它和线程是绑定的。

要是想在子线程中获取一个Looper该怎么做呢:

    Looper.prepare();
    Looper looper = Looper.myLooper();

那么这些都干了哪些工作呢???

来看下它的源码吧:

Looper:

复制代码
……
//准备Looper相关事宜
   public static void prepare() {
     //只能有一个对象哦
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
         sThreadLocal.set(new Looper());
     }
   //构造函数
  /*新建一个消息队列
   * 把当前运行的线程作为运行线程
  */

    private Looper() {
        mQueue = new MessageQueue();
        mRun = true;
        mThread = Thread.currentThread();
    }

             public static final Looper myLooper() {

                            //这个方法是从当前线程的ThreadLocal中拿出设置的looper

                 return (Looper)sThreadLocal.get();

             }

/**
     * Run the message queue in this thread. Be sure to call
     * {@link #quit()} to end the loop.
     */
    public static void loop() {
        Looper me = myLooper();
        if (me == null) {
            throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
        }
        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();
        
        while (true) {
            Message msg = queue.next(); // might block
            if (msg != null) {
                if (msg.target == null) {
                    // No target is a magic identifier for the quit message.
                    return;
                }

                long wallStart = 0;
                long threadStart = 0;

                // 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);
                    wallStart = SystemClock.currentTimeMicro();
                    threadStart = SystemClock.currentThreadTimeMicro();
                }

                msg.target.dispatchMessage(msg);

                if (logging != null) {
                    long wallTime = SystemClock.currentTimeMicro() - wallStart;
                    long threadTime = SystemClock.currentThreadTimeMicro() - threadStart;

                    logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
                    if (logging instanceof Profiler) {
                        ((Profiler) logging).profile(msg, wallStart, wallTime,
                                threadStart, threadTime);
                    }
                }

                // 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.recycle();
            }
        }
    }
复制代码

下面就来看下Handler:

复制代码
public Handler() {
        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对象,这个要是在子线程里,是需要先prepare()的

   
        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 = null;
    }
  /**
     * Returns a new {@link android.os.Message Message} from the global message pool. More efficient than
     * creating and allocating new instances. The retrieved message has its handler set to this instance (Message.target == this).
     *  If you don't want that facility, just call Message.obtain() instead.
   会从消息池里面取得消息队列
     */
    public final Message obtainMessage()
    {
        return Message.obtain(this);
    }
复制代码

那我现在写个小例子,是在子线程实现的消息的传递。

复制代码
@Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn1) {
            new Thread() {

                public void run() {

                    Log.i("log", "run");

                    Looper.prepare();
                    // Looper looper = Looper.myLooper();
                    Toast.makeText(MainActivity.this, "toast", 1).show();
                    Handler h = new Handler() {

                        @Override
                        public void handleMessage(Message msg) {
                            // TODO Auto-generated method stub
                            super.handleMessage(msg);
                            if (msg != null) {
                                String strMsg = (String) msg.obj;
                                System.out.println(strMsg);
                            }

                        }

                    };
                    //获取到Handler对象的消息
                    Message msg = h.obtainMessage();
                    msg.obj = "add";
                    msg.sendToTarget();

                    Looper.loop();// 进入loop中的循环,查看消息队列

                };

            }.start();

        }
    }
复制代码

不知你是否理解,这个小Demo中,我们需要注意:

1  子线程也是可以有Handler的,其实Handler只是从当前的线程中获取到Looper来监听和操作MessageQueue的。

2 子线程需要先prepare()才能获取到Looper的,是因为在子线程只是一个普通的线程,其ThreadLoacl中没有设置过Looper,所以会抛出异常,而在Looper的prepare()方法中sThreadLocal.set(new Looper())是设置了Looper的。

 

而对于主线程里面的Handler,是没有以上的麻烦的,因为这个在Activity创建时,就已经初始化了Looper等其他工作了。

另外可以看下参考文章中的子线程中Toast。

参考文章:

Android之Handler用法总结

Android Handler机制

子线程中Toast

鸡蛋,从外打破是食物,从内打破是生命。 人亦如是:从外打破是压力,从内打破是成长。 如果你等别人从外打破你,那么你注定是要成为别人的食物; 若是你自己从内打破,那么你会发现自己获得重生。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值