android gets the method that currently runs the Activity name

  • 2020-06-15 10:10:53
  • OfStack

This example shows how android gets the name of Activity that is currently running to avoid instant messaging notifications. Share to everybody for everybody reference. The specific methods are as follows:

Recently, when doing IM, you need to know which Activity is the current Activity

Type 1: Make it 1 point easier (not available in Service)

private String getRunningActivityName() {  
        String contextString = context.toString(); 
        return contextString.substring(contextString.lastIndexOf(".") + 1, contextString.indexOf("@")); 
}

Type 2: A little more trouble, need 1 permission (recommended)
<uses-permission android:name="android.permission.GET_TASKS" /> 
private String getRunningActivityName(){          
        ActivityManager activityManager=(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 
        String runningActivity=activityManager.getRunningTasks(1).get(0).topActivity.getClassName(); 
        return runningActivity;                
}

I hope this article has been helpful for your Android programming.


Related articles: