'How can getContentResolver() be called in Android?

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.



Solution 1:[1]

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

Solution 2:[2]

You can use like this:

getApplicationContext().getContentResolver()

with the proper context.

Solution 3:[3]

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

Solution 4:[4]

import android.content.Context;
import android.content.ContentResolver;

context = (Context)this;
ContentResolver result = (ContentResolver)context.getContentResolver();

Solution 5:[5]

  //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);

Solution 6:[6]

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

Application().contentResolver

Solution 7:[7]

This one worked for me getBaseContext();

Solution 8:[8]

A solution would be to get the ContentResolver from the context

ContentResolver contentResolver = getContext().getContentResolver();

Link to the documentation : ContentResolver

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nikolay Ivanov
Solution 2 duggu
Solution 3 Community
Solution 4 E235
Solution 5 Rangana Fernando
Solution 6 Hamed Jaliliani
Solution 7 Humxa Moghal
Solution 8 An-droid