How can getContentResolver() be called in Android?

前端 未结 8 1189
庸人自扰
庸人自扰 2020-12-02 16:58

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 myFu

相关标签:
8条回答
  • 2020-12-02 16:59

    You can use like this:

    getApplicationContext().getContentResolver()
    

    with the proper context.

    0 讨论(0)
  • 2020-12-02 17:07

    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
    
    0 讨论(0)
  • 2020-12-02 17:10

    A solution would be to get the ContentResolver from the context

    ContentResolver contentResolver = getContext().getContentResolver();
    

    Link to the documentation : ContentResolver

    0 讨论(0)
  • 2020-12-02 17:19

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

    0 讨论(0)
  • 2020-12-02 17:19

    This one worked for me getBaseContext();

    0 讨论(0)
  • 2020-12-02 17:21
    import android.content.Context;
    import android.content.ContentResolver;
    
    context = (Context)this;
    ContentResolver result = (ContentResolver)context.getContentResolver();
    
    0 讨论(0)
提交回复
热议问题