【问题标题】:How can I call getContentResolver in android?如何在 android 中调用 getContentResolver?
【发布时间】:2011-02-04 06:16:23
【问题描述】:

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

问题是我一生都无法弄清楚如何从我的库函数中获取ContentResolver。我找不到包含getContentResolver 的导入。谷歌搜索说使用getContext 获取Context 以调用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 是我希望避免传入 ContextContentResolver 的方法,因为我希望能将其彻底黑盒化。

【问题讨论】:

    标签: android android-context android-contentresolver


    【解决方案1】:

    你可以这样使用:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 扩展Application 的其他潜在副作用或“陷阱”是什么?有什么我需要在析构函数或任何东西中清理的东西吗?
      • 好的,所以我正在尝试走这条路。我有一个extends Application 的类,并且我在应用程序清单文件中添加了完全限定的类名android:name。所以现在在我的库类中,我试图调用getApplication(),但它没有找到它作为一种方法,并且没有给我任何需要添加导入的重构提示。如何拨打getApplication() 获取应用句柄?
      【解决方案3】:

      对于以后可能会找到此线程的任何人,我都是这样做的:

      我使用sugarynugs 的方法创建了一个extends Application 的类,然后在应用程序清单文件中添加了相应的注册。我的应用程序类的代码是:

      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;
      }
      

      【讨论】:

      • 如何管理这个权限?如果您想限制某些使用您的库的应用的数据暴露,我们该如何实现?
      【解决方案4】:

      看不到更多关于如何编写库的内容有点困难,但我没有看到使用上下文的其他选项,因此在调用该类时传递它。

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

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

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

      (强调我的)

      【讨论】:

      • 我更新了一小段代码,显示了我正在尝试做的事情。
      • 我认为传递上下文以便您可以使用它来获取 contentresolver 是您的解决方案:)
      猜你喜欢
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多