获取上下文中的非活性类 [英] get Context in non-Activity class
本文介绍了获取上下文中的非活性类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在一个Android应用程序,有没有什么办法让android的情况下在非活性类,如果该活动的类名被称为?
In an android Application, is there any way to get the context in android in a non-activity class if the activity class name is known?
推荐答案
如果你的类是非活性的类,并且从activiy创建它的实例,您可以按如下通过后的构造函数通过上下文的实例
If your class is non-activity class, and creating an instance of it from the activiy, you can pass an instance of context via constructor of the later as follows:
class YourNonActivityClass{
// variable to hold context
private Context context;
//save the context recievied via constructor in a local variable
public YourNonActivityClass(Context context){
this.context=context;
}
}
您可以从该活动创建该类的实例如下:
You can create instance of this class from the activity as follows:
new YourNonActivityClass(this);
这篇关于获取上下文中的非活性类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文