Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Context in ContentProvider

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 :)

like image 632
Julian Avatar asked Aug 02 '11 22:08

Julian


People also ask

How do I access my content provider?

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 .

How many types of context are there in Android?

There are mainly two types of Context that are available in Android.

Which of the following is the correct approach for accessing the context in Android content provider?

getContext() will get the context of the class that extends the ContentProvider..

How do I get current context in Kotlin?

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.


2 Answers

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;
    }
like image 122
Rob Avatar answered Sep 25 '22 00:09

Rob


Do you know at least a single way to get the Context from the ContentProvider?

ContentProvider:getContext()

like image 32
citizen conn Avatar answered Sep 23 '22 00:09

citizen conn