我正在寻找一种快速有效的方法来查询给定电子邮件地址的 Android 联系人(基本上,给定一个电子邮件地址,我想找到匹配的联系人(如果存在))。目前我有一个双 While 循环,这可能是实现这一目标的最低效的方法。
我想我必须做一个自定义 SQLite 连接查询,但我不熟悉如何做到这一点。其他用户的任何帮助或指示将不胜感激!
有一种方法可以在不查询第二个游标的情况下获取所需的详细信息。由于 ContentProvider 知道如何连接表,因此您也可以使用投影参数从连接表中请求列。
所以代码可能如下所示:
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();
}
因此 DISPLAY_NAME_PRIMARY 是由 ContentProvider 自动连接的列。不使用 IS_PRIMARY 来确保我们获得主要联系人原始数据而不是所有数据。
经过更多研究,没有处理此类请求的合同,并且您无法在您不拥有的任何数据库上执行任何自定义 SQL 查询。我已经想出了我能做到的最好的解决方案。请对此发表评论,并让我知道是否有办法使其表现更好/更快/更有效率。
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.
谢谢!