get Contact Name From Number - Android Account

Android examples for Account:Contact Name

Description

get Contact Name From Number

Demo Code


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

public class Main {
    public static String getContactNameFromNumber(Context context,
            String number) {/*from   w  w  w  .j a  v a 2s.c  om*/
        if (number == null || number.equals(""))
            return "";

        Uri lookupUri1 = Uri.withAppendedPath(
                PhoneLookup.CONTENT_FILTER_URI, number);

        String name = getStoredValue(context, lookupUri1, null, null,
                CommonDataKinds.Phone.DISPLAY_NAME);

        return name;

    }

    public static String getStoredValue(Context context, Uri uri,
            String selection, String[] selectionArgs, String column) {
        String value = "";
        Cursor c = context.getContentResolver().query(uri,
                new String[] { column }, selection, selectionArgs, null);
        try {
            if (c.moveToFirst()) {
                value = c.getString(c.getColumnIndex(column));
            }
        } finally {
            c.close();
        }
        return value;
    }
}

Related Tutorials