我的目標是獲取聯繫人的電話號碼和電子郵件。 我試過使用一個遊標,但不知何故,它返回的手機和電子郵件(兩者之一,根據調整一些事情)相同的東西。我現在想要的是將電子郵件映射到電話號碼或兩個散列表,emailToID和IDToPhone的散列表。這是我迄今爲止,但我使用的ID不是相同的跨參數([email protected]的電話是123,他們各自的ID是不一樣的,不容易映射)。將感激的幫助!Android:如何通過電子郵件從聯繫人中獲取號碼?
public String getPhoneByEmail(String userEmail){
final String EMAIL_URI = ContactsContract.CommonDataKinds.Email.DATA;
final String PHONE_URI = ContactsContract.CommonDataKinds.Phone.NUMBER;
Hashtable<String, Integer> emailToId = new Hashtable<>();
Hashtable<Integer, String> idToPhone = new Hashtable<>();
ContentResolver cr = getContext().getContentResolver();
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
null,
null, null);
Cursor cur2 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
null,
null, null);
while (cur1.moveToNext()) {
String phone = cur1.getString(cur1.getColumnIndex(PHONE_URI));
String id1 = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
idToPhone.put(Integer.parseInt(id1), phone);
}
while (cur2.moveToNext()) {
String email = cur2.getString(cur2.getColumnIndex(EMAIL_URI));
String id2 = cur2.getString(cur2.getColumnIndex(ContactsContract.CommonDataKinds.Email._ID));
emailToId.put(email, Integer.parseInt(id2));
}
cur1.close();
cur2.close();
if (emailToId.get(userEmail)!=null){
int id = emailToId.get(userEmail);
int newId = id - 2;
String phone = idToPhone.get(newId);
return phone;
}
else return "not found";
}