简体   繁体   English

给出电子邮件地址查询Android联系人的快速有效方法

[英]Fast & Efficient Way To Query Android Contacts Given An Email Address

I am looking for a fast & efficient way to query the Android contacts given an email address (basically, given an email address I want to find the matching contact if it exists). 我正在寻找一种快速有效的方式来查询给定电子邮件地址的Android联系人(基本上,给定一个电子邮件地址,我想找到匹配的联系人,如果它存在)。 Currently I have a double While loop and it's probably the most inefficient way to accomplish this. 目前我有一个双while循环,它可能是实现这一目标的最低效的方法。

I imagine that I have to do a custom SQLite join query, but I am unfamiliar with how to do this. 我想我必须做一个自定义的SQLite连接查询,但我不熟悉如何做到这一点。 Any help or pointers from other users would be appreciated! 任何来自其他用户的帮助或指示将不胜感激!

There is a way to get the needed details without querying the second cursor. 有一种方法可以在不查询第二个游标的情况下获取所需的详细信息。 Since the ContentProvider knows how to join the tables, using the projection parameter you can request columns from join tables as well. 由于ContentProvider知道如何连接表,因此您可以使用projection参数从连接表中请求列。

So the code might look like this: 所以代码可能如下所示:

Cursor cursor = null;
try {
    //Filter by email address first.
    final String[] projection = new String[] { 
                ContactsContract.CommonDataKinds.Email.CONTACT_ID, 
                ContactsContract.CommonDataKinds.Email.DATA,
                ContactsContract.CommonDataKinds.Email.DISPLAY_NAME_PRIMARY,
                ContactsContract.CommonDataKinds.Email.IS_PRIMARY };

    final String selection = ContactsContract.CommonDataKinds.Email.DATA + "=? and "
                + ContactsContract.CommonDataKinds.Email.IS_PRIMARY+"=1";

    final String[] selectionArgs = new String[] { email };

    cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, selection, selectionArgs, null);

    if (cursor.moveToFirst()) {
        long contactId = cursor.getLong(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID));
        String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DISPLAY_NAME_PRIMARY));

        // Do something with contactId and displayName
    }
} finally {
    if (cursor!=null) cursor.close();
}

So DISPLAY_NAME_PRIMARY is a column that is joined automatically by the ContentProvider. 因此,DISPLAY_NAME_PRIMARY是由ContentProvider自动连接的列。 Not the use of IS_PRIMARY to make sure we get a primary contact raw data and not all data. 不是使用IS_PRIMARY来确保我们获得主要联系人原始数据而不是所有数据。

After a little bit more research, there is no contract that handles this type of request and you can't do any custom SQL query on any DB that you don't own. 经过一些研究后,没有处理此类请求的契约,也无法对您不拥有的任何数据库执行任何自定义SQL查询。 I have come up with the best solution that I could. 我想出了最好的解决方案。 Please comment on this and let me know if there is a way to make this perform any better/faster/efficient. 请对此发表评论并告诉我是否有办法让这项表现更好/更快/更有效率。

String incomingEmail = [EMAIL_WE_ARE_SEARCHING_FOR]...
long contactID = -1;
//Filter by email address first.
final String[] emailProjection = new String[]{ContactsContract.CommonDataKinds.Email.CONTACT_ID, 
    ContactsContract.CommonDataKinds.Email.DATA};
final String emailSelection = ContactsContract.CommonDataKinds.Email.DATA + "=?";
final String[] emailSelectionArgs = new String[]{incomingEmail};
final String emailSortOrder = null;
Cursor emailCursor = context.getContentResolver().query(
        ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
        emailProjection,
        emailSelection, 
        emailSelectionArgs, 
        emailSortOrder);
if(emailCursor.moveToFirst()){
    contactID = emailCursor.getLong(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID)); 
    //Query the specific contact if found.
    final String[] projection = new String[]{ContactsContract.Contacts._ID, 
        ContactsContract.Contacts.DISPLAY_NAME, 
        ContactsContract.Contacts.PHOTO_ID, 
        ContactsContract.Contacts.LOOKUP_KEY};
    final String selection = ContactsContract.Contacts._ID + "=?";
    final String[] selectionArgs = new String[]{String.valueOf(contactID)};
    final String sortOrder = null;
    Cursor contactCursor = context.getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI,
            projection, 
            selection, 
            selectionArgs, 
            sortOrder);             
    if(contactCursor.moveToFirst()){
        contactName = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String photoIDTmp = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID)); 
        if(photoIDTmp != null){
            photoID = Long.parseLong(photoIDTmp);
        }
        lookupKey = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
    }
    contactCursor.close();      
}else{
    emailCursor.close();
    return null;                
}
emailCursor.close();
//Return Contact ID, Contact Name, Contact Photo, Etc.

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM