0

My goal is to get the phone number and the e-mail of a contact. I've tried to use one cursor but somehow it returns the same thing for phone and e-mail (either of the two, depending on tweaking some things). What I want right now is a hashtable that maps e-mails to phone numbers, or two hashtables, emailToID, and IDToPhone. This is what I have so far, but the IDs I use are not the same accross parameters (the a@a.com's phone is 123, their respective IDs are not the same and cannot be easily mapped). Would be grateful for help!

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";
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

2 Answers2

0

Got it with help from this: 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";
}
Community
  • 1
  • 1
0

Try this code to Read Contact Number/Name/Email From Phone..

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();
            }
        }
    }
}

Now call this Function from Your Activity like..

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

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

        }
    }, 100);

Just don't forget to ask/Add Permission to read Contacts!

Uttam Panchasara
  • 5,735
  • 5
  • 25
  • 41