我怎么能叫getContentResolver在Android的? [英] How can I call getContentResolver in android?

查看:268
本文介绍了我怎么能叫getContentResolver在Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一库类封装了一些我的逻辑在我的第一个Android应用程序。其中一个我要封装的函数的功能是查询地址簿。因此,它需要一个 ContentResolver的。我试图找出如何保持库函数的黑盒装...也就是说,为了避免各活动通过在自己的范围内获得 ContentResolver的

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.

问题是我不能为我的生活弄清楚如何获得 ContentResolver的从我的库函数。我找不到包含 getContentResolver 导入。谷歌搜索说要用的getContext 获得上下文上调用 getContentResolver ,但我无法找到包含的getContext 为导入。接下来的帖子说要用 getSystemService 去调用对象的getContext 。但是 - 我不能找到包含 getSystemService 无论任何进口!

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!

所以我坚持想知道,我怎么能得到一个ContentResolver的封装库函数中,还是我pretty的多粘不必每次调用活动传递提到自己的上下文?

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?

我的code是东西基本上是这样的:

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就是我一直在寻找,以避免传递一个上下文的方法 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.

推荐答案

让每个库函数调用通过在一个 ContentResolver的 ...或延长应用来留住上下文和静态访问它。

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

这篇关于我怎么能叫getContentResolver在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆