0

I am currently working on an app, where I need to insert contacts into the mobilephones contact list. Using the ContactsContract.RawContacts batch insertion, I successfully inserted contacts into the phonebook.

http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html

However when trying to update them they just add more phonenumber, instead replacing the old one.

So I came onto ContactsContract.Data where through the Contact_ID one could apparently on update replace the old phonenumber.

http://developer.android.com/reference/android/provider/ContactsContract.Data.html

My question now is, is it possible to retrieve the Contacts_ID on insertion? So that when I insert a contact by this code:

ArrayList<ContentProviderOperation> ops =
          new ArrayList<ContentProviderOperation>();
 ...
 int rawContactInsertIndex = ops.size();
 ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
          .withValue(RawContacts.ACCOUNT_TYPE, accountType)
          .withValue(RawContacts.ACCOUNT_NAME, accountName)
          .build());

 ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
          .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
          .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
          .withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
          .build());

 getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

I would get back the Contacts_ID?

Thank you advance for any help you can give me.

Max
  • 572
  • 2
  • 6
  • 23

2 Answers2

0

Sure you can! Here it is:

ContentProviderResult[] res;
try
{
    res = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    if (res != null && res[0] != null)
    {
        String uri = res[0].uri.getPath().substring(14);
        int contact_id = new Integer(uri).intValue());
    }
}
catch (RemoteException e)
{
    e.printStackTrace();
}
catch (OperationApplicationException e)
{
    e.printStackTrace();
}
Manitoba
  • 8,522
  • 11
  • 60
  • 122
  • 1
    Thanks. Well I think it is not the Contact_ID but the RawContact_ID, however this should solve my problem. I will keep you posted. Thanks again. – Max Mar 27 '12 at 14:57
  • So tried it and found that for some reason the string uri is not content://com.android.contacts/raw_contacts/612 but /raw_contacts/612. Which means substring has to be 14 not 44, so substring(14). Thanks again for your help. – Max Mar 27 '12 at 15:37
  • I do have follow up question when I delete then contact entry it leaves me with contact called "Unknown". Do you know how I could get rid of that contact? Has it to do something with deleting through the RawContact_ID instead of the Contact_ID? And if yes do you have maybe a code to get the Contact_ID instead of the RawContact_ID? ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI) .withSelection(Data.RAW_CONTACT_ID +"=?", new String[]{RawContact_ID}) .build()); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); – Max Mar 27 '12 at 16:02
  • 1
    int contact_id=Integer.parseInt(res[0].uri.getLastPathSegment()); – Hafthor Oct 10 '12 at 19:43
0

Better way, and you get the _ID not the RAW_ID!!

Add that as static variables:

static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
    ContactsContract.Contacts._ID,
    ContactsContract.Contacts.DISPLAY_NAME,
    ContactsContract.Contacts.STARRED,
    ContactsContract.Contacts.TIMES_CONTACTED,
    ContactsContract.Contacts.CONTACT_PRESENCE,
    ContactsContract.Contacts.PHOTO_ID,
    ContactsContract.Contacts.LOOKUP_KEY,
    ContactsContract.Contacts.HAS_PHONE_NUMBER,
};

Then add that after the insert:

// Displayed name
String displayed_name = "Contact's name goes here";

// Add the new contact
try
{
    context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e)
{
    e.printStackTrace();
}

String select = "(" + ContactsContract.Contacts.DISPLAY_NAME + " == \"" +displayed_name+ "\" )";
Cursor c = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
context.startManagingCursor(c);

if (c.moveToNext())
{
    int id = new Integer(c.getString(0)).intValue();
}

It works for me.

Manitoba
  • 8,522
  • 11
  • 60
  • 122
  • Thank you, it works for me aswell. You just solved for me a big problem – Max Mar 28 '12 at 12:38
  • No problem. Maybe you could give me a hand: http://stackoverflow.com/questions/9907751/android-update-a-contact – Manitoba Mar 28 '12 at 12:44