我正在编写一个库类来将我的一些逻辑封装在我的第一个 Android 应用程序中。我要封装的功能之一是查询通讯录的功能。因此,它需要一个ContentResolver
. 我试图弄清楚如何将库函数保持为黑盒状态......也就是说,避免每次Activity
通过在自己的上下文中获取ContentResolver
.
问题是我一生都无法弄清楚如何ContentResolver
从我的库函数中获取一个。我找不到包含getContentResolver
. 谷歌搜索说用来getContext
获取Context
on which to call getContentResolver
,但我找不到包含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 是我希望避免传入 aContext
或ContentResolver
如果可以的话的方法,因为我希望将它完全黑盒化。