Android 正在尝试查询联系人列表

Android 正在尝试查询联系人列表,android,android-contacts,Android,Android Contacts,根据我之前得到的答案,我在广播接收机中有以下代码: Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(ASenderTel)); // Also tried; //ContentResolver cr = getContentResolver(); //Context c = this; Cursor c = getContentResolver().query(lookupUri, n

根据我之前得到的答案,我在广播接收机中有以下代码:

Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(ASenderTel));
// Also tried;
//ContentResolver cr = getContentResolver();
//Context c = this;
Cursor c = getContentResolver().query(lookupUri, new String[] { PhoneLookup._ID }, null, null, null);
return (c.getCount() > 0);
…但获取错误消息,“类型KITSMSReceiver的方法getContentResolver()未定义”

getContentResolver()是来自android.content.Context类的方法。例如,您可以从活动中访问它。要实现此目标,请将广播接收器放入活动类中:

速递稿:

public class MyActivity extends Activity {

  // ...

  private BroadcastReceiver myReceiver = new BroadcastReceiver() {
    public void onReceive(Context c, Intent i) {
      MyActivity.this.recvBroadcast(i); // forward to your activity
      MyActivity.this.getContentResolver();  // <-----
    }
  }};

  // ...

}
公共类MyActivity扩展活动{
// ...
private BroadcastReceiver myReceiver=新的BroadcastReceiver(){
接收时公共无效(上下文c,意图i){
MyActivity.this.recvBroadcast(i);//转到您的活动

MyActivity.this.getContentResolver();//如果我使用:ContentResolver cr=getContentResolver().query(lookupUri,新字符串[]{PhoneLookup.\u ID},null,null,null);…错误消息是,“类型KITSMSReceiver的方法getContentResolver()未定义”(KitsMSRSReceiver是广播接收器)这看起来非常复杂;这真的是“首选方法”吗?私有BroadcastReceiver对应该调用它的事件可见吗?实际上,您可以使用onReceive方法附带的上下文参数,并将您的BroadcastReceiver放在单独的文件中