我创建了一个这样的自定义应用程序类:
class A extends android.app.Application{
public String abc = "xyz";
}
我有一个简单的java类
class B {
private appContext;
// This constructor is called from activity.
B(Context ctx){
this.appContext = ctx;
}
private void foo(){
// want to access Class A's abc String vairable Here...HOW TO DO THAT?????
}
}
如何在foo方法中访问A类的abc字符串vairable。
最佳答案
通过良好的转换,您可以从getApplicationContext
获得带有Context
的Application类。
((A) this.ctx.getApplicationContext()).abc;