如何从联系人列表中获取电子邮件 ID、姓名和号码

Posted

技术标签:

【中文标题】如何从联系人列表中获取电子邮件 ID、姓名和号码【英文标题】:How to get email id, name and number from contact list 【发布时间】:2018-11-15 17:27:50 【问题描述】:

我正在尝试从联系人列表中获取姓名、联系人编号和电子邮件 ID,并将其存储在数组列表中并转发到服务器。我将姓名、电话和电子邮件 ID 存储到 contactNameArrayListcontactNumberArrayListcontactEmailIdArrayList

下面的代码我得到了姓名、电子邮件和联系人号码,但是如何将特定的电子邮件 ID 存储到特定的联系人号码?

private void getAllContacts() 
    ContentResolver contactResolver = context.getContentResolver();

    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 contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));               
            Log.e("TAG",  " Name: " + displayName+"==>phone Number==>"+number);
            contactNameArrayList.add(displayName);
            contactNumberArrayList.add(number);
            final String[] projection = new String[]ContactsContract.CommonDataKinds.Email.DATA,
            ContactsContract.CommonDataKinds.Email.TYPE;            
            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,"EMAIL==>"+email);
            

            emailCursor.close();
        

        cursor.close();

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

上面的代码我调用getAllcontact 方法。我得到了号码、姓名和电子邮件 ID,并存储在 contactNameArrayListcontactNumberArrayListcontactEmailAddressList 中。但是当我收到电子邮件 id 时,回复是这样的:

姓名:efg==>电话号码==>(256) 655-555 EMAIL==>abc@gmail.com EMAIL==>pqr@gmail.com EMAIL==>xyz@gmail.com 姓名:abc==>电话号码==>(256) 655-555 EMAIL==>abc@gmail.com EMAIL==>pqr@gmail.com EMAIL==>xyz@gmail.com 姓名:pqr==>电话号码==>(987) 456-1238 EMAIL==>abc@gmail.com EMAIL==>pqr@gmail.com EMAIL==>xyz@gmail.com 姓名:xyz==>电话号码==>(987) 456-1238 EMAIL==>abc@gmail.com EMAIL==>pqr@gmail.com EMAIL==>xyz@gmail.com

那么如何获取特定手机的电子邮件 ID 并将其存储在 contactEmailArrayList 中。根据上述回复conatactNameArrayListcontactNumberArrayList 大小相同但ContactEmailAddressArrayList 大小不同。所以请给我建议。

【问题讨论】:

【参考方案1】:

为获取电子邮件、姓名、号码等设置一门课程。

public class UserContact 
private String userName,userEmail;
private boolean isSelect;

public boolean isSelect() 
    return isSelect;


public void setSelect(boolean select) 
    isSelect = select;


public UserContact()
public UserContact(String userName, String userEmail) 
    this.userName = userName;
    this.userEmail = userEmail;


public String getUserName() 
    return userName;


public void setUserName(String userName) 
    this.userName = userName;


public String getUserEmail() 
    return userEmail;


public void setUserEmail(String userEmail) 
    this.userEmail = userEmail;

然后在主要活动中列出一个作为全局级别的列表。

    private List<UserContact> mContactList = new ArrayList<>();

然后调用 getContact details。

 /**
 * this method read device in contact name and email.
 */
public void getContact() 
    if (PermissionManager.checkPermission(getActivity(), READ_CONTACT)) 
        mContactList.clear();
        List<UserContact> contactList = new ArrayList<>();
        Cursor cur = getActivity().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (cur.getCount() > 0) 
            while (cur.moveToNext()) 
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                Cursor cur1 = getActivity().getContentResolver().query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                        new String[]id, null);
                while (cur1.moveToNext()) 
                    //to get the contact names
                    String name = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    Log.e("Name :", name);
                    String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    Log.e("Email", email);
                    if (!TextUtils.isEmpty(email)) 
                        contactList.add(new UserContact(name, email));
                    
                
                cur1.close();
            
        
        mContactList = contactList;
        setAdapter();
     else 
        PermissionManager.askForPermission(READ_CONTACT, getActivity());
    

上述方法只获取姓名和电子邮件,您可以修改您的需求并添加电话号码代码。

【讨论】:

字符串编号 = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));试试,但我的电子邮件 ID 是数字

以上是关于如何从联系人列表中获取电子邮件 ID、姓名和号码的主要内容,如果未能解决你的问题,请参考以下文章

从电话联系人中获取姓名、电话号码和电子邮件地址[关闭]

如何从联系人列表中获取姓名

如何从联系人中获取电子邮件和姓氏?

从联系人选择器意图中选择电子邮件、姓名和电话号码

android获取联系人图片、姓名和手机号码

Android从联系人列表中获取电话号码