Search code examples
androidcontactsandroid-contactscontactscontract

Retrieve contact on Android with name matching given string and dial that contact's phone number


I've been having difficulty implementing the retrieval of the phone number of a contact whose name matches a given string and then dialing that number. I know this is possible because many existing apps include this feature. How can it be implemented?


Solution

  • this code getting all contacts in phone

    Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        if (phones != 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();
    
        }
    

    Compare the variable "name" or "phoneNumber" with your String

    and you have add permission "android.permission.READ_CONTACTS"