如果你想直观的知道某个函数是如何被其他函数层层调用的,只需将如下函数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

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



