get Email from Contact - Android Account

Android examples for Account:Contact Email

Description

get Email from Contact

Demo Code


//package com.java2s;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;

public class Main {
    public static String getEmail(Context context, Intent data) {
        String email = "";
        Uri contactUri = data.getData();
        Cursor cursor = context.getContentResolver().query(contactUri,
                null, null, null, null);
        try {//w ww . j a  v a 2 s .c  o  m
            cursor.moveToFirst();
            String contactId = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts._ID));
            Cursor emails = context.getContentResolver().query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID
                            + " = " + contactId, null, null);
            emails.moveToFirst();
            email = emails
                    .getString(emails
                            .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            emails.close();
        } catch (Exception e) {
            email = "No email found";
        }

        return email;
    }
}

Related Tutorials