get Contact Name - Android Account

Android examples for Account:Contact Name

Description

get Contact Name

Demo Code


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

public class Main {
    public static String getContactName(Context context, String phoneNumber) {
        ContentResolver cr = context.getContentResolver();
        Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(" like'%" + phoneNumber + "%'"));
        Cursor cursor = cr//from ww w.j a v a 2s  . c  o m
                .query(uri, new String[] { PhoneLookup.DISPLAY_NAME },
                        null, null, null);
        if (cursor == null) {
            return null;
        }
        String contactName = null;
        if (cursor.moveToFirst()) {
            contactName = cursor.getString(cursor
                    .getColumnIndex(PhoneLookup.DISPLAY_NAME));
        }

        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }

        return contactName;
    }
}

Related Tutorials