如何从Android内部联系人获取电子邮件?

时间:2013-03-15 11:36:50

标签: android android-contacts contactscontract

我正在开发一个应用程序,我需要获得联系人和来自内部电话簿的电子邮件..

在谷歌搜索后,我发现以下代码获取phonenum&名。

Main.java

Cursor numcur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);
    if (numcur != null) {
        while (numcur.moveToNext()) {
            name = numcur.getString(numcur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            number = numcur.getString(numcur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            phonenumarray.add(number);
            displaynamearray.add(name);

        }
    }

但是这里要获取电子邮件ID,他们正在使用单独的光标,如下所示..

Cursor emailcur = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null,null, null);
    if (emailcur != null) {
        while (emailcur.moveToNext()) {
            email = emailcur.getString(emailcur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA1));
                emailarray.add(email);  

        }
    }

在这里,我们在为list2的listview添加值时单独获取值,它是displayng contact7 mail id,对于contact3,它显示contat5 mail id,如下图所示。 enter image description here

那么,任何人都可以帮助我获取同一联系人的电话号码,邮件ID ..

2 个答案:

答案 0 :(得分:0)

试试这个

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    while (phones.moveToNext()) {
    String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));  
    String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));


    ContactBean objContact = new ContactBean();   
    objContact.setName(name);    
    objContact.setPhoneNo(phoneNumber);   
    list.add(objContact);

请参阅此How to get the Android device's primary e-mail address

答案 1 :(得分:0)