getcontentresolver()未定义类型

mst*_*fdz 6 android android-contentresolver

我尝试获取所有联系人姓名和号码,我正在尝试使用,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();
}
Run Code Online (Sandbox Code Playgroud)

}

Ara*_* GM 13

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

Context context;
public ContactManager (Context context) {
    this.context = context;
}
Run Code Online (Sandbox Code Playgroud)

然后用

context.getContentResolver()

这里绝对完善了上下文的使用.