简体   繁体   English

如何从联系人列表中获取电子邮件地址?

[英]how to get email address from contact list?

how to get email address from contact list.below code I got number and name and display on list.but I want number,name and email address so pls check below code 如何从联系人列表中获取电子邮件地址。下面的代码我得到了电话号码和姓名并显示在list.but我想要电话号码,姓名和电子邮件地址,所以请检查以下代码

 ContentResolver contactResolver = context.getContentResolver();

    Cursor c = contactResolver.query(ContactsContract.Contacts.CONTENT_URI, new String[] { ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER }, null, null, null);
    Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
            null, null, null);
    if(cursor.getCount()>0)
        while ( cursor.moveToNext()) {

            String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            // String photoUri = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            //  String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));

            Log.e("TAG",  " Name: " + displayName+"==>phone Number==>"+number);

            contactNameArrayList.add(displayName);
            contactNumberArrayList.add(number);

            if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {


                Cursor pCur = contactResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactId }, null);


                while (pCur.moveToNext())
                {
                    String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String type = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                    String s = (String) ContactsContract.CommonDataKinds.Phone.getTypeLabel(context.getResources(), Integer.parseInt(type), "");

                   // Log.e("TAG", s + " phone: " + phone);

                }
                pCur.close();
            }

            Cursor emailCursor = contactResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null,
                    null, null, null);

            while (emailCursor.moveToNext())
            {
                String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                int type = emailCursor.getInt(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
                String s = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(context.getResources(), type, "");

                Log.e("TAG", s + " email: " + email);
            }

            emailCursor.close();

        }  cursor.close();


    Log.e(TAG,"contactNameArrayList sIZE==>"+contactNameArrayList.size()+"contactNumberArrayList size==>"+contactNumberArrayList.size()+"contactEmailAddressArrayList size==>"+contactEmailAddressArrayList.size());

}

I create three array for name and number and email id but above code I got only mobile and name.when I am add name and mobile no in array so simultaneously store email id particular mobile no in email id array.so pls help ... 我为名称和数字以及电子邮件ID创建了三个数组,但是在上面的代码中,我只有手机和name。当我在数组中添加名称和手机No时,所以同时在电子邮件ID array中同时存储电子邮件ID特别是手机No.so请帮助...

You can get Email from contact using this 您可以使用此方法从联系人处获取电子邮件

public List<String> getEmail(int contactId) {
    List<String> emailStr = new ArrayList<String>();
    ContentResolver cr = context.getContentResolver();
    Cursor emailCur = cr.query(
            ContactsContract.CommonDataKinds.Email.CONTENT_URI,
            null,
            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
            new String[]{String.valueOf(contactId)}, null);
    while (emailCur.moveToNext()) {
        String email = emailCur.getString(
                emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));


        emailStr.add(email);
        System.out.println("Email " + email );
    }
    emailCur.close();
    return emailStr;

}

Use Following Code to get multiple numbers and email from one contact and all contacts from phonebook. 使用“以下代码”可从一个联系人以及电话簿中的所有联系人获取多个号码和电子邮件。

private void getContactsFromPhoneBook() {

    String unique_id = ApplicationConstant.phone_id;
    System.out.println("Unique_ID  : " + unique_id);

    Contact_To_Sync contact_to_sync = new Contact_To_Sync();

    List<Contacts> AllContact_toSync = new ArrayList<>();

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);

    System.out.println("Contacts Count : " + cur.getCount());

    if (cur.getCount() > 0) {

        Contacts contacts = null;

        List<String> Phones;
        List<String> Emails;

        while (cur.moveToNext()) {

            contacts = new Contacts();
            Phones = new ArrayList<>();
            Emails = new ArrayList<>();

            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String timeStamp = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP));




                contacts.setContact_id(id);
                contacts.setName(name);

                System.out.println("name : " + name + ", ID : " + id + " Status : " + timeStamp);

                // get the phone number
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        new String[]{id}, null);

                while (pCur.moveToNext()) {
                    String phone = pCur.getString(
                            pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    System.out.println("phone" + phone);

                    Phones.add(phone);

                }
                pCur.close();


                // get email and type

                Cursor emailCur = cr.query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                        new String[]{id}, null);
                while (emailCur.moveToNext()) {
                    // This would allow you get several email addresses
                    // if the email addresses were stored in an array
                    String email = emailCur.getString(
                            emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    String emailType = emailCur.getString(
                            emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

                    Emails.add(email);

                    System.out.println("Email " + email + " Email Type : " + emailType);
                }
                emailCur.close();


                contacts.setEmails(Emails);
                contacts.setPhones(Phones);

                AllContact_toSync.add(contacts);


        }

        contact_to_sync.setContacts(AllContact_toSync);
        contact_to_sync.setPhone_device_name(unique_id);

        if (!contact_to_sync.getContacts().isEmpty()) {
            Sync_Contacts_To_Server(contact_to_sync);
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从联系人列表中获取电子邮件ID,姓名和电话号码 - How to get email id, name and number from contact list 使用TextView从通讯簿获取联系人 - Use a TextView to get a Contact from Address Book 如何从设备获取单独的联系人姓名数组列表和联系人号码列表 - how to get the separate contact name array list and contact number array list from the device 如何在Android中获取已保存的联系人电子邮件? - How to get saved contact email in android? 如何从Java中的电子邮件地址获取IMAP主机名? - How to get IMAP Host name from email address in java? 如何从uid Firebase数据库获取电子邮件地址? - How to get the email address from uid Firebase database? 如何从Android中的联系人读取电子邮件字段? - How to read email field from contact in android? 根据Android发送的电子邮件地址从服务器获取具有特定记录的列表视图 - get list view with specific records from server based on email address sent from android 如何以编程方式从二维码中获取所有数据,如姓名、电子邮件、联系信息? - How to get all data from Qr code like Name, Email, Contact info programetically? 从 email 地址获取谷歌日历 ID - get google calendar id from an email address
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM