如何以编程方式获取应用程序类名称

时间:2019-01-10 06:23:52

标签: java android

我需要在运行时为Android应用程序获取应用程序的子类名称;

例如:public class TargetApplicationClass extends Application{...}

我需要获取TargetApplicationClass的字符串值,

有什么办法可以做到吗?

让我提供有关我的案件的更多详细信息: 我不在一个已经有TargetApplicationClass上下文的简单android应用程序上工作;我正在使用一个对应用程序类有0知识的注入库;我可以通过

枚举并获取一些信息
app = ActivityThread.currentApplication();
context = app.getApplicationContext();
...

问题是以下代码没有必要返回正确的应用程序类名称:

app.getClass().getName()

3 个答案:

答案 0 :(得分:1)

TargetApplicationClass.getClass().getName() // use anyone as per your usecase
//TargetApplicationClass.getClass().getCanonicalName()
//TargetApplicationClass.getClass().getSimpleName()

这应该有效

答案 1 :(得分:0)

我确定可以通过Google进行快速搜索。但是,您要进行this.getClass().getName()并确保它不在静态上下文中运行

答案 2 :(得分:0)

如果您想使用该名称作为标签,则可以使用

private String TAG = TargetApplicationClass.class.getSimpleName();

但是,如果您想获得用于动态加载类的名称, 那么您可以使用

private String Name = TargetApplicationClass.class.getName();

有关更多信息,请参见此示例

System.out.println(String.class.getName());
System.out.println(String.class.getCanonicalName());
System.out.println(String.class.getSimpleName());

输出

java.lang.String
java.lang.String
String