通过电话号码获取姓名 (+86或者飞信)

/**
* 通过电话号码获取姓名 (+86或者飞信)
*/
/*	public String getContactName(String phoneNum) {
String contactName = "";

// 处理电话号码格式问题
if (phoneNum.length() > 11) {

ContentResolver cr = context.getContentResolver();
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?",
new String[] { phoneNum }, null);
if (pCur == null) {
pCur.close();
return contactName;
}
if (pCur.moveToFirst()) {
contactName = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
pCur.close();
}
if (contactName.equals("")) {
phoneNum = phoneNum.substring(phoneNum.length() - 11);


}
} 

ContentResolver cr = context.getContentResolver();
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?",
new String[] { phoneNum }, null);
if (pCur == null) {
pCur.close();
return contactName;
}
if (pCur.moveToFirst()) {
contactName = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
pCur.close();
}
return contactName;
}
*/

public String getContactName(String phoneNum) {
String contactName = "";
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.NUMBER + " like ? ",
new String[] { "%"+phoneNum+"%" }, null);
if (cursor == null) {
cursor.close();
return contactName;
}
while(cursor.moveToNext()) {
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
}
cursor.close();
return contactName;
}

你可能感兴趣的:(通过电话号码获取姓名 (+86或者飞信))