Contact phone Number Lookup - Android Account

Android examples for Account:Contact Number

Description

Contact phone Number Lookup

Demo Code


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

public class Main {
    public static String phoneNumberLookup(Context context, String number) {

        //Log.e("PhoneNumberLookup", number);

        Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(number));/*from w w w . ja  va  2  s  .co  m*/
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(uri,
                    new String[] { PhoneLookup.DISPLAY_NAME
                    /*, PhoneLookup.TYPE
                      , PhoneLookup.LABEL
                      , PhoneLookup.ID */
                    }, null, null, null);
        } catch (IllegalArgumentException e) {
            return number;
        }

        // Take the first match only
        if (cursor != null && cursor.moveToFirst()) {
            int nameIndex = cursor
                    .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
            if (nameIndex != -1) {
                String name = cursor.getString(nameIndex);
                //Log.e("PhoneNumberLookup", "success: " + name);
                cursor.close();
                return name;
            }
        }

        if (cursor != null)
            cursor.close();

        return number;

    }
}

Related Tutorials