query Contact For Email - Android Account

Android examples for Account:Contact Email

Description

query Contact For Email

Demo Code

/**/*from www.j  av  a 2  s .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 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