I have a ContentProvider Class and a DatabaseHelper Class (extends SQLiteOpenHelper). The ContentProvider instantiates the Helper which needs access to a Context because the constructor requires it:
public DBHelper(Context context, AssetFileDescriptor db_asset) {
super(context, DB_NAME, null, 1);
Do you know at least a single way to get the Context from the ContentProvider?
Thanks :)
Accessing a provider. When you want to access data in a content provider, you use the ContentResolver object in your application's Context to communicate with the provider as a client. The ContentResolver object communicates with the provider object, an instance of a class that implements ContentProvider .
There are mainly two types of Context that are available in Android.
getContext() will get the context of the class that extends the ContentProvider..
getContext() This method can be called on a View like textView. getContext() . This will give the context of activity in which the view is currently hosted in.
In your ContentProvider.onCreate method you can pass the result of getContext() to the DBHelper
@Override
public boolean onCreate() {
dbHelper = new DBHelper(getContext(), db_asset);
return true;
}
Do you know at least a single way to get the Context from the ContentProvider?
ContentProvider:getContext()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With