How can getContentResolver() be called in Android?

Android

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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 - Android

You can use like this:

getApplicationContext().getContentResolver()

with the proper context.

Solution 3 - Android

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 - Android

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

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

Solution 5 - Android

  //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 - Android

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

Application().contentResolver

Solution 7 - Android

This one worked for me getBaseContext();

Solution 8 - Android

A solution would be to get the ContentResolver from the context

ContentResolver contentResolver = getContext().getContentResolver();

Link to the documentation : ContentResolver

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndroid_programmer_officeView Question on Stackoverflow
Solution 1 - AndroidNikolay IvanovView Answer on Stackoverflow
Solution 2 - AndroidAnkit JainView Answer on Stackoverflow
Solution 3 - AndroidAzurespotView Answer on Stackoverflow
Solution 4 - AndroidE235View Answer on Stackoverflow
Solution 5 - AndroidRangana FernandoView Answer on Stackoverflow
Solution 6 - AndroidHamed JalilianiView Answer on Stackoverflow
Solution 7 - AndroidHumxa MoghalView Answer on Stackoverflow
Solution 8 - AndroidAmul BhatiaView Answer on Stackoverflow