get Contact Name By Phone Number - Android Account

Android examples for Account:Contact Name

Description

get Contact Name By Phone Number

Demo Code


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

public class Main {

    public static String getContactNameByPhoneNumber(Context context,
            String address) {/*from   w  ww.  j  a  v  a2 s  . c om*/
        String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER };

        Cursor cursor = context.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection,
                ContactsContract.CommonDataKinds.Phone.NUMBER + " = '"
                        + address + "'", null, null);

        if (cursor == null) {
            return "";
        }
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToPosition(i);

            int nameFieldColumnIndex = cursor
                    .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
            String name = cursor.getString(nameFieldColumnIndex);
            return name;
        }
        return "";
    }
}

Related Tutorials