Get contacts with email id

让人想犯罪 __ 提交于 2019-12-01 10:48:59
Leon

I just ran into the same problem. I know this thread is pretty old, but maybe this answer will help somebody else in the future.

You must filter by MIME type to get rid of duplicates. This is how I did it:

Uri contacts = ContactsContract.Data.CONTENT_URI;

String[] projection = new String[] {
        ContactsContract.Contacts._ID,
        ContactsContract.Contacts.LOOKUP_KEY,
        ContactsContract.Contacts.DISPLAY_NAME
};

String selection =
        ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ? AND " + 
        ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?"  + " AND " +
        ContactsContract.Data.MIMETYPE + "='" +
        ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'";

String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

mContactCursor = managedQuery(
        contacts,
        projection,
        selection,
        new String[] {"1", constraint.toString() + '%'},
        sortOrder);

Try using this snippet: Showing Contact name and Email on the same row in list view.

 /**
 * Populate the contact list based on account.
 */
private void populateContactList() {
    // Build adapter with contact entries

    Cursor cursorEmail = getContactsEmail();//get all emails

    String[] fields = new String[] //fields of data to take
    {       ContactsContract.Contacts._ID,
            ContactsContract.Data.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Email.DATA
    };
    SimpleCursorAdapter adapter = 
            new SimpleCursorAdapter(this, R.layout.contact_entry, cursorEmail ,
            fields, new int[] 
                         {R.id.UID,R.id.contactEntryText,R.id.contactEmail});
    mContactList.setAdapter(adapter);
}

/**
 * Obtains the contact list for the currently selected account.
 *
 * @return A cursor for for accessing the contact list.
 */
private Cursor getContactsEmail()
{
    // Run query
    Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Email.DATA
    };
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP +"='1'";
    //showing only visible contacts  
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
  return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}

just do it in simple way first of all find contact_id, on basis of it we will search all email_ids related for that contact. on any button click event write this code

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 2);

now on activity result,

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    System.out.println("Request Code--"+requestCode);
    super.onActivityResult(requestCode, resultCode, data);
    if (data != null && requestCode == 2) 
     {
            fromCurrent = true;
            Uri uri = data.getData();
            //fromCurrent=true;
            if (uri != null) {
                Cursor c = null;
                try {
                    c = getContentResolver().query(uri, new String[]{ 
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                                ContactsContract.CommonDataKinds.Email.DATA ,
                                ContactsContract.CommonDataKinds.Email.TYPE },
                            null, null, null);

                    if (c != null && c.moveToFirst()) {
                        String id = c.getString(0);
                        String name = c.getString(1);

                        System.out.println("id  "+id+" name:"+name);

                        ContactID = id;
                        retriveEmail();


                        if(arrEmail.size() == 0)
                        {
                            showToast("No Email Address found for the selected contact!");
                        }
                        else
                        {
                        ListFile = arrEmail.toArray(new String[arrEmail.size()]);

                          builder1 = new AlertDialog.Builder(ShareTheHeart_Activity.this);

                          builder1.setTitle("Select an email address :");

                          builder1.setSingleChoiceItems(ListFile,-1,new DialogInterface.OnClickListener() {     //@Override
                         public void onClick(DialogInterface dialog, int which) 
                         {
                            txtEmail.setText(ListFile[which]);
                            alert.cancel();
                          }
                         });

                          alert = builder1.create();
                          alert.show();      

                        }
                    }
                } finally {
                    if (c != null) {
                        c.close();
                    }
                }
            }
        }
}

where retriveEmail is created method, write it in your code this way,

  private void retriveEmail()
{
    try { 
        arrEmail = new ArrayList<String>();
    String id = ContactID;

     // query for everything email  
   cursor = getContentResolver().query(Email.CONTENT_URI,  
            null, Email.CONTACT_ID + "=?", new String[] { id },  
            null);  

    int emailIdx = cursor.getColumnIndex(Email.DATA);  

    // let's just get the first email  
   if (cursor.moveToFirst())
   {
   do{  
        email = cursor.getString(emailIdx);  
        arrEmail.add(email);
        Log.v("ABC", "Got email: " + email);  
    } while(cursor.moveToNext());
    } else {  
        Log.w("ABC", "No results");  
    }  

} catch (Exception e) {  
    Log.e("ABC", "Failed to get email data", e);  
} finally {  
    if (cursor != null) {  
        cursor.close();  
    }   
    }
}

that's it.

please understand the code, don't just copy and paste!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!