有没有办法获取我们在 Android 中使用 getContext() 的上下文的名称?

Is there a way to get the name of the context we're in using getContext() in Android?

我昨天才开始 Android 开发,我已经学到了很多东西。我有点被困在一件事上。这是 "this" 关键字(我认为它与 getContext() 相同)以及上下文是什么。我不会问这个,因为它已经被问了很多。我想我知道上下文是什么,也可能不是。我认为如果我可以通过某种方式获取名称来查看当前上下文是什么,那将是很好的,如果可能的话。

例如,在加载内容后启动一个空白的默认应用程序。我想打电话给 this/getContext() 并在可能的情况下获取名称。我希望 "Main Activity" 之类的内容作为当前上下文出现在我的 LogCat 中,对吗?

也许代码示例会有所帮助(来自默认代码)

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d("Main Activity", getContext().getTitle()); //getContext().getTitle() is MADE UP
}
.......

谢谢。

您可以在 class 上使用 getName()getSimpleName():

this.getClass().getSimpleName()

来自 getName() 的 Javadoc:

Returns the name of the class represented by this Class. For a description of the format which is used, see the class definition of Class.

getSimpleName() 的 Javadoc:

Returns the simple name of the class represented by this Class as defined in the source code. If there is no name (that is, the class is anonymous) then an empty string is returned. If the receiver is an array then the name of the underlying type with square braces appended (for example "Integer[]") is returned.