如何在Android中调用getContentResolver()?

And*_*ice 57 android

我想知道getContentResolver()调用的上下文?

我有一个这样的场景:
我有一个活动A,它调用myFunc()B类方法而不是活动.
所以,在BI类中必须使用getContentResolver().我直接打电话getContentResolver().它显示错误.然后我myFunc(Acitivy act)从活动中调用并调用 act.getContentResolver()它解决了我的问题.这是唯一的调用方式getContentResolver(),这意味着它可以在活动的上下文中使用,也可以单独使用.

Nik*_*nov 98

getContentResolver()是类的方法android.content.Context,所以要调用它,你肯定需要一个Context实例(例如Activity或Service).


Ank*_*ain 35

你可以像这样使用:

getApplicationContext().getContentResolver()
Run Code Online (Sandbox Code Playgroud)

有适当的背景.

  • 当你必须在一个非Activity的类中使用它时,`getApplicationContext()`也是未定义的. (21认同)
  • 在片段的上下文中,可以使用getActivity()。getContentResolver() (2认同)

Azu*_*pot 6

使用对象getContentResolver()查询a时也会使用该方法.我曾经查询Android手机应用程序,从一个人的电话号码中查找联系信息,以包含在我的应用程序中.查询中的不同元素(如下所示)表示您想要的联系人详细信息,以及是否应该对其进行排序等.这是另一个示例.ContactCursorgetContentResolver()Contacts

来自Android文档的" 内容提供商基础"页面.

// 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
Run Code Online (Sandbox Code Playgroud)