0

I am first time on stack-overflow, so please forgive me if I am not able to explain the question properly OR not following the policies of this community in any way.

My Question is : Today morning I was learning from stack-overflow and suddenly a question struck my mind. Supposed my email address is simran....@gmail.com and this is currently logged-In in my device. So how to get the all email contacts associated with my email account. I have 200+ folks added in my gmail account, so want to show these all in listview.

Note : I know how to implement the list-view and how to get the primary email address and other email logged-in my device.

What I Need : I want to fetch all email contacts of simran....@gmail.com. Is this possible and if the answer is yes then how? Can we get this without permission of user?

Thanks Simran

Community
  • 1
  • 1

1 Answers1

-1

You can use this code to get all the email contacts from your contact list -

public ArrayList<String> getNameEmailDetails(){
        ArrayList<String> names = new ArrayList<String>();
        ContentResolver cr = 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(ContactsContract.Contacts._ID));
                Cursor cur1 = cr.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(email!=null){
                        names.add(name);
                    }
                } 
                cur1.close();
            }
        }
        return names;
    }

Hope this helps you.

You have to add permission -

<uses-permission android:name="android.permission.READ_CONTACTS" />

Without this permission its seems not to be possible.