ShReYaNsH
ShReYaNsH

Reputation: 302

Searching Phone number in Android Code

I am trying to implement a functionality in android app where as user keys in the numbers, I want to incrementally search those numbers in Phone book (the generic phone book search) and display result.

For this, I am using

Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(aNumber));

This seems to work for most of the cases and is handling search for ' ' etc.

There are 2 issues that I am not able to resolve :

  1. It does not ignore the country code.

So as an e.g. if I have a number : +9199776xx123 When my search string is +9199, the result comes up. While if my search string is 9977, it does not come up.

  1. It does not search from between. When my search string is 776, then also result does not come up.

So the behavior of CONTENT_FILTER_URI of Phone is not exactly clear to me.

P.S. : I have tried PhoneLookup but for some reason, it does not throw any result. My belief is that it might not be capable to searching partial numbers.

Upvotes: 1

Views: 660

Answers (2)

ShReYaNsH
ShReYaNsH

Reputation: 302

After quite a lot or research, I was able to find the answer. Now, I search on both the columns, Number and normalized number. This takes care of both the cases since normalized number contains country code in the number.

My selection string looks like :

            String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ? OR "
                + ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER + " LIKE ?";

Upvotes: 1

marmor
marmor

Reputation: 28239

Phone.CONTENT_FILTER_URI is built for speed, but it's not perfect, as you've noticed. You can use the plain Phone.CONTENT_URI to process any kind of request you want.

e.g.:

String partialPhone = "9977";
String[] projection = new String[] {Phone.DISPLAY_NAME, Phone.NUMBER, Phone. NORMALIZED_NUMBER};
String selection = "(" + Phone.NUMBER + " LIKE %" + partialPhone + "%) OR (" + Phone.NORMALIZED_NUMBER + " LIKE %" + partialPhone + "%)";
Cursor c = cr.query(Data.CONTENT_URI, projection, selection, null, null);

Upvotes: 0

Related Questions