1

我需要在组内插入联系人。我有该组的ID。

联系人插入到帐户内,但不在指定的组内。我不知道为什么。任何人都可以帮助我吗?

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Account[] accounts = AccountManager.get(getActivity()).getAccounts();

String accountName = null;
String accountType = null;
String account = "mail@gmail.com";
    for(Account account2 : accounts)
        if(account2.name.equals(account)){
            accountName = account2.name;
            accountType = account2.type;
        }
ops.add(ContentProviderOperation
        .newInsert(ContactsContract.RawContacts.CONTENT_URI)
        .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
        .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName)
        .build());

ops.add(ContentProviderOperation
        .newAssertQuery(ContactsContract.Groups.CONTENT_URI)    
        .withSelection(ContactsContract.Groups._ID + " = ?", new String[]{Long.toString(idGroup)})   //new String[]{Long.toString(idGroup)}
        .withExpectedCount(1)
        .build());

ops.add(ContentProviderOperation
        .newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
        .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "RICARDITO bla bla2")
        .build());

try{
    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}catch(Exception e){
    Log.e(ClientEditContact.class.getName(), e.toString());
}
4

1 回答 1

0

Here is the code I use to insert a contact into a group. I use the group ID and the Contact ID:

   ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
                        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, groupId)
                        .build());

                try 
                {
                    resolver.applyBatch(ContactsContract.AUTHORITY, ops);
                } catch (RemoteException e) {

                } catch (OperationApplicationException e) {
                // TODO Auto-generated catch block
                    e.printStackTrace();
                }

I'm going to say that you need to add your contact in the ContactsContract.Data database by setting the rows ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID & and the CONTACT_ID and the correct mimetype, instead of adding directly to the ContactsContracts.Group database.

于 2013-05-21T19:09:42.730 回答