public void getUserInfo(){
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while(cursor.moveToNext()){
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.d(TAG , "Name is : "+name);
int isHas = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if(isHas>0){
Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " + id,null,null);
while(c.moveToNext()){
String number = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.d(TAG , "Number is : "+number);
}
c.close();
}
}
cursor.close();
}
有时候也会遇到根据号码找联系人的情况,及时提示用户。
private String getNameFromPhone(String number) {
String name = null;
String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor cursor = this.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, // Which columns to return.
ContactsContract.CommonDataKinds.Phone.NUMBER + " = '"
+ number + "'", // WHERE clause.
null, // WHERE clause value substitution
null); // Sort order.
if (cursor == null) {
Log.d(TAG, "getPeople null");
return null;
}
Log.d(TAG, "getPeople cursor.getCount() = " + cursor.getCount());
for (int i = 0; i < cursor.getCount(); i++) {
cursor.moveToPosition(i);
int nameFieldColumnIndex = cursor
.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
name = cursor.getString(nameFieldColumnIndex);
Log.i(TAG, "" + name + " .... " + nameFieldColumnIndex);
}
cursor.close();
return name;
}