动态获取活动名称 - android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10628152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Activity name dynamically - android
提问by evanmcdonnal
I'd like to get the name of the current Activity
to be sent along in the URI of an HttpRequest
. Is there a way to do this without referring specifically to the Activity
?
我想Activity
在一个HttpRequest
. 有没有办法做到这一点而不特别提到Activity
?
I know I can do myActivity.class.toString()
but this is just a less efficient way of hard coding "myActivity" since I'm making a static reference to my Activity
. Is there a more general way to do this using something like 'this' (which btw doesn't actually work here because it returns more information than what's desired).
我知道我可以做到,myActivity.class.toString()
但这只是一种对“myActivity”进行硬编码的效率较低的方法,因为我正在对我的Activity
. 有没有更通用的方法来使用类似“this”的东西(顺便说一句,这里实际上不起作用,因为它返回的信息比所需的要多)。
回答by Kevin Coppock
Use this.getClass().getSimpleName()
to get the name of the Activity.
使用this.getClass().getSimpleName()
来获取活动的名称。
From the comments, if you're in the context of an OnClickListener
(or other inner class), specify the class manually:
从评论中,如果您处于OnClickListener
(或其他内部类)的上下文中,请手动指定类:
MainActivity.class.getSimpleName()
MainActivity.class.getSimpleName()
回答by darrenp
For purists out there who may not want to use reflection, an alternative way is to use the PackageManager
as follows:
对于那些可能不想使用反射的纯粹主义者,另一种方法是使用PackageManager
如下:
PackageManager packageManager = activity.getPackageManager();
try {
ActivityInfo info = packageManager.getActivityInfo(activity.getComponentName(), 0);
Log.e("app", "Activity name:" + info.name);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
However this seems like a lot of work just to do the same as getClass().getName()
(and not even getSimpleName()
). But I guess it may be useful for someone who wants more information about the activity than just the class name.
然而,这似乎是很多工作只是为了做同样的事情getClass().getName()
(甚至不是getSimpleName()
)。但我想它可能对那些想要更多关于活动的信息而不仅仅是类名的人有用。
回答by RJ CR
ActivityManager am = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
this.currentActivity = taskInfo.get(0).topActivity.getClassName();
Log.i( "CURRENT Activity ", currentActivity);
回答by Mahmoud Ayman
First:open your manifest.xml
file
第一:打开你的manifest.xml
文件
you will find your package name i.e. "com.company.projectname"
你会找到你的包名,即 "com.company.projectname"
Then:lets say your activity name is MainActivity
然后:假设您的活动名称是MainActivity
MainActivity.class.getCanonicalName() >output> "com.company.projectname.MainActivity"
OR
或者
MainActivity.class.getSimpleName() >output> "MainActivity"
OR
或者
MainActivity.class.getName() >output> "com.company.projectname.MainActivity"
回答by Pierre
For Xamarin
对于 Xamarin
string GetActivityClassName(Activity activity) //Receiving LoginActivity
{
//ComponentName
activity.ComponentName; //Output: "md5101a0260d0a0e5d40a0a9009be09b0c2.LoginActivity"
//LocalClassName
activity.LocalClassName; //Output: "md5101a0260d0a0e5d40a0a9009be09b0c2.LoginActivity"
//Java.Lang.Class.FromType.SimpleName
Java.Lang.Class.FromType(activity.GetType()).SimpleName; //Output "LoginActivity"
//Type.Name
activity.GetType().Name; //Output "LoginActivity"
}