我想从单个查询中获取名字、姓氏、电子邮件 ID 的 android 联系人详细信息。如果我通过以下过程获取联系人。获取和过滤需要花费大量时间。请给出解决方案来解决这个问题。目前我正在这样做。请查看下面的代码。
public static JSONArray getMABTosendServer(Context context, UserBean user,
DatabaseUserManager userManager) throws JSONException {
hashedEmails = new HashMap<String, AddressBean>();
JSONArray jsonArray = new JSONArray();
HashMap<String, String> emailValues = new HashMap<String, String>();
StringBuffer sb = new StringBuffer();
sb.append("......Contact Details.....");
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
String emailContact = "";
String emailType = "";
Cursor nameCursor = null;
String firstName = "";
String lastName = "";
String id = null;
long userLastContactId = 0;
int i = 0;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
i++;
id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
if (user != null && user.getLastContactId() == null) {
userLastContactId = 0;
} else {
userLastContactId = Long.parseLong(user.getLastContactId());
}
if (Long.parseLong(id) > userLastContactId) {
Uri contactUri = ContentUris.withAppendedId(
Contacts.CONTENT_URI, Long.parseLong(id));
Uri dataUri = Uri.withAppendedPath(contactUri,
Contacts.Data.CONTENT_DIRECTORY);
nameCursor = context.getContentResolver().query(dataUri,
null, Data.MIMETYPE + "=?",
new String[] { StructuredName.CONTENT_ITEM_TYPE },
null);
while (nameCursor.moveToNext()) {
firstName = nameCursor.getString(nameCursor
.getColumnIndex(Data.DATA2));
lastName = nameCursor.getString(nameCursor
.getColumnIndex(Data.DATA3));
Cursor emailCur = cr
.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] { id },
null);
while (emailCur.moveToNext()) {
emailContact = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
emailType = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
sb.append("\nEmail:" + emailContact + "Email type:"
+ emailType);
if (EMAIL_ADDRESS_PATTERN.matcher(
emailContact.trim()).matches()
&& !emailValues.containsKey(emailContact)) {
emailValues
.put(emailContact, String.valueOf(i));
JSONObject jsonObject = new JSONObject();
AddressBean ab = new AddressBean();
if (firstName == null) {
firstName = "";
}
if (lastName == null) {
lastName = "";
}
ab.setFirstName(firstName);
ab.setLastName(lastName);
ab.setEmailId(emailContact);
jsonObject.put("firstName", firstName);
jsonObject.put("lastName", lastName);
jsonObject.put("email", Encryption
.encryptUsingSha1(emailContact
.toLowerCase().trim()));
hashedEmails.put(Encryption
.encryptUsingSha1(emailContact), ab);
jsonArray.put(jsonObject);
}
}
emailCur.close();
}
nameCursor.close();
}
}
cur.close();
user.setLastContactId(id);
userManager.updateUser(user);
}
return jsonArray;
}