How to use Data.HAS_PHONE_NUMBER

I am a bit confused about how to use Data.HAS_PHONE_NUMBER. I have inserted contacts along with some additional details and I would like to get some of these contacts.

If I run the following code everything is fine:

Uri uri = Data.CONTENT_URI;
String[] projection = new String[]{Data.CONTACT_ID, Data.DISPLAY_NAME };
String selection = Data.DATA4+" = ?";
String[] selectionArgs = new String[] {MY_VALUE};

Cursor truContactCursor = mContext.getContentResolver().query(uri, projection, selection, selectionArgs, null);


Now I would like to know if these contacts have a phone number, so I added HAS_PHONE_NUMBER to the projection like this:

String[] projection = new String[]{Data.CONTACT_ID, Data.DISPLAY_NAME, Data.HAS_PHONE_NUMBER };


When executing the request, I got the following execution:

java.lang.IllegalArgumentException: Invalid column has_phone_number


Why can't I access HAS_PHONE_NUMBER? No problem with Data.DISPLAY_NAME being in the same table. How can we use Data.HAS_PHONE_NUMBER ??

thanks for your reply


source to share

3 answers

The use of HAS_PHONE_NUMBER is used to check if a contact has at least one phone number. Example:

ContentResolver cr = getContentResolver();  
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));id
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//Query phone here. 


or add to the projection:

String [] projection = {ContactsContract.Contacts._ID,




Add "Data.MIMETYPE = ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE" to the select statement.

The result cursor will only contain lines with a phone number.

Council . There is a case: several numbers in one contact. If you want all numbers to be requested, add "ContactsContract.Data.DATA1" (which stands for phone number) to the forecast statement.



I faced this same problem and the only link to the problem I found is another stackoverflow question .

I am planning to write my own ContentProvider

to do the "connection" programmatically. That is, make the original query against Data.CONTENT_URI

, but no projection Data.HAS_PHONE_NUMBER

, and then query the given pin for the actual value Data.HAS_PHONE_NUMBER

. This certainly does not have the performance of an actual DB connection (this is an n + 1 type choice), but will provide the desired / expected functionality. This additional processing can be OS layer driven to maintain efficiency when OS versions function correctly.

Given the legalistic answer above and my own experience, in some versions of Android it will seem like a bug that this implicit connection will fail. My Android phone v2.3.7 crashes with the same error you get when Data.HAS_PHONE_NUMBER

in projection, whereas my Android tablet v4.2.2 doesn't crash.

I have introduced an Android bug regarding the issue:




All Articles