views:

501

answers:

1

I would like to retrieve the name of a contact associated with an incoming telephone number. As I process the incoming number in the broascastreceiver having a String with the name of the incoming caller would help my project greatly.

I would think this involves a query using the sql WHERE clause as a filter, but do I need to sort the contacts? An example or hint would be of great assistance.

+3  A: 

For that you need to use the optimized PhoneLookup provider as described here

Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
 resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME} .....)
Pentium10
Right off the dev page, thank you for the quick response. The toString() method in Uri should convert this query to the contacts name?
Noah Seidman
No, it won't you will have to resolve the Cursor yourself. For some help check this questions: http://stackoverflow.com/questions/903343/cursor-get-the-field-value-android
Pentium10
I would initialize a cursor with a managedQuery using the URI, then move the cursor to the fist position and get the data? Once I get the cursor at the first position I use getString? I assume that the first position would be right because the query is for a number therefore the query will pull up names only for that number?
Noah Seidman
That's exactly how you have to do. Don't forget to wrap in a try/catch.
Pentium10
Important on SO, you have to mark accepted answers by using the tick on the left of the posted answer, below the voting. This will increase your rate.
Pentium10