我想通过一个电话号码获取所有联系人,我也想要每个联系人的所有电话号码和所有电子邮件。
当前代码:
// To get All Contacts having atleast one phone number. Uri uri = ContactsContract.Contacts.CONTENT_URI; String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " > ?"; String[] selectionArgs = new String[] {"0"}; Cursor cu = applicationContext.getContentResolver().query(uri,null,selection,selectionArgs,null); // For getting All Phone Numbers and Emails further queries : while(cu.moveToNext()){ String id = cu.getString(cu.getColumnIndex(ContactsContract.Contacts._ID)); // To get Phone Numbers of Contact Cursor pCur = context.getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",new String[]{id},null); // To get Email ids of Contact Cursor emailCur = context.getContentResolver().query( ContactsContract.CommonDataKinds.Email.CONTENT_URI,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",null); // Iterate through these cursors to get Phone numbers and Emails }
如果我的设备中有超过1000个联系人,则需要花费太多时间。如何在单个查询中获取所有数据,而不是为每个联系人增加两个查询?
还是有其他的优化方法?
先谢谢你。
ICS:当您从Data.CONTENT_URI查询时,您已将所有相关联的联系人的行已加入 – 即这将工作:
ContentResolver resolver = getContentResolver(); Cursor c = resolver.query( Data.CONTENT_URI,Data.HAS_PHONE_NUMBER + "!=0 AND (" + Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?)",new String[]{Email.CONTENT_ITEM_TYPE,Phone.CONTENT_ITEM_TYPE},Data.CONTACT_ID); while (c.moveToNext()) { long id = c.getLong(c.getColumnIndex(Data.CONTACT_ID)); String name = c.getString(c.getColumnIndex(Data.DISPLAY_NAME)); String data1 = c.getString(c.getColumnIndex(Data.DATA1)); System.out.println(id + ",name=" + name + ",data1=" + data1); }
如果您要定位2.3,则需要考虑到在查询数据时使用的连接不能使用HAS_PHONE_NUMBER。
乐趣。
例如,这可以通过跳过您的联系人必须具有电话号码的要求来解决,而不是解决“至少有一个电话号码或电子邮件地址的任何联系人”:
Cursor c = resolver.query( Data.CONTENT_URI,Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?",Data.CONTACT_ID);
如果这不是一个选项,你总是可以去一个可怕的黑客子选择:
Cursor c = resolver.query( Data.CONTENT_URI,"(" + Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?) AND " + Data.CONTACT_ID + " IN (SELECT " + Contacts._ID + " FROM contacts WHERE " + Contacts.HAS_PHONE_NUMBER + "!=0)",Data.CONTACT_ID);
或使用两个游标来解决它:
Cursor contacts = resolver.query(Contacts.CONTENT_URI,Contacts.HAS_PHONE_NUMBER + " != 0",Contacts._ID + " ASC"); Cursor data = resolver.query(Data.CONTENT_URI,Data.CONTACT_ID + " ASC"); int idIndex = contacts.getColumnIndexOrThrow(Contacts._ID); int nameIndex = contacts.getColumnIndexOrThrow(Contacts.DISPLAY_NAME); int cidIndex = data.getColumnIndexOrThrow(Data.CONTACT_ID); int data1Index = data.getColumnIndexOrThrow(Data.DATA1); boolean hasData = data.moveToNext(); while (contacts.moveToNext()) { long id = contacts.getLong(idIndex); System.out.println("Contact(" + id + "): " + contacts.getString(nameIndex)); if (hasData) { long cid = data.getLong(cidIndex); while (cid <= id && hasData) { if (cid == id) { System.out.println("\t(" + cid + "/" + id + ").data1:" + data.getString(data1Index)); } hasData = data.moveToNext(); if (hasData) { cid = data.getLong(cidIndex); } } } }