get Contact Phone From Name - Android Account

Android examples for Account:Contact Name

Description

get Contact Phone From Name

Demo Code


import java.util.ArrayList;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;

public class Main{
    //from w  w w .ja va2s.  c o m
    public static ArrayList<String> getPhoneFromName(Context context,
            String name) {
        ArrayList<String> phone = new ArrayList<String>();
        String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER };
        String[] selectionArgs = new String[] { "%" + name + "%" };
        Cursor cursor = context.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection, // Which columns to return.
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                        + " like ? ", // WHERE clause.
                selectionArgs, // WHERE clause value substitution
                null); // Sort order.

        if (cursor == null) {
            // Log.d(TAG, "getPeople null");
            return null;
        }
        ZogUtils.printLog(ContactUtils.class,
                "getPeople cursor.getCount() = " + cursor.getCount());
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToPosition(i);

            int nameFieldColumnIndex = cursor
                    .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
            name = cursor.getString(nameFieldColumnIndex);

            String phoneNumber = cursor
                    .getString(cursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            // Log.i(TAG, "" + name + " .... " + nameFieldColumnIndex);

            phone.add(phoneNumber);

        }
        cursor.close();
        return phone;

    }
}

Related Tutorials