I am trying to get all contacts that have a phone number, and record their full name and phone number, (and in the future, their contact photo), but I am stuck. here is my code:
String contacts = "";
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone == "1") {
contacts += cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)) + ":" + "how to get number?" + "|";
}
}
cursor.close();
String hasPhone should contain "1" if the contact has a phone number, then add that name and the persons phone number to the "contact" string. Even though hasPhone does contain "1", (checked from logcat) no code in the condition statement runs. Also, how do you get the phone number, there is nothing in ContactsContract.Contacts for number.
Try this:
if (Integer.parseInt(hasPhone) > 0) {
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +"="+ contactId, null, null);
phones.moveToNext(); //if you are interested in all contact phones do a while()
String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
phones.close();
contacts += cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)) + ":" + phoneNumber + "|";
}