修复了你的代码,确保处理其余的我做了一个例子这是固定的代码:
String name = "contact_name_to_search_for"
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
null,"DISPLAY_NAME" + " LIKE '%"+name+"%'", null,
ContactsContract.CommonDataKinds.Phone.LAST_TIME_CONTACTED + " DESC");
例如,这是我制作的完整处理代码
String name = "contact_name_to_search_for"
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
null,"DISPLAY_NAME" + " LIKE '%"+name+"%'", null,
ContactsContract.CommonDataKinds.Phone.LAST_TIME_CONTACTED + " DESC");
if (cursor.moveToFirst()) {
String contactId =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)).toLowerCase();
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId.toLowerCase(), null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
switch (type) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
Toast.makeText(getApplicationContext(), number+" Home number",Toast.LENGTH_SHORT ).show();
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
Toast.makeText(getApplicationContext(), number+" Mobile number",Toast.LENGTH_SHORT ).show();
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
Toast.makeText(getApplicationContext(), number+" Work number",Toast.LENGTH_SHORT ).show();
break;
}
}
phones.close();
}
cursor.close();