我想从联系人姓名中检索电话号码。所以我有这个代码
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;
}
但是,如果联系人姓名与数据库中的姓名不完全匹配,它将不返回任何内容。那么有没有什么办法可以找回连输入名字都不太可能的电话号码呢?
例如,有一个联系人:Prototype Alex,我输入的名称是:prototype 或 alex。