5

I have a listview of all the contact names in the phone. I want to get the email id (if contact have one) of the contact which I click on in the listview. How can I do this?

Bjarke Freund-Hansen
  • 28,728
  • 25
  • 92
  • 135
Monali
  • 1,966
  • 12
  • 39
  • 59

6 Answers6

25

Use the following code to get all email ids. I checked the code. It is working.

public static void getContactEmails(Context context) {
        String emailIdOfContact = null;
        int emailType = Email.TYPE_WORK;
        String contactName = null;


            ContentResolver cr = context.getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                    null, null, null);
            if (cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    String id = cur.getString(cur
                            .getColumnIndex(BaseColumns._ID));
                    contactName = cur
                            .getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    // Log.i(TAG,"....contact name....." +
                    // contactName);

                    cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = ?", new String[] { id }, null);

                    Cursor emails = cr.query(Email.CONTENT_URI, null,
                            Email.CONTACT_ID + " = " + id, null, null);
                    while (emails.moveToNext()) {
                        emailIdOfContact = emails.getString(emails
                                .getColumnIndex(Email.DATA));
                        // Log.i(TAG,"...COntact Name ...."
                        // + contactName + "...contact Number..."
                        // + emailIdOfContact);
                        emailType = emails.getInt(emails
                                .getColumnIndex(Phone.TYPE));


                    }
                    emails.close();

                }
            }// end of contact name cursor
            cur.close();


    }
Ardalan Shahgholi
  • 11,967
  • 21
  • 108
  • 144
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • thanks deepak but this code doesnt helped me.I want to get email address of the contact which i click. (I have a list of contact name) – Monali May 30 '11 at 09:25
  • Here you will get all email id and contacts. create a bean where you will store name and email id. and create an arraylist of that bean. when you click on any contact you will get contact name and contact email id from that position. – Sunil Kumar Sahoo May 30 '11 at 09:29
  • this code works well but it is slow.for a particular contact how to retrieve email id – Android Killer Jun 12 '13 at 07:45
  • @SunilKumarSahoo -when I am reading 2000 contact from phone book in this way along with phone number it is taking 30 to 35 Sec. is there any other way to reduce time – Ajit Kumar Dubey Apr 07 '16 at 05:21
5

Phone Numbers

Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact.

if (Integer.parseInt(cur.getString(
               cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor pCur = cr.query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
         new String[]{id}, null);
         while (pCur.moveToNext()) {
         // Do something with phones
         } 
         pCur.close();
     }

Perform a second query against the Android contacts SQLite database. The phone numbers are queried against the URI stored in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. The contact ID is stored in the phone table as ContactsContract.CommonDataKinds.Phone.CONTACT_ID and the WHERE clause is used to limit the data returned.

Email Addresses

Querying email addresses is similar to phone numbers. A query must be performed to get email addresses from the database. Query the URI stored in ContactsContract.CommonDataKinds.Email.CONTENT_URI to query the email address table

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)); 
 } 
 emailCur.close();
Balaji.K
  • 8,745
  • 5
  • 30
  • 39
0
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI,Uri.encode(name.toString().trim()));
Cursor mapContact = getContext().getContentResolver().query(uri, new String[]{PhoneLookup._ID}, null, null, null);
if(mapContact.moveToNext())
{
 String _id = mapContact.getString(mapContact.getColumnIndex(ContactsContract.Contacts._ID));

}
Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79
0

Xamarin Version of Sunil's answer. Took me a while, but I figured it out.

ContentResolver cr = ContentResolver;
        string contactName = null;
        var cur = cr.Query(ContactsContract.Contacts.ContentUri,null,null,null,null);

        if (cur.MoveToFirst())
        {
            do
            {
                string id = cur.GetString(cur.GetColumnIndex(BaseColumns.Id));
                contactName = cur.GetString(cur.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.DisplayName));
                var emails = cr.Query(ContactsContract.CommonDataKinds.Email.ContentUri, null, ContactsContract.CommonDataKinds.Email.InterfaceConsts.ContactId + " = " + id, null, null);
                if (emails.MoveToFirst()) {
                    do
                    {
                        // This is where it loops through if there are multiple Email addresses
                        var email = emails.GetString(emails.GetColumnIndex(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Data));
                    } while (emails.MoveToNext());
                }
            } while (cur.MoveToNext());
        }
nishantvodoo
  • 835
  • 3
  • 17
  • 32
0

I am using below code. it is working fine. checked it.

ArrayList<ContactInfo> listContactsData = new ArrayList<>();
   // Retrieve Email address 
                Cursor emailCursor = cr.query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                        new String[]{id}, null);
                while (emailCursor.moveToNext()) {
                    // This would allow you get email addresses

                    String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));                              String emailType = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
        Log.e(“Email :“,” ”+email)

                    objContact.strEmail = email;
               }
               emailCur.close();

                listContactsData.add(objContact);
Jignesh Goyani
  • 1,020
  • 9
  • 17
0
 Uri contactData = data.getData();
                Cursor c = getContentResolver().query(contactData, null, null, null, null);
                if (c.moveToFirst()) {
                    String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
                    String hasNumber = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                    String email= "";
                    if (Integer.valueOf(hasNumber) == 1) {
                        Cursor numbers = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
                        while (numbers.moveToNext()) {
                            email= numbers.getString(numbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                            //Toast.makeText(getApplicationContext(), "Number=" + num, Toast.LENGTH_LONG).show();

                            //asdasdasdsa
                            if(getEmail(email).isEmpty()){
                                Toast.makeText(this, "Email Not Found In That Contact Try Another", Toast.LENGTH_SHORT).show();
                            }
                            else {
                                edt_email_contact.setText("" + getEmail(email));
                            }                            }
                    }
                }
                break;
            }
Abhisek
  • 791
  • 5
  • 3