Add contact number on Android 2.0

I am trying to add a phone number to an existing contact on a Droid phone. Performing this process at the same time as creating the contact is trivial, because when I create the ContentProviderOperation I provide just 0. But trying to find the backlink using a query for a display name like this does not work:

Cursor rawContactsReferenceCursor = contentResolver.query(Data.CONTENT_URI, new String[]{Data.RAW_CONTACT_ID}, Data.DISPLAY_NAME+"=\""+displayName+"\"", null, null); 

While I get the raw contact ID, the following code simply throws an IndexOutOfBoundException (rawConcactReferenceID is a variable obtained from the previous request):

 ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>(); op_list.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) .withValueBackReference(Data.RAW_CONTACT_ID, rawConcactReferenceID) .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE) .withValue(Phone.NUMBER, testNumber) .withValue(Phone.TYPE, Phone.TYPE_CUSTOM) .withValue(Phone.LABEL, testLabel) .build()); ContentProviderResult[] result = contentResolver.applyBatch(ContactsContract.AUTHORITY, op_list); 

The big problem is the huge lack of good documentation. I would be very pleased if I just got work on working copies to study.

Greetings

+4
source share
3 answers

I have found the answer. It's not atomic if you want to add a few things at once, but hey, who needs stupid atomicity?

 ContentValues values = new ContentValues(); values.put(Data.RAW_CONTACT_ID, new Integer(contactId).intValue()); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(Phone.NUMBER, dataValue); values.put(Phone.TYPE, Phone.TYPE_CUSTOM); values.put(Phone.LABEL, customLabel); Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values); 
+4
source

I had a similar problem with email addresses. Here the solution I used was used:

 ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) .withValue(Data.RAW_CONTACT_ID, id) .withValue(Email.DATA, value) .withValue(Email.MIMETYPE, .Email.CONTENT_ITEM_TYPE) .withValue(Email.LABEL, label) .withValue(Email.TYPE, Email.TYPE_CUSTOM) .build()); ContentProviderResult[] res = cr.applyBatch(ContactsContract.AUTHORITY, ops); 

The same solution should work for phone numbers.

+2
source

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


All Articles