如何在android中调用getContentResolver?

发布于 2024-10-16 05:37:28 字数 1158 浏览 2 评论 0原文

我正在编写一个库类来封装我的第一个 Android 应用程序中的一些逻辑。我要封装的函数之一是查询地址簿的函数。因此,它需要一个 ContentResolver。我试图弄清楚如何将库函数保持黑盒状态...也就是说,避免让每个 Activity 在其自己的上下文中传递以获取 ContentResolver

问题是我一生都无法弄清楚如何从我的库函数中获取 ContentResolver 。我找不到包含 getContentResolver 的导入。谷歌搜索说使用 getContext 来获取要调用 getContentResolverContext,但我找不到包含 getContext< 的导入/代码> 要么。下一篇文章说使用getSystemService来获取一个对象来调用getContext。但是 - 我也找不到任何包含 getSystemService 的导入!

所以我一直想知道,如何在封装的库函数中获取 ContentResolver,或者我几乎一直在让每个调用 Activity 传递对其自身上下文的引用?

我的代码基本上是这样的:

public final class MyLibrary {
    private MyLibrary() {  }

    // take MyGroupItem as a class representing a projection
    // containing information from the address book groups
    public static ArrayList<MyGroupItem> getGroups() {
        // do work here that would access the contacts
        // thus requiring the ContentResolver
    }
}

getGroups 是我希望避免必须传递 ContextContentResolver 如果可以的话,因为我希望有的方法它完全是黑匣子。

I'm writing a library class to encapsulate some of my logic in my first Android app. One of the functions which I want to encapsulate is a function which queries the address book. As such, it needs a ContentResolver. I'm trying to figure out how to keep the library functions black-boxed... that is, to avoid having each Activity pass in its own context to get a ContentResolver.

Problem is I cannot for the life of me figure out how to get a ContentResolver from within my library function. I can't find an import that contains getContentResolver. Googling said to use getContext to get a Context on which to call getContentResolver, but I can't find an import containing getContext either. Next posts said to use getSystemService to get an object to call getContext. BUT - I can't find any import containing getSystemService either!

So I'm stuck wondering, how can I get a ContentResolver within an encapsulated library function, or am I pretty much stuck having every calling Activity pass in a reference to its own context?

My code is something basically like this:

public final class MyLibrary {
    private MyLibrary() {  }

    // take MyGroupItem as a class representing a projection
    // containing information from the address book groups
    public static ArrayList<MyGroupItem> getGroups() {
        // do work here that would access the contacts
        // thus requiring the ContentResolver
    }
}

getGroups is the method where I was looking to avoid having to pass in a Context or ContentResolver if I could, as I was hoping to have it cleanly black-boxed.

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

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

发布评论

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

评论(4

2024-10-23 05:37:28

你可以这样使用:

getApplicationContext().getContentResolver() with the proper context.
getActivity().getContentResolver() with the proper context.

You can use like this:

getApplicationContext().getContentResolver() with the proper context.
getActivity().getContentResolver() with the proper context.
陌若浮生 2024-10-23 05:37:28

让每个库函数调用传入 ContentResolver...或扩展 Application 以保留上下文并静态访问它。

Have each library function call pass in a ContentResolver... Or extend Application to keep hold of a context and access it statically.

望笑 2024-10-23 05:37:28

以下是我最后的做法,供将来可能发现此线程的任何人使用:

我使用 Sugarynugs 的方法创建一个扩展应用程序的类,然后在应用程序清单中添加适当的注册文件。我的应用程序类的代码是:

import android.app.Application;
import android.content.ContentResolver;
import android.content.Context;

public class CoreLib extends Application {
    private static CoreLib me;

    public CoreLib() {
        me = this;
    }

    public static Context Context() {
        return me;
    }

    public static ContentResolver ContentResolver() {
        return me.getContentResolver();
    }
}

然后,要在我的库类中获取 ContentResolver,我的函数代码是这样的:

public static ArrayList<Group> getGroups(){
    ArrayList<Group> rv = new ArrayList<Group>();

    ContentResolver cr = CoreLib.ContentResolver();
    Cursor c = cr.query(
        Groups.CONTENT_SUMMARY_URI, 
        myProjection, 
        null, 
        null, 
        Groups.TITLE + " ASC"
    );

    while(c.moveToNext()) {
        rv.add(new Group(
            c.getInt(0), 
            c.getString(1), 
            c.getInt(2), 
            c.getInt(3), 
            c.getInt(4))
        );          
    }

    return rv;
}

Here is how I wound up doing this, for any who may find this thread in the future:

I used sugarynugs' method of creating a class that extends Application, and then added the appropriate registration in the application manifest file. The code for my application class is then:

import android.app.Application;
import android.content.ContentResolver;
import android.content.Context;

public class CoreLib extends Application {
    private static CoreLib me;

    public CoreLib() {
        me = this;
    }

    public static Context Context() {
        return me;
    }

    public static ContentResolver ContentResolver() {
        return me.getContentResolver();
    }
}

Then, to get a ContentResolver in my library class, my function code is such:

public static ArrayList<Group> getGroups(){
    ArrayList<Group> rv = new ArrayList<Group>();

    ContentResolver cr = CoreLib.ContentResolver();
    Cursor c = cr.query(
        Groups.CONTENT_SUMMARY_URI, 
        myProjection, 
        null, 
        null, 
        Groups.TITLE + " ASC"
    );

    while(c.moveToNext()) {
        rv.add(new Group(
            c.getInt(0), 
            c.getString(1), 
            c.getInt(2), 
            c.getInt(3), 
            c.getInt(4))
        );          
    }

    return rv;
}
狼性发作 2024-10-23 05:37:28

如果不了解更多如何编码库的信息,有点困难,但我没有看到使用上下文的其他选项,因此在调用该类时传递它。

“随机”类没有获取内容解析器的环境:您需要一个上下文。

现在,将您的(活动)上下文实际传递给您的类并不太奇怪。来自 http://android-developers.blogspot.com/2009/ 01/avoiding-memory-leaks.html

在 Android 上,上下文用于许多操作,但主要用于加载和
访问资源。这就是为什么所有
小部件接收 Context 参数
他们的构造函数。在常规的
Android应用程序,你通常有
两种Context,Activity和
应用。 通常是第一个
开发者传递给的一个
需要的类和方法
上下文

(强调我的)

A bit hard without seeing more of how you are coding your library, but I don't see another option then to use the context, and so pass that when calling that class.

A 'random' class does not have the environment to get a contentresolver: you need a context.

Now it's not too strange to actually pass your (activity) context to your class. From http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html

On Android, a Context is used for many operations but mostly to load and
access resources. This is why all the
widgets receive a Context parameter in
their constructor. In a regular
Android application, you usually have
two kinds of Context, Activity and
Application. It's usually the first
one that the developer passes to
classes and methods that need a
Context

(emphasis mine)

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