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.
Instead of using ContactsContract.CommonDataKinds.Phone.NUMBER, use ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER (this return phone numbers in E164 format)
Related
I have a requirement when I need to get the Contact name of the selected numbers from the ANDROID_CONTACTS.
I can get the name of a particular number using ->
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
if (cursor == null) {
return;
}
How can I pass a list of numbers and get their respective names?
Here's the naive approach:
String[] phones = new String[] { "(212) 555-1111", "(212) 555-2222", "(212) 555-3333"};
String selection = Phone.NUMBER + "IN (?,?,?)";
String[] selectionArgs = phones;
String[] projection = new String[] { Phone.NUMBER, Phone.CONTACT_ID, Phone.DISPLAY_NAME };
Cursor cur = getContentResolver().query(Phone.CONTENT_URI, projection, selection, selectionArgs, null);
DatabaseUtils.dumpCursor(cur); // dumps the cursor to logcat
However, this will only work if you enter the phone number in exactly the same format as it is stored in the ContactsContract DB under NUMBER.
The nice thing about the PhoneLookup.CONTENT_FILTER_URI API you've used is that the format of the number doesn't matter, it'll find it in the DB in any format you use.
To replicate something like that, you can try using another field called NORMALIZED_NUMBER which should always hold the number in a specific format called E164.
So you can run a query like this:
String[] e164_phones = new String[] { "+12125551111", "+12125552222", "+12125553333"};
String selection = Phone.NUMBER + "IN (?,?,?)";
String[] selectionArgs = e164_phones; // all phones here must be in e164 format
String[] projection = new String[] { Phone.NUMBER, Phone.CONTACT_ID, Phone.DISPLAY_NAME };
Cursor cur = getContentResolver().query(Phone.CONTENT_URI, projection, selection, selectionArgs, null);
You'll probably need a way to convert a phone number to E164 format for this code, check out this answer then.
I'm trying do retrieve a list of contacts in phonebook and SIM, excluding contacts from skype, google, facebook and so on. I use the following code, but it works only on Samsung devices, I think because "vnd.sec.contact.phone" and "vnd.sec.contact.sim" used as selection arguments for ACCOUNT_TYPE field are both vendor related. How rewrite this code in a more "generic" way?
private Cursor getContactsNew()
{
// Run query
Uri uri = ContactsContract.RawContacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.RawContacts._ID,
ContactsContract.RawContacts.CONTACT_ID,
ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY,
ContactsContract.RawContacts.ACCOUNT_TYPE
};
String selection = ContactsContract.RawContacts.ACCOUNT_TYPE + "= ? OR " + ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?";
String selectionArgs = new String[] { "vnd.sec.contact.phone", "vnd.sec.contact.sim" };
String sortOrder = ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY + " COLLATE LOCALIZED ASC";
ContentResolver cr = getContentResolver();
return cr.query(uri, projection, selection, selectionArgs, sortOrder);
}
This tutorial should help you out to fetch contacts from an android device :
http://www.c-sharpcorner.com/UploadFile/1e5156/fetch-contacts-from-android-phone-in-android-studio/
Lemme know if it helped out
I'm currently using the following to retrieve contacts from the phone:
ContentResolver cr = getContentResolver();
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] columnName = new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor contactsCursor = cr.query(uri, columnName, null, null, "display_name ASC");
Does anyone knows how to retrieve distinct phone numbers from Android phone?
Hi all I am trying to get contact information from the contact database using ContentResolver with these field want to get name, number, FORMATTED_ADDRESS, PHOTO
details for a contact in one single query.
So basically I need to make 3 queries per contact to obtain these
details.
What I want to know is that, is there a simpler and more efficient way
to achieve what this.
but using the below code i am getting exception.
java.lang.IllegalArgumentException: Invalid column data1
Can any body help me for finding the solution for the same.
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
ContactsContract.CommonDataKinds.Photo.PHOTO};
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '1'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor contacts = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
Maybe problem will be at selection. Replace yours method with mine.
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
ContactsContract.CommonDataKinds.Photo.PHOTO};
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = ?";
String[] selectionArgs = { String.valueOf(1) };
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor contacts = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
You should always use parametrized statements. Your approach is dangerous. And your URI was bad.
Here you go
Replace
ContactsContract.Contacts.CONTENT_URI;
with
ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
I'm trying to write a very basic app that reads from the contacts content provider and pulls the contacts from a specific group name. How do I set the selection arguments so that the contacts information is pulled from a specific group?
I apologize if this has been answered elsewhere. I have searched and failed to find a good example on how to set selectionArgs so sort through information.
private Cursor getContacts() {
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
String selection = ContactsContract.Contacts.STARRED + " = '"
+ ("1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs,
sortOrder);
}
Thanks for the help!