Skip to content Skip to sidebar Skip to footer

How To Get The Phone Number From Selected Contact?

public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data); switch (reqCode) { case (1) : if (

Solution 1:

finalUriPerson= Uri.withAppendedPath(
            ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,
            Uri.encode(number));

    final String[] projection = newString[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };

    finalCursorcursor= context.getContentResolver().query(Person, projection,
            null, null, null);

    if (cursor.moveToFirst()) {

        finalStringnumber= cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        return number;
    }
    cursor.close();

Solution 2:

Try the following,

publicvoidgetContactDetails(String conatctname)
    {
        try
        {
            ContentResolver cr =getContentResolver();
            Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
            while (cursor.moveToNext()) 
            {

                FirstName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                if(FirstName!=null)
                {
                    try
                    {
                    String[] splitval=FirstName.split(" ");
                    if(splitval.length>=1)
                    {
                        FirstName=splitval[0];
                        if(FirstName.equals(conatctname))
                        {
                            if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
                            {
                            Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",newString[]{id}, null);
                            while (pCur.moveToNext()) 
                            {
                            PhoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            PhoneNumberArray.add(PhoneNumber);
                            }
                            pCur.close();
                        }

                    }
                    }
                    catch(Exception error)
                    {
                        Log.d("SplitError", error.getMessage());
                    }                   

            }
            cursor.close();
        }
        catch (NumberFormatException e)
        {
            e.printStackTrace();
        }
    }

Solution 3:

You can get selected name and number from contact of phone

Uriuri= data.getData();
        String[] projection    = newString[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER};

        Cursorpeople= getActivity().getContentResolver().query(uri, projection, null, null, null);

        intindexName= people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        intindexNumber= people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

        people.moveToFirst();
        do {
            Stringname= people.getString(indexName);
            Stringnumber= people.getString(indexNumber);
            System.out.println(name+number);

        } while (people.moveToNext());

Post a Comment for "How To Get The Phone Number From Selected Contact?"