简体   繁体   中英

How to get the application context from a “non-graphic” class

I have a "database" class that handles SQLites actions. 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. 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?

@Override
public void onReceive(Context context, Intent intent) {
    MyDataBase myDb = new MyDataBase(context);
    ...
}

Can't you get the context from there?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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