Android get contact number using name

前端 未结 3 1278
既然无缘
既然无缘 2021-01-16 16:41

I am trying to make an application on android that takes the contact name as a string input and returns his phone number if that contact exists in the phone book...

3条回答
  •  星月不相逢
    2021-01-16 17:23

    Try this way..

    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
                String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.NUMBER};
    
                Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    
                int idxName = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                int idxNumber = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    
                if(cursor.moveToFirst()) {
                    do {
                        contactname   = cursor.getString(idxName);
                        contactNumber = cursor.getString(idxNumber);
    
                        if (contactname.equals("YOUR CONTACT NAME")){
                            Log.d(LOG_TAG,"Contact Name -> "+ contactname +" Contact Number -> "+ contactNumber);
                        }
                    } while (cursor.moveToNext());
                }
                cursor.close();
    

提交回复
热议问题