String msgtoprint = Thread.CurrentThread (). Getstacktrace () [3]. Getmethodname ();
Is the method name at the time of invocation.
The first method of the thread class that is used:
public static Thread CurrentThread ()
Returns the current thread object.
Instance code:
Package Com.loaderman.getthreadstatusdemo;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;public class Mainactivity extends Appcompatactivity {@Override protected void onCreate (Bundle sav Edinstancestate) {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 ()); } }}
Log output
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 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 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 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 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 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 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 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 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
You can find that when the statement that gets the stack trace is placed in another method (tool class method), the element with the array index of 3 can find the calling location of the tool class method.
In this example, the 12th line in the OnCreate () method is found.
The source of this study: http://www.jianshu.com/p/71fefdf6d62c
Gets the current thread state--thread class