String msgToPrint = Thread.currentThread().getStackTrace()[3] .getMethodName();
就是调用时的方法名。
其中使用的Thread类的第一个方法:
public static Thread currentThread()
返回当前线程对象。
实例代码:
package com.loaderman.getthreadstatusdemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); printStackInfos(); } private void printStackInfos() { StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); System.out.println("Called in printStackInfos() method!!!!!!!"); System.out.println("The stackTraceElements length: " + stackTraceElements.length); for (int i = 0; i < stackTraceElements.length; ++i) { System.out.println("---- the " + i + " element ----"); System.out.println("toString: " + stackTraceElements[i].toString()); System.out.println("ClassName: " + stackTraceElements[i].getClassName()); System.out.println("FileName: " + stackTraceElements[i].getFileName()); System.out.println("LineNumber: " + stackTraceElements[i].getLineNumber()); System.out.println("MethodName: " + stackTraceElements[i].getMethodName()); } } }
日志输出
03-17 04:48:10.088 701-701/com.loaderman.getthreadstatusdemo I/System.out: Called in printStackInfos() method!!!!!!!
03-17 04:48:10.098 701-701/com.loaderman.getthreadstatusdemo I/System.out: The stackTraceElements length: 18
03-17 04:48:10.108 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 0 element ----
03-17 04:48:10.108 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: dalvik.system.VMStack.getThreadStackTrace(Native Method)
03-17 04:48:10.108 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: dalvik.system.VMStack
03-17 04:48:10.108 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: VMStack.java
03-17 04:48:10.118 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: -2
03-17 04:48:10.118 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: getThreadStackTrace
03-17 04:48:10.118 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 1 element ----
03-17 04:48:10.138 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: java.lang.Thread.getStackTrace(Thread.java:591)
03-17 04:48:10.138 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: java.lang.Thread
03-17 04:48:10.148 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: Thread.java
03-17 04:48:10.148 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 591
03-17 04:48:10.148 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: getStackTrace
03-17 04:48:10.148 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 2 element ----
03-17 04:48:10.148 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: com.loaderman.getthreadstatusdemo.MainActivity.printStackInfos(MainActivity.java:16)
03-17 04:48:10.148 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: com.loaderman.getthreadstatusdemo.MainActivity
03-17 04:48:10.158 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: MainActivity.java
03-17 04:48:10.158 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 16
03-17 04:48:10.168 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: printStackInfos
03-17 04:48:10.168 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 3 element ----
03-17 04:48:10.168 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: com.loaderman.getthreadstatusdemo.MainActivity.onCreate(MainActivity.java:12)
03-17 04:48:10.168 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: com.loaderman.getthreadstatusdemo.MainActivity
03-17 04:48:10.168 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: MainActivity.java
03-17 04:48:10.168 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 12
03-17 04:48:10.179 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: onCreate
03-17 04:48:10.179 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 4 element ----
03-17 04:48:10.179 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.app.Activity.performCreate(Activity.java:5008)
03-17 04:48:10.188 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.app.Activity
03-17 04:48:10.188 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: Activity.java
03-17 04:48:10.198 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 5008
03-17 04:48:10.198 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: performCreate
03-17 04:48:10.198 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 5 element ----
03-17 04:48:10.208 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-17 04:48:10.208 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.app.Instrumentation
03-17 04:48:10.208 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: Instrumentation.java
03-17 04:48:10.208 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 1079
03-17 04:48:10.218 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: callActivityOnCreate
03-17 04:48:10.218 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 6 element ----
03-17 04:48:10.218 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-17 04:48:10.228 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.app.ActivityThread
03-17 04:48:10.228 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: ActivityThread.java
03-17 04:48:10.228 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 2023
03-17 04:48:10.228 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: performLaunchActivity
03-17 04:48:10.228 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 7 element ----
03-17 04:48:10.238 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-17 04:48:10.238 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.app.ActivityThread
03-17 04:48:10.238 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: ActivityThread.java
03-17 04:48:10.238 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 2084
03-17 04:48:10.248 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: handleLaunchActivity
03-17 04:48:10.248 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 8 element ----
03-17 04:48:10.248 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.app.ActivityThread.access$600(ActivityThread.java:130)
03-17 04:48:10.258 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.app.ActivityThread
03-17 04:48:10.258 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: ActivityThread.java
03-17 04:48:10.258 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 130
03-17 04:48:10.258 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: access$600
03-17 04:48:10.268 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 9 element ----
03-17 04:48:10.268 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-17 04:48:10.268 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.app.ActivityThread$H
03-17 04:48:10.268 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: ActivityThread.java
03-17 04:48:10.268 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 1195
03-17 04:48:10.268 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: handleMessage
03-17 04:48:10.278 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 10 element ----
03-17 04:48:10.278 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.os.Handler.dispatchMessage(Handler.java:99)
03-17 04:48:10.278 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.os.Handler
03-17 04:48:10.278 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: Handler.java
03-17 04:48:10.288 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 99
03-17 04:48:10.288 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: dispatchMessage
03-17 04:48:10.288 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 11 element ----
03-17 04:48:10.288 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.os.Looper.loop(Looper.java:137)
03-17 04:48:10.288 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.os.Looper
03-17 04:48:10.288 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: Looper.java
03-17 04:48:10.288 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 137
03-17 04:48:10.288 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: loop
03-17 04:48:10.300 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 12 element ----
03-17 04:48:10.300 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.app.ActivityThread.main(ActivityThread.java:4745)
03-17 04:48:10.308 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.app.ActivityThread
03-17 04:48:10.308 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: ActivityThread.java
03-17 04:48:10.308 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 4745
03-17 04:48:10.308 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: main
03-17 04:48:10.308 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 13 element ----
03-17 04:48:10.308 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: java.lang.reflect.Method.invokeNative(Native Method)
03-17 04:48:10.308 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: java.lang.reflect.Method
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: Method.java
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: -2
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: invokeNative
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 14 element ----
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: java.lang.reflect.Method.invoke(Method.java:511)
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: java.lang.reflect.Method
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: Method.java
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 511
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: invoke
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 15 element ----
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: com.android.internal.os.ZygoteInit$MethodAndArgsCaller
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: ZygoteInit.java
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 786
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: run
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 16 element ----
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: com.android.internal.os.ZygoteInit
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: ZygoteInit.java
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 553
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: main
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: ---- the 17 element ----
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: dalvik.system.NativeStart.main(Native Method)
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: dalvik.system.NativeStart
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: NativeStart.java
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: -2
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: main
可以发现当获取stack trace的语句放在另一个方法(工具类方法)中的时候,数组索引为3的元素可以找到该工具类方法的调用地点。
在此例中,找到的是onCreate()方法中的第12行。
本文学习来源:http://www.jianshu.com/p/71fefdf6d62c