如何在 Android 中调用 getContentResolver()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3750903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:39:34  来源:igfitidea点击:

How can getContentResolver() be called in Android?

android

提问by Android_programmer_office

I want to know the context in which getContentResolver()is called?

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

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.

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

回答by Nikolay Ivanov

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

getContentResolver()是 class 的方法android.content.Context,因此要调用它,您肯定需要一个 Context 实例(例如 Activity 或 Service)。

回答by Ankit Jain

You can use like this:

你可以这样使用:

getApplicationContext().getContentResolver()

with the proper context.

有适当的上下文。

回答by Azurespot

The getContentResolver()method is also used when you query a Contact, using a Cursorobject. I have used getContentResolver()to query the Android phone Contactsapp, 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.

getContentResolver()当您Contact使用Cursor对象查询 a 时,也会使用该方法。我曾经getContentResolver()查询 Android 手机Contacts应用程序,从一个人的电话号码中查找联系信息,以包含在我的应用程序中。查询中的不同元素(如下所示)表示您想要什么样的联系方式,以及是否应该订购等。这是另一个示例

From the Content Provider Basicspage from Android docs.

来自 Android 文档的Content Provider Basics页面。

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

回答by E235

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

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

回答by Humxa Moghal

This one worked for me getBaseContext();

这个对我有用 getBaseContext();

回答by Rangana Fernando

  //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);

回答by Hamed Jaliliani

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

访问 Kotlin 中的 contentResolver 、内部活动、对象类 &... :

Application().contentResolver

回答by Amul Bhatia

A solution would be to get the ContentResolverfrom the context

一个解决方案是ContentResolvercontext

ContentResolver contentResolver = getContext().getContentResolver();

Link to the documentation : ContentResolver

文档链接:ContentResolver