无法从Android获取所有联系人

时间:2015-03-20 07:04:28

标签: android cursor android-contacts android-contentresolver

我需要获取Android设备上的所有联系人。但是当我使用contentResolver查询时,我只获得新创建的联系人。即使设备上存在多个联系人。以下是我用来获取联系人的查询。光标不会返回所有光标。

我也观察过一件事。如果我去编辑未获取的联系人,则会获取该特定联系人。帮助我获得所有联系人。我错过了什么吗?

String[] projection = new String[] { Contacts.LOOKUP_KEY };
            Cursor people = mContext.getContentResolver().query(Contacts.CONTENT_URI, projection, null, null, null);

1 个答案:

答案 0 :(得分:0)

试试这个。

public class ContactActivity extends Activity {

Cursor cursor;
ArrayList<String> NameList=new ArrayList<String>();
ArrayList<String> PhoneList=new ArrayList<String>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

            cursor = getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);

    while (cursor.moveToNext()) {
        int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);   
        int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);

        String name = cursor.getString(nameIdx);
        String phone = cursor.getString(phoneNumberIdx);    
        NameList.add(name);
        PhoneList.add(phone);

        System.out.println("Name is :"+name +" number is : "+phone);
        System.out.println("Name is :"+name +" number is : "+phone);
        System.out.println("Name is :"+name +" number is : "+phone);



    }


}
}