2016-08-02 67 views
0

我的目標是獲取聯繫人的電話號碼和電子郵件。 我試過使用一個遊標,但不知何故,它返回的手機和電子郵件(兩者之一,根據調整一些事情)相同的東西。我現在想要的是將電子郵件映射到電話號碼或兩個散列表,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"; 
} 

回答

0

與幫助,得到它從這個:https://stackoverflow.com/a/4154729/6463084

private String getPhoneByEmail(String userEmail) { 
    Hashtable<String, String> emailToId = new Hashtable<>(); 
    Hashtable<String, String> idToPhone = new Hashtable<>(); 
    ContentResolver cr = getContext().getContentResolver(); 
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
    while (cursor.moveToNext()) { 
     String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
     String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
     String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
     if (hasPhone.equalsIgnoreCase("1")) 
      hasPhone = "true"; 
     else 
      hasPhone = "false"; 
     if (Boolean.parseBoolean(hasPhone)) { 
      Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null); 
      while (phones.moveToNext()) { 
       String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       idToPhone.put(contactId, phoneNumber); 
      } 
      phones.close(); 
     } 
     Cursor emails = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null); 
     while (emails.moveToNext()) { 
      String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
      emailToId.put(emailAddress, contactId); 
     } 
     emails.close(); 
    } 
    cursor.close(); 
    if (emailToId.get(userEmail) != null) { 
     String id = emailToId.get(userEmail); 
     if (idToPhone.get(id) != null) { 
      String phone = idToPhone.get(id); 
      return phone; 
     } 
    } 
    return "not found"; 
} 
0

試試這個代碼讀取聯繫人號碼/姓名/電子郵件從電話..

public void storeContactsInLocal() { 
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; 
    // retrieve all contacts as a cursor. 
    Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, sortOrder); 

    //now we have cusror with contacts and get diffrent value from cusror. 
    String lastNumber = null; 

    while (cursor.moveToNext()) { 
     String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
     String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
     String email = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 

     String number = phoneNumber.replaceAll("\\s+", ""); 

     if (!number.equals(lastNumber)) { 
      lastNumber = number; 

      try { 

       // Save Contact in Database! 


       String contactDetail = "Name :" + name + " : " + "Number :" + number+ " : " + "Email :" + email; 

       Log.e("Contact :", contactDetail); 


      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

現在從調用這個函數你活動像..

new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 

       Thread t = new Thread(new Runnable() { 
        @Override 
        public void run() { 
         storeContactsInLocal(); 
        } 
       }); 
       t.start(); 

     } 
    }, 100); 

只是不要忘記問/添加權限閱讀聯繫人!

相關問題