3

What i did until now is:

when clicking on some button:

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, CONTACT_PICKER_RESULT); 

then:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {  
        switch (requestCode) {  
        case CONTACT_PICKER_RESULT:  
            if (data != null) {
                Uri uri = data.getData();
                if (uri != null) {
                    Cursor cursor = null;
                    Cursor cursorStructuredName = null;
                    try{    
                        cursor = getContentResolver().query(uri, null, null, null, null);      
                        boolean hasNext = cursor.moveToNext();
                        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                        String hasPhoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                        String phoneNumber = null;      

                        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);

                        if (phones.moveToFirst()) {//does not get inside the if
                            phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        }
                        phones.close();


                        // projection
                        String[] projection = new String[] {ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, 
                                ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
                                ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,
                                ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
                                ContactsContract.CommonDataKinds.StructuredName.PREFIX,
                                ContactsContract.CommonDataKinds.StructuredName.SUFFIX};


                        String where = ContactsContract.Data.RAW_CONTACT_ID + " =?"; 
                        String[] whereParameters = new String[]{contactId};

                        //Request
                        cursorStructuredName = getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, where, whereParameters, null);

                        String displayName = null;
                        String givenName = null;
                        String middleName = null;                           
                        String familyName = null;
                        String prefix = null;
                        String suffix = null;
                        hasNext = cursorStructuredName.moveToFirst();
                        if (cursorStructuredName != null && hasNext) {//does not get here because hasNext equals false
                            displayName = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME)); 
                            givenName = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)); 
                            middleName = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME)); 
                            familyName = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)); 
                            prefix = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX)); 
                            suffix = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX)); 
                        }

                    } finally {
                        if (cursor != null) {
                            cursor.close();
                        }
                        if (cursorStructuredName != null) {
                            cursorStructuredName.close();
                        }
                    }
                }
            }
            break;
        }
    }
}

I can't get the family name, given name etc. I can't get the selected phone number.

As i said in the title, all i want is that when the user clicks on some button then the contact list will be opened and when he choose a specific contact then a list of this contact phone numbers appear and then when he choose a number the method onAtivityResult will be invoked and there i will "parse" the phone number, family name, given name, etc.

  • I suggest you split your code up in smaller methods. One big method doing everything is bad practise. – danihodovic Jan 12 '14 at 13:29
  • are you getting an exception? post the logcat. need more info – Greg Ennis Jan 12 '14 at 13:36
  • there is no exception, the problem is that i am doing something worng(maybe i dont query correctly...). phones.moveToFirst() return false, and cursorStructuredName.moveToFirst() return false. – Android Fan Jan 12 '14 at 13:48
  • here is a link to a solved question which let the user choose a contact and a phone number from that contact: http://stackoverflow.com/questions/4993063/how-to-call-android-contacts-list-and-select-one-phone-number-from-its-details-s and here is a link to a solved question on how to get the family name and the given name etc: http://stackoverflow.com/questions/4301064/how-to-get-the-first-name-and-last-name-from-android-contacts but i need both and i didn't succeed – Android Fan Jan 12 '14 at 13:51
  • after more research i tried the next code: http://paste.ofcode.org/33dtpsnzNQ5Cd5UCRBYKKUz but any of the id's are not equals to the contactId from the above code. I check the display name in both from curser and cursorStructuredName and they where the same so how can you found a contact family name, given name... by contact id? – Android Fan Jan 13 '14 at 08:57

2 Answers2

1

Well, after more research i have solved my problem.

The next code lets a user press a button which then open the Contact App letting the user choose a contact(showing only contact that have at least one phone number). After choosing a contact a list of all its numbers is open letting the user the option to choose one(not all devices have this option, for example, in some devices the same contact is appearing more then once, one for each of his phone numbers). After that the method onActivityResult() is invoked and then(if everything is ok) i am extractig the chosen phone number, the given name and the family name(of the chosen contact):

@Override
public void onClick(View v) {
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
    startActivityForResult(intent, CONTACT_PICKER_RESULT);  
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {  
        switch (requestCode) {  
        case CONTACT_PICKER_RESULT:  
            handleContactSelection(data);
            break;
        }
    }
}

private void handleContactSelection(Intent data) {
    if (data != null) {
        Uri uri = data.getData();
        if (uri != null) {
            Cursor cursor = null;
            Cursor nameCursor = null;
            try {
                cursor = getContentResolver().query(uri, new String[]{  
                        ContactsContract.CommonDataKinds.Phone.NUMBER,
                        CommonDataKinds.Phone.CONTACT_ID} ,                                
                        null, null, null);

                String phoneNumber = null;                  
                String contactId = null;
                if (cursor != null && cursor.moveToFirst()) {
                    contactId = cursor.getString(cursor.getColumnIndex(CommonDataKinds.Phone.CONTACT_ID));
                    phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                               
                }

                String givenName = null;///first name.
                String familyName = null;//last name.

                String projection[] = new String[]{ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
                        ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME};
                String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + 
                        ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?";
                String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, contactId};

                nameCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, 
                        projection, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);

                if(nameCursor != null && nameCursor.moveToNext()) {
                    givenName = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
                    familyName = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
                }

                doSomething(phoneNumber,givenName,familyName);

            } finally {
                if (cursor != null) {
                    cursor.close();
                }

                if(nameCursor != null){
                    nameCursor.close();
                }
            }
        }
    }
}   
0

I believe this one will solve some of your issues . This basically takes in a phoneno and queries for its contact No. I use this to upload the data into a server. As for the phone number I get it from the received calls listener.


String[] projection = new String[]{
                                ContactsContract.PhoneLookup.DISPLAY_NAME};
                        Uri contacturi= Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneno));
                    ContentResolver cr = getContentResolver();      
Cursor crc= cr.query(contacturi, projection, null, null, null);
                    if(crc.moveToNext()){
                            name=crc.getString(crc.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));

                        }
                        else{
                            name ="Unknown";
                        }


You need to add all your required fields on the crc cursor. ie the home, email, family. Hope this gives you some insight :-/

Kevin Kaburu
  • 504
  • 4
  • 19
  • 1
    This is not good, this will return the display name which can be, for example, "Dr. Mike Junior Laufer Suffix ..." i only requested the first name ,last name and phone number. Thanks but Anyway i already found a solution and even posted it here when i answered my own question. – Android Fan Jan 16 '14 at 12:49
  • Ok, I was just giving this as an insight..... You were to tailor it to suite you need :-) . Yeah you answered your own Question Lol – Kevin Kaburu Jan 17 '14 at 08:36