How to get phone number from person name which are in my contact list in android

牧云@^-^@ 提交于 2019-12-04 12:29:12

Here is it:

static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
    ContactsContract.Contacts._ID,
    ContactsContract.Contacts.DISPLAY_NAME,
    ContactsContract.Contacts.STARRED,
    ContactsContract.Contacts.TIMES_CONTACTED,
    ContactsContract.Contacts.CONTACT_PRESENCE,
    ContactsContract.Contacts.PHOTO_ID,
    ContactsContract.Contacts.LOOKUP_KEY,
    ContactsContract.Contacts.HAS_PHONE_NUMBER,
};

String name_to_search = "sidharth";

String select = "(" + ContactsContract.Contacts.DISPLAY_NAME + " == \"" +name_to_search+ "\" )";
Cursor c = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
context.startManagingCursor(c);

if (c.moveToNext())
{
    String id = c.getString(0);
    ArrayList<String> phones = new ArrayList<String>();

    Cursor pCur = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
    while (pCur.moveToNext())
    {
        phones.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
        Log.i("", name_to_search+ " has the following phone number "+ pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
    } 
    pCur.close();   
}
Chris A

Use the moveToFirst() function of the cursor to focus on the match and then call getString

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!