[英]How to get the application context from a “non-graphic” class
I have a "database" class that handles SQLites actions. 我有一个处理SQLite操作的“数据库”类。 Part of its constructor is the application's context.
其构造函数的一部分是应用程序的上下文。 So far so good but now, I have a "monitor" class that extends BroadcastReceiver that needs to use this database class and I have no context to build that instance.
到目前为止,还不错,但是现在,我有一个“监视器”类,它扩展了需要使用该数据库类的BroadcastReceiver,并且我没有上下文来构建该实例。 So there are two alternatives:
因此,有两种选择:
Thanks 谢谢
Doesn't the onReceive
method of the BroadcastReceiver
take a Context
as a parameter? BroadcastReceiver
的onReceive
方法是否不将Context
作为参数?
@Override
public void onReceive(Context context, Intent intent) {
MyDataBase myDb = new MyDataBase(context);
...
}
Can't you get the context
from there? 您不能从那里得到
context
吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.