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.