如果你想直观的知道某个函数是如何被其他函数层层调用的,只需将如下函数findCaller()添加到该函数中,之后就可以通过logcat查看调用过程。
import java.lang.Throwable;
import java.lang.StackTraceElement;
import android.util.Log;
public void findCaller() {
final Throwable mThrowable = new Throwable();
final StackTraceElement[] elements = mThrowable.getStackTrace();
final int len = elements.length;
StackTraceElement item = null;
for (int i = 0; i < len; i++) {
item = elements[i];
Log.i("caller", "Position: " +
item.getClassName() + "." + item.getMethodName()
+ " ---" + item.getLineNumber() + " line");
}
}
本文介绍如何使用findCaller()函数直观地了解某个函数的调用路径,通过logcat查看调用过程,实现函数间的追踪。
413

被折叠的 条评论
为什么被折叠?



