问题
I know that with aggregation contact _ids are not static, but may change over time (see: Google Group Discussion).
So, in my app I am storing the content lookup key and the current id of a selected contact.
What I am trying to do is this: when a message comes in from a particular email address, query the phone contacts and see if that from email address is associated with any of them, and then compare that contact with my stored contact, first doing a fresh query using my stored contact _id and lookup key to get the current contact _id.
Here is how I am attempting to find the contact _id using the email address:
String[] projection = {
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts._ID
}
uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_LOOKUP_URI, Uri.encode(data)); //data = email address
cur = resolver.query(uri, projection, null, null, null);
if (cur != null){
while (cur.moveToNext()){
msg.set_contact(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)), cur.getLong(cur.getColumnIndex(ContactsContract.Contacts._ID)));
}
}
The above code works in that it finds that the email address is associated with a contact on the phone.
The problem: I think I am getting the _id of the raw email address entry, not the _id of the overall contact. Because when I refresh my stored contact _id and compare it with what is returned from the above they don't match, even though I have confirmed that the email is associated with my stored contact.
Here is how I am looking up my stored contact:
String[] projection = {
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts._ID
}
Uri uri = ContactsContract.Contacts.getLookupUri(contact_id, contact_lookup);
Cursor cur = resolver.query(cLookupUri, projection, null, null, null);
if(cur != null){
while (cur.moveToNext()){
msg.set_contact(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)), cur.getLong(cur.getColumnIndex(ContactsContract.Contacts._ID)));
}
}
What am I doing wrong? Basically I just want to get the contact_id of any contact associated with the incoming email address...
Any help is much appreciated!
回答1:
Try this:
Query ContactsContract.Contacts.Entity. Select on MIMETYPE = "vnd.android.cursor.dir/email_v2", and DATA1 = incoming email address.
This will return a table that contains one row for every raw contact whose e-mail address matches the incoming address, but the row will also contain the raw contact's aggregate contact id. From that, you can query ContactsContract.Contacts to find the LOOKUP_KEY.
回答2:
public Long getContactIdByEmail(String email) {
Uri uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_FILTER_URI, Uri.encode(email));
String name = "?";
long contactId =0;
ContentResolver contentResolver = v.getContext().getContentResolver();
Cursor contactLookup = contentResolver.query(uri, new String[] {ContactsContract.Data.CONTACT_ID, ContactsContract.Data.DISPLAY_NAME }, null, null, null);
try {
if (contactLookup != null && contactLookup.getCount() > 0) {
contactLookup.moveToNext();
name = contactLookup.getString(contactLookup.getColumnIndex( ContactsContract.Data.DISPLAY_NAME ));
contactId = contactLookup.getLong(contactLookup.getColumnIndex(ContactsContract.Data.CONTACT_ID));
}
} finally {
if (contactLookup != null) {
contactLookup.close();
}
}
return contactId;
}
来源:https://stackoverflow.com/questions/13202403/attempting-to-get-contact-id-using-email-address