Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the name of the current method being executed [duplicate]

Well what I am basically doing is an app which has many activities. I have a few friends with android phones and I gave them the app for testing. However, it sometimes goes into endless cycles and does strange behaviour which I am not able to understand due to their lack of programming experience and inability to dump the logcat in those particular moments.

So what I need to do is to create a static always visible window, probably popup window, that shows in which method is the program now.

So my question would be, which is the best way to achieve this functionality and how to retrieve the current method the App is in (it has several threads).

like image 976
Norbert Avatar asked Sep 21 '11 06:09

Norbert


3 Answers

Thread.currentThread().getStackTrace()[1].getMethodName()
like image 51
Karol Król Avatar answered Sep 30 '22 23:09

Karol Król


You can try this:

public String getCurrentMethod(){
    try{
        throw new Exception("");
    }catch(Exception e){
        return e.getStackTrace()[0].toString();
    }
    return ""; // This never happens
}

It will return something like this:
ClassName.methodName():123

like image 21
Sky Kelsey Avatar answered Sep 30 '22 23:09

Sky Kelsey


There are many answers for this if you search SO. This is a simple method

String name = new Object(){}.getClass().getEnclosingMethod().getName();

You should refer this post. Getting the name of the current executing method

like image 24
blessanm86 Avatar answered Sep 30 '22 22:09

blessanm86