Getting a phone number using ContactsContract in Android - doesn't work

I wrote the following function to get one phone number that belongs to a contact with the identifier "contactID".

Function that is designed to retrieve a phone number:

private String getContactPhone(String contactID) { Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; String[] projection = null; String where = ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?"; String[] selectionArgs = new String[] { contactID }; String sortOrder = null; Cursor result = managedQuery(uri, projection, where, selectionArgs, sortOrder); if (result.moveToFirst()) { String phone = result.getString(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); if (phone == null) { result.close(); return null; } result.close(); return phone; } result.close(); return null; } 

How this function is called:

 ArrayList<Contact> resultContacts = new ArrayList<Contact>(); Cursor result = null; Uri uri = ContactsContract.Data.CONTENT_URI; String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Event.CONTACT_ID, ContactsContract.CommonDataKinds.Event.START_DATE, }; String where = ContactsContract.Data.MIMETYPE+" = ? AND "+ContactsContract.CommonDataKinds.Event.TYPE+" = "+ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY; String[] selectionArgs = new String[] {ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE}; String sortOrder = null; result = managedQuery(uri, projection, where, selectionArgs, sortOrder); while (result.moveToNext()) { Long id = result.getLong(result.getColumnIndex(ContactsContract.Contacts._ID)); String phone = getContactPhone(String.valueOf(id)); ... } ... 

Unfortunately, it does not work. I get null if I call this function with a value that I got from "ContactsContract.Contacts._ID". Why is this so? What's wrong?

Edit: I used the Contacts._ID card for CommonDataKinds.Phone.CONTACT_ID - this did not work. But now I map Contacts.DISPLAY_NAME to CommonDataKinds.Phone.DISPLAY_NAME, and it works all of a sudden - it's strange, isn't it? But I would prefer matching identifiers instead of display names. Therefore, the issue remains relevant. Could this be due to different identifiers in these tables? Doesn't that mean there are search identifiers?

+4
source share
3 answers

To get the contact id in the first part, you should use:

 ContactsContract.Data.CONTACT_ID 

instead:

 ContactsContract.Contacts._ID 

Thus, the projection should be:

 String[] projection = new String[] { ContactsContract.Data.CONTACT_ID, ContactsContract.CommonDataKinds.Event.CONTACT_ID, ContactsContract.CommonDataKinds.Event.START_DATE, }; 

And then, of course, get the correct line:

 Long id = result.getLong(result.getColumnIndex(ContactsContract.Data.CONTACT_ID)); 
+10
source

You get a null value because you specified a null projection. A projection is basically a list of columns that you want to return, for example.

 String[] projection = {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER}; 

Usually, when you find a contact, they may have a list of phone numbers, so you need to use a different cursor to iterate over the phone numbers, for example.

 Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); while (phones.moveToNext()) { phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)); } 

Hope this helps.

+1
source

Your code for getContactPhone () works fine on my part. I tested by opening a contact group, selecting a contact, then using the identifier that was returned and passed to your method.

Therefore, I suspect that you are really passing an invalid identifier. Can you post a full stack trace to exclude a null pointer?

Yes, search keys are available because _ID is not guaranteed to do the same, since synchronization and aggregation of contacts change them.

+1
source

Source: https://habr.com/ru/post/1389373/


All Articles