Android:如何通过电子邮件从通讯录中获取号码?

时间:2016-08-02 06:15:16

标签: android contacts

我的目标是获取联系人的电话号码和电子邮件。 我试过使用一个光标,但不知怎的,它为电话和电子邮件返回相同的东西(两者中的任何一个,取决于调整一些东西)。我现在想要的是一个哈希表,它将电子邮件映射到电话号码,或两个哈希表,emailToID和IDToPhone。这是我到目前为止,但我使用的ID不是相同的参数(a@a.com' s手机是123,它们各自的ID不一样,不能轻易映射)。不胜感激求助!

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";
}

2 个答案:

答案 0 :(得分: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";
}

答案 1 :(得分: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);

不要忘记询问/添加权限以阅读联系人!