转载请注明原文出处:奔跑的蜗牛(袁方的技术博客)http://blog.youkuaiyun.com/yf210yf
读到短信常有些飞信号码或+86等号码,在联系人中没有存储,下面稍微改了下已有的程序,让已有的联系人号码不因 飞信号码 或 +86号码不能识别
/**
* 通过电话号码获取姓名
*/
public String getContactNameFromPhoneNum(Context context, 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.moveToFirst())
{
contactName = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
pCur.close();
}
if (contactName.equals(""))
{
phoneNum=phoneNum.substring(phoneNum.length()-11);
System.out.println(phoneNum);
}
}
ContentResolver cr = context.getContentResolver();
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?",
new String[]
{ phoneNum }, null);
if (pCur.moveToFirst())
{
contactName = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
pCur.close();
}
return contactName;
}