前言:
我们知道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("
关注
打赏