6

我尝试获取所有联系人姓名和电话号码,我正在尝试使用getContentResolver,但我得到了

方法获取内容解析器()未定义类型

这个错误。

我该如何解决?

这是下面的代码:

public class ContactManager  {

public ArrayList<Product> getContactNumber() {
    Cursor phones = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);
    while (phones.moveToNext()) {
        String name = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    }
    phones.close();
}

}

4

2 回答 2

13

问题是Context,传递你在它的构造函数Activity中使用你的上下文:Class

Context context;
public ContactManager (Context context) {
    this.context = context;
}

然后使用

context.getContentResolver()

这里绝对完美地使用上下文。

于 2013-08-12T08:30:54.713 回答
0

你也可以简单地使用这个:

 public class ContactManager  {

  public ArrayList<Product> getContactNumber(Context mContext) {
    Cursor phones = mContext.getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
        null, null);
    while (phones.moveToNext()) {
    String name = phones
            .getString(phones
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    String phoneNumber = phones
            .getString(phones
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
   phones.close();}}
于 2013-08-12T08:35:41.720 回答