如何在android中调用getContentResolver?
我正在编写一个库类来封装我的第一个 Android 应用程序中的一些逻辑。我要封装的函数之一是查询地址簿的函数。因此,它需要一个 ContentResolver。我试图弄清楚如何将库函数保持黑盒状态...也就是说,避免让每个 Activity
在其自己的上下文中传递以获取 ContentResolver
。
问题是我一生都无法弄清楚如何从我的库函数中获取 ContentResolver
。我找不到包含 getContentResolver
的导入。谷歌搜索说使用 getContext
来获取要调用 getContentResolver
的 Context
,但我找不到包含 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 是我希望避免必须传递 Context
或 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
.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以这样使用:
You can use like this:
让每个库函数调用传入
ContentResolver
...或扩展Application
以保留上下文并静态访问它。Have each library function call pass in a
ContentResolver
... Or extendApplication
to keep hold of a context and access it statically.以下是我最后的做法,供将来可能发现此线程的任何人使用:
我使用 Sugarynugs 的方法创建一个
扩展应用程序
的类,然后在应用程序清单中添加适当的注册文件。我的应用程序类的代码是:然后,要在我的库类中获取 ContentResolver,我的函数代码是这样的:
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:Then, to get a ContentResolver in my library class, my function code is such:
如果不了解更多如何编码库的信息,有点困难,但我没有看到使用上下文的其他选项,因此在调用该类时传递它。
“随机”类没有获取内容解析器的环境:您需要一个上下文。
现在,将您的(活动)上下文实际传递给您的类并不太奇怪。来自 http://android-developers.blogspot.com/2009/ 01/avoiding-memory-leaks.html
(强调我的)
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
(emphasis mine)