How to add app connections to an existing contact

2019-04-12 21:58发布

I want my android app's connection to be added in an existing contact. I am sending my all contacts of Phonebook to server(with Name,Phone Number, and Contact ID) to check which contacts are registered for my app. I will send back to client app the contact IDs which are matched.

Now I want to add connection to those contacts in my Phonebook based on those returned IDs.

How can I add app connection by editing the contact based on one of returned contact ID?

Thanks

1条回答
孤傲高冷的网名
2楼-- · 2019-04-12 22:25

Try with this code, it will give you desired output

public static void addContact(Context context, MyContact contact) {
    ContentResolver resolver = context.getContentResolver();
     // add condition that you want to check
    String where= RawContacts.ACCOUNT_TYPE + " = ? AND " +RawContacts.DISPLAY_NAME_PRIMARY+"=?";
    //values of that condotion
    String[] value=new String[] { AccountGeneral.ACCOUNT_TYPE ,contact.name};
    resolver.delete(RawContacts.CONTENT_URI, where, value);

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

    ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(RawContacts.CONTENT_URI, true))
            .withValue(RawContacts.ACCOUNT_NAME, AccountGeneral.ACCOUNT_NAME)
            .withValue(RawContacts.ACCOUNT_TYPE, AccountGeneral.ACCOUNT_TYPE)
            //.withValue(RawContacts.SOURCE_ID, 12345)
            //.withValue(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_DISABLED)
            .build());

    ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(Settings.CONTENT_URI, true))
            .withValue(RawContacts.ACCOUNT_NAME, AccountGeneral.ACCOUNT_NAME)
            .withValue(RawContacts.ACCOUNT_TYPE, AccountGeneral.ACCOUNT_TYPE)
            .withValue(Settings.UNGROUPED_VISIBLE, 1)
            .build());

    ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true)) 
            .withValueBackReference(Data.RAW_CONTACT_ID, 0) 
            .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE) 
            .withValue(StructuredName.GIVEN_NAME, contact.name) 
            .withValue(StructuredName.FAMILY_NAME, contact.lastName) 
            .build()); 

    ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true)) 
            .withValueBackReference(Data.RAW_CONTACT_ID, 0) 
            .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "12342145")

            .build());


    ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true)) 
             .withValueBackReference(Data.RAW_CONTACT_ID, 0)
             .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
             .withValue(ContactsContract.CommonDataKinds.Email.DATA, "sample@email.com")
             .build());     


    ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
            .withValueBackReference(Data.RAW_CONTACT_ID, 0)
            .withValue(Data.MIMETYPE, MIMETYPE)
            .withValue(Data.DATA1, 12345)
            .withValue(Data.DATA2, "sample")
            .withValue(Data.DATA3, "sample")
            .build());
    try {
        ContentProviderResult[] results = resolver.applyBatch(ContactsContract.AUTHORITY, ops);
        i++;
        if (results.length == 0)
            ;
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
查看更多
登录 后发表回答