【问题标题】:How can getContentResolver() be called in Android?如何在 Android 中调用 getContentResolver()?
【发布时间】:2011-04-14 15:13:33
【问题描述】:

我想知道getContentResolver() 被调用的上下文?

我有这样一个场景:
我有一个活动 A,它调用 B 类的方法 myFunc(),这不是一个活动。
因此,在 B 类中,我必须使用 getContentResolver()。我直接打电话给getContentResolver()。它显示错误。然后我从活动中拨打myFunc(Acitivy act) 并拨打act.getContentResolver() 解决了我的问题。这是调用getContentResolver()的唯一方法吗,这意味着它可以在活动上下文中使用,也可以单独使用。

【问题讨论】:

    标签: android


    【解决方案1】:

    getContentResolver() 是类android.content.Context 的方法,所以要调用它,你肯定需要一个实例 上下文(例如 Activity 或 Service)。

    【讨论】:

      【解决方案2】:

      你可以这样使用:

      getApplicationContext().getContentResolver()
      

      使用适当的上下文。

      【讨论】:

      • getApplicationContext() 在非 Activity 类中使​​用时也是未定义的。
      • 是的,在这种情况下你不能使用这种方式
      • 从片段的上下文中你可以使用 getActivity().getContentResolver()
      • 使用 getContext() @PawełGościcki
      • @PawełGościcki 但大多数情况下会在活动课程中使用它。因此,对此的高票数。但是你的观点被注意到了。谢谢。
      【解决方案3】:

      当您使用Cursor 对象查询Contact 时,也会使用getContentResolver() 方法。我使用getContentResolver() 查询Android 手机Contacts 应用程序,从一个人的电话号码中查找联系信息,以包含在我的应用程序中。查询中的不同元素(如下所示)代表您想要什么样的联系方式,以及是否应该订购等。这里是另一个example

      来自 Android 文档的 Content Provider Basics 页面。

      // Queries the user dictionary and returns results
      mCursor = getContentResolver().query(
          UserDictionary.Words.CONTENT_URI,   // The content URI of the words table
          mProjection,                        // The columns to return for each row
          mSelectionClause                    // Selection criteria
          mSelectionArgs,                     // Selection criteria
          mSortOrder);                        // The sort order for the returned rows
      

      【讨论】:

        【解决方案4】:
        import android.content.Context;
        import android.content.ContentResolver;
        
        context = (Context)this;
        ContentResolver result = (ContentResolver)context.getContentResolver();
        

        【讨论】:

        • 谢谢,不知道为什么没人说从哪里导入。
        【解决方案5】:

        访问 Kotlin 中的 contentResolver 、内部活动、对象类和...:

        Application().contentResolver
        

        【讨论】:

        • 我不知何故认为这不是一个好主意。可以说蜘蛛的感觉。无论如何感谢 Kotlin。
        【解决方案6】:

        这个对我有用 getBaseContext();

        【讨论】:

          【解决方案7】:
            //create activity object to get activity from Activity class for use to content resolver
              private final Activity ActivityObj;
          
            //create constructor with ActivityObj to get activity from Activity class
              public RecyclerViewAdapterClass(Activity activityObj) {
                  this.ActivityObj = activityObj;
              }
          
          
               ActivityObj.getContentResolver(),.....,.....,null);
          

          【讨论】:

          • 您正在回答一个 7 年前的问题,这是一个很好且被接受的答案,以及许多其他涵盖更多选项的答案。此外,您的答案只是带有 2 个 cmets 的代码 sn-p。在代码中添加一些解释性文本通常是一个好主意(或者更好的是,编写一个文本答案,在其中添加代码以显示您正在解释的内容)。如果您想回答问题,这很好,请尝试关注最近的(最好是未回答的)问题。如果您回答的问题已经有答案,请尝试确保您的问题涵盖了分数,而其他答案则没有。
          【解决方案8】:

          解决方案是从context 获取ContentResolver

          ContentResolver contentResolver = getContext().getContentResolver();
          

          文档链接:ContentResolver

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-08-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多