get Contact Name From Phone - Android Account

Android examples for Account:Contact Name

Description

get Contact Name From Phone

Demo Code


//package com.java2s;
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;

import android.provider.ContactsContract;

public class Main {
    public static ArrayList<String> getNameFromPhone(Context context,
            String number) {//from w  w w .j  a  v a 2  s.co  m
        ArrayList<String> name = new ArrayList<String>();
        String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER };

        Cursor cursor = context.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection, // Which columns to return.
                ContactsContract.CommonDataKinds.Phone.NUMBER + " = '"
                        + number + "'", // WHERE clause.
                null, // WHERE clause value substitution
                null); // Sort order.

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

            int nameFieldColumnIndex = cursor
                    .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
            String nameTemp = cursor.getString(nameFieldColumnIndex);
            // Log.i(TAG, "" + name + " .... " + nameFieldColumnIndex);
            name.add(nameTemp);
        }
        cursor.close();
        return name;

    }
}

Related Tutorials