Get the main email address of the contact - Android Account

Android examples for Account:Contact Email

Description

Get the main email address of the contact

Demo Code

/**//from  w w  w.java  2s.  com
 * Property of Matt Allen
 * mattallen092@gmail.com
 * http://mattallensoftware.co.uk/
 *
 * This software is distributed under the Apache v2.0 license and use
 * of the Repay name may not be used without explicit permission from the project owner.
 *
 */
import android.content.Context;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;
import java.util.HashMap;

public class Main{
    private static final String TAG = ContactsContractHelper.class
            .getName();
    private static int[] typesEmail = new int[] {
            ContactsContract.CommonDataKinds.Phone.TYPE_HOME,
            ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
            ContactsContract.CommonDataKinds.Phone.TYPE_WORK };
    /**
     * Get the main email address of the contact
     * @param contactID The last known ContactID of the contact
     * @param c The context to run in
     * @return String representation of their email address
     * @throws android.database.CursorIndexOutOfBoundsException
     */
    public static HashMap<String, String> getContactsEmailAddress(
            String contactID, Context c)
            throws CursorIndexOutOfBoundsException {
        /*
         * For some shitting reason, using ContactsContract.CommonDataKinds.Phone works instead of Email?
         * Leaving it anyway, might just be some stupid HTC Sense 5 bug
         */
        HashMap<String, String> emails = new HashMap<String, String>();
        for (int i = 0; i <= typesEmail.length - 1; i++) {
            String[] whereArgs = new String[] { String.valueOf(contactID),
                    String.valueOf(typesEmail[i]) };
            String email = queryContactForEmail(c, whereArgs);
            if (email != null) {
                if (ContactsContract.CommonDataKinds.Phone.TYPE_HOME == typesEmail[i]) {
                    emails.put("Home", email);
                } else if (ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE == typesEmail[i]) {
                    emails.put("Other", email);
                } else if (ContactsContract.CommonDataKinds.Phone.TYPE_WORK == typesEmail[i]) {
                    emails.put("Work", email);
                }
            }
        }
        return emails;
    }
    private static String queryContactForEmail(Context c, String[] whereArgs) {
        String phoneNumber = null;
        Cursor cursor = c.getContentResolver().query(
                ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID
                        + " = ? and "
                        + ContactsContract.CommonDataKinds.Email.TYPE
                        + " = ?", whereArgs, null);

        int phoneNumberIndex = cursor
                .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.ADDRESS);

        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            phoneNumber = cursor.getString(phoneNumberIndex);
            cursor.close();
        }
        Log.i(TAG, "Returning email address: " + phoneNumber);
        return phoneNumber;
    }
}

Related Tutorials