android中获取当前activity的名称

之前看到网上和教程中通常的做法如下:


private String getRunningActivityName(){
ActivityManager activityManager=(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
String runningActivity=activityManager.getRunningTasks(1).get(0).topActivity.getClassName();
return runningActivity;
}

这个方法不好的是需要在AndroidManifest.xml里面定义权限


事实上有更简单的方法,我一般都这么干!

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

既不用定义权限,也代码简单。

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image