问题描述
我想获取要在 HttpRequest
的 URI 中发送的当前 Activity
的名称.有没有办法在不特别提到 Activity
的情况下做到这一点?
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
?
我知道我可以做 myActivity.class.toString()
但这只是硬编码myActivity"的一种效率较低的方式,因为我正在对我的 Activity 进行静态引用代码>.有没有更通用的方法来使用类似this"的东西(顺便说一句,在这里实际上不起作用,因为它返回的信息比所需的要多).
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).
推荐答案
使用 this.getClass().getSimpleName()
获取 Activity 的名称.
Use this.getClass().getSimpleName()
to get the name of the Activity.
根据评论,如果您处于 OnClickListener
(或其他内部类)的上下文中,请手动指定类:
From the comments, if you're in the context of an OnClickListener
(or other inner class), specify the class manually:
MainActivity.class.getSimpleName()
这篇关于动态获取活动名称 - android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!