Android gets the name of the currently running class or method

  • 2020-06-19 11:43:51
  • OfStack

This example shows how Android gets the name of the currently running class or method. Share to everybody for everybody reference. Specific implementation methods are as follows:


public static String getCurrentMethodName() {
int level = 1;
StackTraceElement[] stacks = new Throwable().getStackTrace();
String methodName = stacks[level].getMethodName();
return methodName;
}
public static String getCurrentClassName() {
int level = 1;
StackTraceElement[] stacks = new Throwable().getStackTrace();
String className = stacks[level].getClassName();
return className;
}

Hopefully, this article has been helpful in your Android programming.


Related articles: