tôi cần để có được tất cả danh bạ điện thoại và địa chỉ email của họ và uri ảnh:nhà cung cấp Liên hệ Android chỉ nhận được danh bạ điện thoại với tất cả các email
Đây là những gì đang làm:
private void getContacts() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(Contacts.CONTENT_URI, null, null, null, Contacts.DISPLAY_NAME);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
// if
// (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))
// > 0) {
Contact contact = new Contact();
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Uri uri = getContactPhotoUri(Long.parseLong(id));
// set photoUri
contact.setContactPhotoUri(uri);
// set name
contact.setContactName(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
// set phone munber
contact.setContactNumber(pCur.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
contacts.add(contact);
}
pCur.close();
// get email and type
Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { id }, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
// set email
contact.setContactEmail(emailCur.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
contacts.add(contact);
}
emailCur.close();
}
}
cur.close();
contactAdapter = new ContactAdapter(this, R.id.contactList, contacts);
// }
}
public Uri getContactPhotoUri(long contactId) {
Uri photoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
photoUri = Uri.withAppendedPath(photoUri, Contacts.Photo.CONTENT_DIRECTORY);
return photoUri;
}
Vấn đề của tôi đang nhận được tất cả địa chỉ liên hệ bao gồm cả địa chỉ liên hệ gmail, tôi không muốn các liên hệ gmail được bao gồm. Và thời gian thực hiện cũng rất chậm. Làm thế nào để tôi tối ưu hóa điều này, tôi biết thời gian của nó coz tôi đang sử dụng nhiều con trỏ .. nhưng không biết làm thế nào để thực hiện một cusror duy nhất có thể cho tôi tên email ảnh số uri ... Cảm ơn!
CẬP NHẬT CUỐI CÙNG:
private void getContacts() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(Data.CONTENT_URI, new String[] { Data.CONTACT_ID, Data.MIMETYPE, Email.ADDRESS,
Contacts.DISPLAY_NAME, Phone.NUMBER }, null, null, Contacts.DISPLAY_NAME);
Contact contact;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(Data.CONTACT_ID));
String mimeType = cur.getString(cur.getColumnIndex(Data.MIMETYPE));
if (allContacts.containsKey(id)) {
// update contact
contact = allContacts.get(id);
} else {
contact = new Contact();
allContacts.put(id, contact);
// set photoUri
contact.setContactPhotoUri(getContactPhotoUri(Long.parseLong(id)));
}
if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE))
// set name
contact.setContactName(cur.getString(cur.getColumnIndex(Contacts.DISPLAY_NAME)));
if (mimeType.equals(Phone.CONTENT_ITEM_TYPE))
// set phone munber
contact.setContactNumber(cur.getString(cur.getColumnIndex(Phone.NUMBER)));
if (mimeType.equals(Email.CONTENT_ITEM_TYPE))
// set email
contact.setContactEmail(cur.getString(cur.getColumnIndex(Email.ADDRESS)));
}
}
cur.close();
// get contacts from hashmap
contacts.clear();
contacts.addAll(allContacts.values());
// remove null contacts
for (Contact _contact : contacts) {
if (_contact.getContactName() == null && _contact.getContactNumber() == null
&& _contact.getContactEmail() == null) {
contacts.remove(_contact);
break;
}
}
contactAdapter = new ContactAdapter(this, R.id.contactList, contacts);
contactAdapter.notifyDataSetChanged();
}
public Uri getContactPhotoUri(long contactId) {
Uri photoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
photoUri = Uri.withAppendedPath(photoUri, Contacts.Photo.CONTENT_DIRECTORY);
return photoUri;
}
thế nào về nhận tên hiển thị bằng cách truy vấn trên .. – sukarno
quên lọc theo tài khoản gmail .. cần phải nhận tất cả các số liên lạc email hiển thị tên và uri ảnh trong một truy vấn tùy chỉnh .. để cải thiện hiệu suất – sukarno
Trong liên kết đầu tiên tôi cung cấp, bạn có thể thấy rằng bảng Dữ liệu cũng được hợp nhất với ContactContarct .Contacts -> để bạn có thể yêu cầu Contacts.DISPLAY_NAME. Hãy xem xét bảng Dữ liệu chứa nhiều bản ghi để bạn cần đảm bảo rằng bạn đang tìm kiếm bản ghi "đúng". – Raanan