简体   繁体   中英

Search Phone numbers from Android Contact Database

I am implementing an AutocompleteView to search phone numbers. The code is working fine except in some conditions.

My Code :

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE};
    String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ?";
    String[] selectionArgs = new String[]{"%" + charSequence.toString() + "%"};
    Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

This code is working fine where there is no space in the phone numbers. For example if i enter ' 123 ' in my autocompleteView, it is able to find phone numbers like '9123456789' or '8283929383' but it is not able to find numbers '9123 456 789' or '912 3456 789'

I even tried implementing this with ContactsContract.PhoneLookup API but with this, it didnt work at all.

Code with ContactsContract.PhoneLookup API:

String[] projection = new String[]{ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.NUMBER, ContactsContract.PhoneLookup.TYPE};

Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(charSequence.toString()));

Cursor cursor = context.getContentResolver().query(uri, projection, null, null, ContactsContract.PhoneLookup.DISPLAY_NAME + " ASC");

Any help is appreciated.

而不是使用ContactsContract.CommonDataKinds.Phone.NUMBER ,请使用ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER (此返回E164格式的电话号码)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM