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 "";
}