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.
This question is related to
android
Access contentResolver in Kotlin , inside activities, Object classes &... :
Application().contentResolver
//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);
A solution would be to get the ContentResolver
from the context
ContentResolver contentResolver = getContext().getContentResolver();
Link to the documentation : ContentResolver
You can use like this:
getApplicationContext().getContentResolver()
with the proper context.
This one worked for me getBaseContext();
import android.content.Context;
import android.content.ContentResolver;
context = (Context)this;
ContentResolver result = (ContentResolver)context.getContentResolver();
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
Source: Stackoverflow.com