开发者

Android Matching Phone Number Content Resolver w/ different formatting

I am looking at building a co开发者_开发问答ntent resolver query which tries to do some phone number matching. It does not appear to be working when the phone number in the DB is stored w/ a different format as the one I am trying to compare to

eg. LocalContact Phone Number = '1 555 555 1234' I am trying to compare with '+15555551234'

String phoneQueryString = "( mimetype = 'vnd.android.cursor.item/phone_v2' 
                             AND (data1 = '+15555551234'  ))";

Cursor myContacts3 = myCR.query(
                    Data.CONTENT_URI,
                    null, 
                    phoneQueryString, 
                    null,
                    null);

myContacts3.getCount() returns zero records. I know I could use phoneNumberUtils to grab every phone number in the DB & PhoneNumberUtils.compare(num1,num2) but that is not efficient. I can strip out '+' from my '+15555551234', but then if it was reversed where my local contact was '+15555551234' and my source was '15555551234' it would not work. basically I need to strip out any extra non-number chars in the query on data1. Or other suggestions are welcome.


So there is an undocumented sqlite function google added to the sqlite extern library: static void phone_numbers_equal(sqlite3_context * context, int argc, sqlite3_value ** argv)

I have not used it for a while, but you should be able to use this:

String phoneQueryString = "( mimetype = 'vnd.android.cursor.item/phone_v2' 
                             AND (phone_numbers_equal(data1,'+15555551234')  ))";

Cursor myContacts3 = myCR.query(
                    Data.CONTENT_URI,
                    null, 
                    phoneQueryString, 
                    null,
                    null);


You can try something like this:

String s = //your phone number goes here
s = s.replaceAll("[^0-9]", "");

This should remove all characters that are not 0-9. If you use that on both phone numbers their formats should match.


You should use ContactsContract.PhoneLookup.

The format of the phone number does not matter. Comparison is robust and highly optimized on Android; it's done using a native sqlite function named PHONE_NUMBERS_EQUAL.

Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode("+15555551234"));
Cursor phone =getContentResolver().query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜