简体   繁体   English

如何从“非图形”类获取应用程序上下文

[英]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: 因此,有两种选择:

  1. How can I get the application's context from the "monitor" class or 如何从“监视器”类获取应用程序的上下文,或者
  2. How can I get the application's context from the "database" class 如何从“数据库”类获取应用程序的上下文

Thanks 谢谢

Doesn't the onReceive method of the BroadcastReceiver take a Context as a parameter? BroadcastReceiveronReceive方法是否不将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM