i want to retrieve the phone number from a contact name. so i have this code
public String getPhoneNumber(String name) {
String phonenumber = "";
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME LIKE '%" + name + "%'", null, null);
if (cursor.moveToFirst()) {
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
//
// Get all phone numbers.
//
Cursor phones = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID
+ " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones
.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
switch (type) {
case Phone.TYPE_HOME:
//phonenumber = number;
// do something with the Home number here...
break;
case Phone.TYPE_MOBILE:
phonenumber = number;
// do something with the Mobile number here...
break;
case Phone.TYPE_WORK:
//phonenumber = number;
// do something with the Work number here...
break;
}
}
phones.close();
//
// Get all email addresses.
//
}
cursor.close();
return phonenumber;
}
But, if the contact name is not match exactly with name in db, it will return nothing. So whether there have any way to retrieve the phone number even the input name is not very likely to?
for example, there have an contact : Prototype Alex and my input name is: prototype or alex.