您当前的位置: 首页 >  android
  • 0浏览

    0关注

    674博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Android打印主线程所有方法执行时间

沙漠一只雕得儿得儿 发布时间:2020-02-22 17:48:58 ,浏览量:0

前言:

我们知道Android卡顿主要是主线程中有耗时操作导致的,那么我们怎么能方便快捷的获取主线程中的所有耗时方法执行时间呢?今天我们来介绍两个方案

方案一:利用Looper.java中loop()方法的logging.print的特殊关键字进行耗时打印:

在消息分发时,主线程的looper.loop()方法会遍历所有的消息进行分发,执行耗时任务。我们看下源码的loop()方法:

for (;;) {
            Message msg = queue.next(); // might block
            if (msg == null) {
                // No message indicates that the message queue is quitting.
                return;
            }

            // This must be in a local variable, in case a UI event sets the logger
            final Printer logging = me.mLogging;
            if (logging != null) {
                logging.println(">>>>> Dispatching to " + msg.target + " " +
                        msg.callback + ": " + msg.what);
            }

            final long traceTag = me.mTraceTag;

... ...
... ...

if (logging != null) {
                logging.println(">>>>")) {
                    startTime = System.currentTimeMillis();
                } else if (x.startsWith(">>>>")) {
                    startTime = System.currentTimeMillis();
                } else if (x.startsWith("            
关注
打赏
1657159701
查看更多评论
0.0398s