Lena
Lena

Reputation: 71

Accessing contacts list, searching for a contact, pulling its number and finally dial (android)

My app gets a contact name from the user, and then it is supposed to search in the contacts list if the contact exists. If it does, then pull its number and then automatically dial this number.

I can't seem to understand how to look for the contact name and then pull its number, if it exists.

public boolean seekAction(String s)
    {

        Intent callIntent= new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
        callIntent.setType(Phone.CONTENT_TYPE);
        if (callIntent.resolveActivity(getPackageManager()) != null) 
        {
             startActivityForResult(callIntent, CONTACT_CALL_CODE_REQUEST);

        }

    }



@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {

        if(resultCode == RESULT_OK)
        {
            if(requestCode == CONTACT_CALL_CODE_REQUEST)
            {
                // Get the URI and query the content provider for the phone number
                Uri contactUri = data.getData();
                String[] s = new String[]{Phone.NUMBER};
                Cursor cursor = getContentResolver().query(contactUri, s, null, null, null);

                // If the cursor returned is valid, get the phone number
                if (cursor != null && cursor.moveToFirst()) 
                {
                    int numberIndex = cursor.getColumnIndex(Phone.NUMBER);
                    String number = cursor.getString(numberIndex);
                    // Do something with the phone number
                    Log.i("actionCallResult()", "phone number= "+number);

                    Intent intent = new Intent(Intent.ACTION_DIAL);//calling intent
                    intent.setData(Uri.parse("tel:" + number));
                    if (intent.resolveActivity(getPackageManager()) != null) 
                    {
                        startActivity(intent);
                    }
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

I have this code, but it doesn't help me to look for the contact by its name.

edit:

public boolean actionCallResult(String str)
{
    Log.i("actionCallResult()", "inside actionCallResult");

    Cursor contacts = getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
    boolean contactFound = false; // If the cursor returned is valid, get the phone number

    if (contacts != null && contacts.moveToFirst()) 
    {
        do{

            String contactName =  contacts.getString(contacts.getColumnIndex(Phone.DISPLAY_NAME));
            if (contactName.contains(str)) //contactName.equals(str)
            {
                contactFound = true;
                break;
            }
            else
            {
                if(str.charAt(0)== 'ל')
                {
                    if (contactName.contains(str.substring(1))) 
                    {
                        contactFound = true;
                        break;
                    }
                }
            }

        }while (contacts.moveToNext());
    }

    if(contactFound)
    {
        String number= contacts.getString(contacts.getColumnIndex(Phone.NUMBER));
        Log.i("actionCallResult()", "phone number= "+number);

        Intent intent = new Intent(Intent.ACTION_DIAL);//calling intent
        intent.setData(Uri.parse("tel:" + number));
        if (intent.resolveActivity(getPackageManager()) != null) 
        {
            startActivity(intent);
        }
    }

    contacts.close();
    return contactFound;
}

Upvotes: 1

Views: 147

Answers (1)

Sahar Avr
Sahar Avr

Reputation: 1168

I wrote a simple code that looks for a contact named "Mike Peterson" and toasts a message with the contact's number if found, and a "not found" message otherwise.

This code is raw and by no means should serve any professional implementations. A better approach would be to use custom AsyncTasks and Array Storage.

Please note that this following permission declaration must appear in your manifest:

<uses-permission android:name="android.permission.READ_CONTACTS" />

The following was tested under a simple main activity:

    String str = "Mike Peterson"; // As an example

    Cursor contacts = getApplicationContext().getContentResolver()
            .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

    boolean contactFound = false;

    while (contacts.moveToNext()) {

        String contactName =
                contacts.getString(contacts.getColumnIndex
                        (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

        if (contactName.equals(str)) {
            contactFound = true;
            break;
        }
    }

    Toast.makeText(getApplicationContext(), contactFound ?
            contacts.getString(contacts.getColumnIndex
                    (ContactsContract.CommonDataKinds.Phone.NUMBER)) : "Contact not found!"
            , Toast.LENGTH_LONG).show();

    contacts.close();

Upvotes: 5

Related Questions