Android essay: Get the called information of the current function

         public String  getMyGrandpaStackTrace(){
             StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
             StackTraceElement father = stackTrace[1];
             StackTraceElement log = stackTrace[2];
             String tag = null;
             for (int i = 1; i < stackTrace.length; i++) {
                 StackTraceElement e = stackTrace[i];
                 if (!e.getClassName().equals(log.getClassName())) {
                     tag = e.getClassName() + "." + e.getMethodName();
                     break;
                 }
             }
             if (tag == null) {
                 tag = log.getClassName() + "." + log.getMethodName();

             }
             System.err.println(String.format("My father  is %s.%s", father.getClassName() ,father.getMethodName()));
             System.err.println(String.format("My grandpa is %s",tag));
             return tag;
         }

Check which class and which method is called

Guess you like

Origin blog.csdn.net/hmz0303hf/article/details/125080829