2

I am working on a application where i need to add my app link just like freecharge or whatspp in phone's existing contact profile without creating new.

I tried doing this with the following code

public static void addContactTag(Context context, String number) {


    ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

    // Create our RawContact
    ContentProviderOperation.Builder builder = ContentProviderOperation
            .newInsert(RawContacts.CONTENT_URI);
    builder.withValue(RawContacts.ACCOUNT_NAME, AccountGeneral.ACCOUNT_NAME);
    builder.withValue(RawContacts.ACCOUNT_TYPE, AccountGeneral.ACCOUNT_TYPE);
    operationList.add(builder.build());

    // Create a Data record of common type 'Phone' for our RawContact
    builder = ContentProviderOperation
            .newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(Data.RAW_CONTACT_ID, 0);
    builder.withValue(ContactsContract.Data.MIMETYPE,
            ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
    builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number);
    operationList.add(builder.build());

    // Create a Data record of custom type
    // "vnd.android.cursor.item/vnd.be.ourservice.profile" to display a link
    // to our profile
    builder = ContentProviderOperation
            .newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(Data.RAW_CONTACT_ID, 0);
    builder.withValue(ContactsContract.Data.MIMETYPE, MIMETYPE);
    builder.withValue(ContactsContract.Data.DATA1, number);
    builder.withValue(ContactsContract.Data.DATA3, "My app");
    operationList.add(builder.build());

    try {
        context.getContentResolver().applyBatch(ContactsContract.AUTHORITY,
                operationList);
        Log.i("addContact batch applied");
    } catch (Exception e) {
        Log.i("Something went wrong during creation! " + e);
        e.printStackTrace();
    }
}

Everything seems fine but its not updating existing contact rather its creating a new one .

Any help will be appreciated.

Thanks in advance.

Harry Sharma
  • 2,190
  • 2
  • 15
  • 41
Kushminder Garg
  • 518
  • 1
  • 6
  • 14

1 Answers1

-1

The code you mentioned here is meant to write a new contact.to edit a current contact you should do something like this.

       try {
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
   .withSelection(ContactsContract.CommonDataKinds.Phone._ID + "=? AND " +
           Data.MIMETYPE + "='" +
           ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'",
           new String[]{contact_id})
    .withValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, "anything")
    .build());

    ContentProviderResult[] result = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
    Log.w("UpdateContact", e.getMessage()+"");
    for(StackTraceElement ste : e.getStackTrace()) {
        Log.w("UpdateContact", "\t" + ste.toString());
    }
    Context ctx = getApplicationContext();
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(ctx, "Update failed", duration);
    toast.show();
}

If you haven't figured this out yet, give that a go. I found updating contacts to be very tricky in getting the selection arguments right.

Accept and mark it up if this helps. Happy coding :)

Harry Sharma
  • 2,190
  • 2
  • 15
  • 41