79

I want to know the context in which getContentResolver() is called?

I have a scenario like this:
I have an activity A that calls a method myFunc() of class B which is not an activity.
So, in class B I have to use getContentResolver(). I directly called getContentResolver(). It was showing error. Then I called myFunc(Acitivy act) from the activity and called act.getContentResolver() which solved my problem. Is this the only way to call getContentResolver(), which means it can be used in context with activity or can be used alone.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228

8 Answers8

130

getContentResolver() is method of class android.content.Context, so to call it you definitely need an instance of Context ( Activity or Service for example).

Nikolay Ivanov
  • 8,897
  • 3
  • 31
  • 34
53

You can use like this:

getApplicationContext().getContentResolver()

with the proper context.

duggu
  • 37,851
  • 12
  • 116
  • 113
Ankit Jain
  • 2,230
  • 1
  • 18
  • 25
8

The getContentResolver()method is also used when you query a Contact, using a Cursor object. I have used getContentResolver() to query the Android phone Contacts app, looking for contact info from a person's phone number, to include in my app. The different elements in a query (as shown below) represent what kind of contact details you want, and if they should be ordered, etc. Here is another example.

From the Content Provider Basics page from Android docs.

// Queries the user dictionary and returns results
mCursor = getContentResolver().query(
    UserDictionary.Words.CONTENT_URI,   // The content URI of the words table
    mProjection,                        // The columns to return for each row
    mSelectionClause                    // Selection criteria
    mSelectionArgs,                     // Selection criteria
    mSortOrder);                        // The sort order for the returned rows
Community
  • 1
  • 1
Azurespot
  • 3,066
  • 3
  • 45
  • 73
4
import android.content.Context;
import android.content.ContentResolver;

context = (Context)this;
ContentResolver result = (ContentResolver)context.getContentResolver();
E235
  • 11,560
  • 24
  • 91
  • 141
1
  //create activity object to get activity from Activity class for use to content resolver
    private final Activity ActivityObj;

  //create constructor with ActivityObj to get activity from Activity class
    public RecyclerViewAdapterClass(Activity activityObj) {
        this.ActivityObj = activityObj;
    }


     ActivityObj.getContentResolver(),.....,.....,null);
  • You are answering a 7-year-old question which as a good and accepted answer, as well as a lot of other answers covering more options. Additionally, your answer is just a code snippet with 2 comments. It is generally a good idea to add some explaining text to your code (or even better, write a text answer to which you add code to show, what you are explaining). If you want to answer questions, which is good, try to focus on more recent (preferably unanswered) questions. And if you answer a question which already has answers, try to make sure yours covers points, the other answers do not. – Leon Aug 29 '18 at 20:39
1

Access contentResolver in Kotlin , inside activities, Object classes &... :

Application().contentResolver
Hamed Jaliliani
  • 2,789
  • 24
  • 31
0

This one worked for me getBaseContext();

Humxa Moghal
  • 122
  • 11
0

A solution would be to get the ContentResolver from the context

ContentResolver contentResolver = getContext().getContentResolver();

Link to the documentation : ContentResolver

An-droid
  • 6,433
  • 9
  • 48
  • 93
Amul Bhatia
  • 154
  • 1
  • 11