Android 中如何调用 getContentResolver() ?

发布于 2024-09-24 11:19:27 字数 378 浏览 8 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

爱本泡沫多脆弱 2024-10-01 11:19:27

getContentResolver()android.content.Context 类的方法,因此要调用它,您肯定需要一个实例
上下文(例如活动或服务)。

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

雨夜星沙 2024-10-01 11:19:27

您可以这样使用:

getApplicationContext().getContentResolver()

在适当的上下文中。

You can use like this:

getApplicationContext().getContentResolver()

with the proper context.

风铃鹿 2024-10-01 11:19:27

当您使用 Cursor 对象查询 Contact 时,也会使用 getContentResolver() 方法。我使用 getContentResolver() 查询 Android 手机 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

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
千紇 2024-10-01 11:19:27
import android.content.Context;
import android.content.ContentResolver;

context = (Context)this;
ContentResolver result = (ContentResolver)context.getContentResolver();
import android.content.Context;
import android.content.ContentResolver;

context = (Context)this;
ContentResolver result = (ContentResolver)context.getContentResolver();
残龙傲雪 2024-10-01 11:19:27
  //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);
  //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);
甜心小果奶 2024-10-01 11:19:27

在 Kotlin 中访问 c​​ontentResolver、内部活动、对象类 &...:

Application().contentResolver

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

Application().contentResolver
甜柠檬 2024-10-01 11:19:27

这对我有用
获取BaseContext();

This one worked for me
getBaseContext();

久光 2024-10-01 11:19:27

解决方案是从 context 获取 ContentResolver

ContentResolver contentResolver = getContext().getContentResolver();

链接到文档:ContentResolver

A solution would be to get the ContentResolver from the context

ContentResolver contentResolver = getContext().getContentResolver();

Link to the documentation : ContentResolver

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文