views:

70

answers:

2

In my app, I get the name of the person calling from the contacts database and read it out. A user contacted me about a number in the format 123-456-7890 which doesn't work. I've tested this myself and can't see why it wouldn't work.

The full code I'm using is available here, but the relevant part is below:

Cursor c = getContentResolver().query(Contacts.Phones.CONTENT_URI, 
                            new String[] {Contacts.Phones.DISPLAY_NAME}, 
                            selection, 
                            args, null);
+1  A: 

Hi,

Check out PhoneLookup.

ognian
I'm looking for something compatible with API level 4 (Donut)
Al
A: 

This snippet is from my App, it uses Memoization...

static HashMap<String,String> phone2Name = new HashMap<String, String>();

    public static String getDisplayNameFromPhoneNo(Context ctx,String phoneNo) {
        if(phone2Name.containsKey(phoneNo))
        {
            return phone2Name.get(phoneNo);
        }
        if(phoneNo.trim().length() == 0) return null;

        String[] projection = new String[] {
                Contacts.Phones.DISPLAY_NAME,
                Contacts.Phones.NUMBER };


        Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(phoneNo));

        Cursor c = ctx.getContentResolver().query(contactUri, projection, null,
                null, null);

        if (c.moveToFirst()) {
            String name = c.getString(c
                    .getColumnIndex(Contacts.Phones.DISPLAY_NAME));
            phone2Name.put(phoneNo, name);
            return name;
        }

        return null;
    }
st0le
Thanks, I got it working now. One thing I did notice about your code was you don't close the cursor, that'll make it throw a (non-crash causing) exception during finalization.
Al
doh! ;) [15 chars]
st0le