The code looks fine to me (except for the cursor leak).

When you say "but not on 2.0", what exactly do you mean?  Does it not
return any data or throw an exception or what?

BTW, compatibility API will only give you access to the very first
Google account you add on the phone, so make sure the phone number you
are looking for is in that original account.  For the sake of testing,
you might want to use a phone with a single account.  Also, if you are
running this on the emulator, make sure you have the latest version of
the SDK (2.0.1).

Thank you,
- Dmitri

On Fri, Dec 4, 2009 at 3:37 PM, ghassett <[email protected]> wrote:
> Hi -- the semi-standard way of getting a contact name from a phone
> number is to use Contacts.Phones.CONTENT_FILTER_URL and append the
> phone number, as in the following sample code.  This works fine on
> Android 1.6, but not on 2.0 -- is there a way to perform this function
> that works in all versions of the Android API?
>
>
> private String getContactNameFromNumber(String number) {
>                // define the columns I want the query to return
>                String[] projection = new String[] {
>                                Contacts.Phones.DISPLAY_NAME,
>                                Contacts.Phones.NUMBER };
>
>                // encode the phone number and build the filter URI
>                Uri contactUri = Uri.withAppendedPath
> (Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number));
>
>                // query time
>                Cursor c = getContentResolver().query(contactUri, projection, 
> null,
>                                null, null);
>
>                // if the query returns 1 or more results
>                // return the first result
>                if (c.moveToFirst()) {
>                        String name = c.getString(c
>                                        
> .getColumnIndex(Contacts.Phones.DISPLAY_NAME));
>                        return name;
>                }
>
>                // return the original number if no match was found
>                return number;
>        }
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to