问题
I need to read the contacts from device. The fields I required are ID
, Display Name
, Phone Number
(All types) Email id
(All types) and 'photo'. For this right now I am doing like this.
1 : First I am reading all the id
s from ContactsContract.Contacts.CONTENT_URI;
as shown below
Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
// Querying the table ContactsContract.Contacts to retrieve all the contacts
String[] projection = {ID};
Cursor contactsCursor = mContentResolver.query(contactsUri, projection, null, null,
"upper(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
2 : Then iterating through this cursor to read requred fields of all contacts.
if (contactsCursor.moveToFirst()) {
do {
long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex(ID));
Uri dataUri = ContactsContract.Data.CONTENT_URI;
// Querying the table ContactsContract.Data to retrieve individual items like
// home phone, mobile phone, work email etc corresponding to each contact
String[] columns = {CONTACT_ID, PHOTO_URI, DISPLAY_NAME, MIME_TYPE, DATA_1, DATA_2, DATA_4};
String selection = ContactsContract.Data.CONTACT_ID + "=" + contactId;
Cursor dataCursor = mContentResolver.query(dataUri, columns,
selection,
null, null);
if (dataCursor.moveToFirst()) {
// Getting Display Name
displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
do {
// Getting Phone numbers
String mimeType = dataCursor.getString(dataCursor.getColumnIndex(MIME_TYPE));
switch (mimeType) {
case ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE:
String phoneNumber = dataCursor.getString(dataCursor.getColumnIndex(DATA_1));
switch (dataCursor.getInt(dataCursor.getColumnIndex(DATA_2))) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
homePhone = phoneNumber;
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
mobilePhone = phoneNumber;
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
workPhone = phoneNumber;
break;
default:
otherPhone = phoneNumber;
}
break;
// Getting emails
case ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE:
switch (dataCursor.getInt(dataCursor.getColumnIndex(DATA_2))) {
case ContactsContract.CommonDataKinds.Email.TYPE_HOME:
homeEmail = dataCursor.getString(dataCursor.getColumnIndex(DATA_1));
break;
case ContactsContract.CommonDataKinds.Email.TYPE_WORK:
workEmail = dataCursor.getString(dataCursor.getColumnIndex(DATA_1));
break;
default:
otherEmail = dataCursor.getString(dataCursor.getColumnIndex(DATA_1));
}
break;
// getting photo Uri
case ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE:
if (dataCursor.getString(dataCursor.getColumnIndex(PHOTO_URI)) != null) {
photoUri = Uri.parse(dataCursor.getString(dataCursor.getColumnIndex(PHOTO_URI)));
}
break;
}
} while (dataCursor.moveToNext());
} while (contactsCursor.moveToNext());
The queries work fine but the problem is it is taking too much time to iterate and get details of each contact. First query is returning quickly , but the whole delay is now in the second part, that is iterating through each row of the first query and querying for each fields. Can this be done in a single join query so that I can optimize the performance?
回答1:
Yes, you can do that in a single query - all the data about contacts is actually stored in a single table Data
, which contains everything you need on a contact including CONTACT_ID and DISPLAY_NAME which are also hosted on the Contacts
table.
So you need to basically query everything from the Data table in a single query, and sort it out into contacts using a HashMap from CONTACT_ID to some contact object (or if you prefer a list of details).
(Note: make sure you import the following classes from ContactsContract
package only)
Map<Long, List<String>> contacts = new HashMap<Long, List<String>>();
String[] projection = {Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3, Data.PHOTO_ID};
String selection = Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "', '" + Email.CONTENT_ITEM_TYPE + "')";
Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);
while (cur != null && cur.moveToNext()) {
long id = cur.getLong(0);
String name = cur.getString(1);
String mime = cur.getString(2); // email / phone / company
String data = cur.getString(3); // the actual info, e.g. +1-212-555-1234
int type = cur.getInt(4); // a numeric value representing type: e.g. home / office / personal
String label = cur.getString(5); // a custom label in case type is "TYPE_CUSTOM"
long photoId = cur.getLong(6);
String kind = "unknown";
String labelStr = "";
switch (mime) {
case Phone.CONTENT_ITEM_TYPE:
kind = "phone";
labelStr = Phone.getTypeLabel(getResources(), type, label);
break;
case Email.CONTENT_ITEM_TYPE:
kind = "email";
labelStr = Email.getTypeLabel(getResources(), type, label);
break;
}
Log.d(TAG, "got " + id + ", " + name + ", " + kind + " - " + data + " (" + labelStr + ")");
// add info to existing list if this contact-id was already found, or create a new list in case it's new
List<String> infos;
if (contacts.containsKey(id)) {
infos = contacts.get(id);
} else {
infos = new ArrayList<String>();
infos.add("name = " + name);
infos.add("photo = " + photoId);
contacts.put(id, infos);
}
infos.add(kind + " = " + data + " (" + labelStr + ")");
}
来源:https://stackoverflow.com/questions/35959508/how-to-retrieve-all-contacts-details-from-content-provider-with-single-query