idleHandler

3,278 阅读2分钟

技术和职场问题,欢迎关注公众号【Android茶话会】

idleHandler

最近在做一些性能优化方面工作,发现idleHandler这么个神器,挺有意思的

什么是idleHandler

Android是基于Looper消息循环的系统,我们通过Handler向Looper包含的MessageQueue投递Message,在MessageQueue中我们可以看到这样的一个接口

 /**
     * Callback interface for discovering when a thread is going to block
     * waiting for more messages.
     */
    public static interface IdleHandler {
        /**
         * Called when the message queue has run out of messages and will now
         * wait for more.  Return true to keep your idle handler active, false
         * to have it removed.  This may be called if there are still messages
         * pending in the queue, but they are all scheduled to be dispatched
         * after the current time.
         */
        boolean queueIdle();
    }

从信息描述可以看到在Looper里面的Messsage暂时处理完了,这个时候会回调这个接口,即 ”idle“,

返回值

如果返回false用完就会移除这个接口,相当与使用一次,后面这个消息就从队列移除了
返回true就会保留,在下次Looper空闲时继续处理。比如在主线程return true,就会出现一直轮询

作用

从上面可以看出,这个仅仅是在消息队列暂时处理完会调用这个回调,那么就有下面两种场景可以使用

UI线程 即主线程

用法

Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
            @Override
            public boolean queueIdle() {
                /*dosometing*/
                return false;
            }
        });

actvity中UI绘制完全结束时机

image.png

onStart是用户可见,onResume是用户可交互,此时绘制不一定完成了,而idler等到这些都完成了,再去执行自己,因此 我们获取一些宽高可以在这里做。

提升性能

在做性能优化时候,可以错开执行消息队列,将一些耗时操作放到这里执行,比如我们国际化司机端,首页有个Google地图,还有一些出车文字滚动和波纹动画之类的,此时就可以将出车动画放在idleHanlder中,让底图先渲染。

自定义Looper 如HandlerThread

除了主线程之外,我们也可以自定义自己的Looper去处理,这时候可以让自己Looper关联上idlerHandler在Looper消息处理完时再去处理其他的东西,

 MessageQueue messageQueue = handlerThread.getLooper().getQueue();
        messageQueue.addIdleHandler(new MessageQueue.IdleHandler() {
            @Override
            public boolean queueIdle() {
             
                return true;
            }
        });

比如在单线程异步场景中,多次快速数据,但是只需要显示一次,就可以使用这种方式

注意点

要是有强时序性的操作最好不好放到这里处理,容易出问题

参考

画板 27.png