getContentResolver()不起作用

时间:2014-06-08 17:45:38

标签: java android

我有一个班级名称MyPhoneStateListener。在这里,我想使用getContentResolver,但是课堂上有一些问题。这不是我的主要活动。

Cursor cur = getContentResolver()
         .query(Calls.CONTENT_URI, projection, null, null, Calls.DATE +" desc");

1 个答案:

答案 0 :(得分:0)

要在另一个未扩展getContentResolver / Activity的类中使用Service,您应该拥有Context个对象。

MyPhoneStateListener课程内,在您的构造函数中请求Context并保存,然后在需要content resolver时使用context.getContentResolver()

示例:

public class MyPhoneStateListener {
 private Context context;
 public MyPhoneStateListener(Context context) {
   this.context = context;
 }

 public void someMethod() {
    Cursor cur = context.getContentResolver()
     .query(Calls.CONTENT_URI, projection, null, null, Calls.DATE +" desc");
 }
}
相关问题