现在的位置: 首页 > 综合 > 正文

由电话号码得到姓名(转)

2012年12月09日 ⁄ 综合 ⁄ 共 1215字 ⁄ 字号 评论关闭

由电话号码得到姓名

1已有 2 次阅读  21小时前   标签:  电话号码  姓名 

2.0之前:
public String getName(Context ctx,String number){
String[] projection = new String[] {
Contacts.Phones.DISPLAY_NAME,
Contacts.Phones.NUMBER };
 
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number));
// query time
Cursor c = ctx.getContentResolver().query(contactUri, projection, null,
null, null);
 
// if the query returns 1 or more results
// return the first result
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(Contacts.Phones.DISPLAY_NAME));
      Log.d("DEBUG","XYZ_"+name + " is calling");
return name;
}
return "";
}

2.0以及之后:
public String getName(Context ctx,String number){
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

// Query the filter URI
String[] projection1 = new String[]{ PhoneLookup.DISPLAY_NAME};
Cursor cursor1 = ctx.getContentResolver().query(uri, projection1, null,null,null);
if (cursor1.moveToFirst()) {
String name = cursor1.getString(cursor1
.getColumnIndex(PhoneLookup.DISPLAY_NAME));
 int phoneNameIndex = cursor1.getColumnIndex(PhoneLookup.DISPLAY_NAME);
      String phoneNameStr = cursor1.getString(phoneNameIndex);
      Log.d("DEBUG","XYZ_"+phoneNameStr + " is calling");
return name;
}
return "";
}

抱歉!评论已关闭.