我正在尝试从设备中读取联系人。我正在获取设备联系人列表,但问题是大多数都是重复的。但在我的默认联系人应用程序中,那些重复项不在那里。现在它正在返回来自设备的1150
个联系人602
周围的重复项(当我使用日志检查结果时),但我的默认联系人App没有显示此重复项。这是我的代码
private void readPhoneContacts(Context context) {
try {
Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
HashMap<String, Contact> mContactListItems = new HashMap<>();
if (cursor != null) {
while (cursor.moveToNext()) {
String key = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
String image_uri = "";
String phoneNumber = "";
String name = "";
String emailId = null;
int image_urlCursorIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI);
if (!cursor.isNull(image_urlCursorIndex)) {
image_uri = cursor.getString(image_urlCursorIndex);
}
int phoneNumberCursorIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
if (!cursor.isNull(phoneNumberCursorIndex)) {
phoneNumber = cursor.getString(phoneNumberCursorIndex);
}
int nameCursorIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
if (!cursor.isNull(nameCursorIndex)) {
name = cursor.getString(nameCursorIndex);
}
emailId = cursor.getString(ContactsContract.CommonDataKinds.Email.TYPE_HOME);
Contact contact = new Contact();
contact.setName(name);
contact.setEmailId(emailId);
contact.setMobileNumber(phoneNumber);
contact.setId(key);
key = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
if (image_uri != null && !image_uri.isEmpty()) {
contact.setHasLocalContactImage(true);
contact.setDeviceContactImageUri(Uri.parse(image_uri));
}
mContactListItems.put(String.valueOf(key), contact);
}
cursor.close();
}
} catch (SQLiteException e) {
} catch (SecurityException e) {
} catch (Exception e) {
}
}
我想得到的是来自设备的所有联系人的Id
,Name
,Phone number
,Email Id
。此外,它应列出所有类型的电话和电子邮件(家庭,办公室等)。有没有办法避免重复并获取这些数据?提前谢谢。