How can getContentResolver() be called in Android?

Viewed 159381

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.

8 Answers
import android.content.Context;
import android.content.ContentResolver;

context = (Context)this;
ContentResolver result = (ContentResolver)context.getContentResolver();
  //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);

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

Application().contentResolver

This one worked for me getBaseContext();

A solution would be to get the ContentResolver from the context

ContentResolver contentResolver = getContext().getContentResolver();

Link to the documentation : ContentResolver

Related