2012-11-22 4 views
0

연락처를 그룹에 삽입해야합니다. 나는이 그룹의 이드를 가지고있다. 그러나 나는 할 수 없다. 이유를 모르겠다.그룹 내부에 연락처 추가

아무도 도와 줄 수 있습니까?

여기 내 코드입니다.

Builder builder = ContentProviderOperation.newInsert(Data.CONTENT_URI); 
builder.withValueBackReference(Data.RAW_CONTACT_ID, 0); 
builder.withValue(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE); 
builder.withValue(GroupMembership.GROUP_ROW_ID, groupId); 

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
ops.add(builder.build()); 

try { 
    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
} catch (Exception e) { 
    Log.e("ContactsManager", "Failed to apply batch: "+e); 
} 
이이 ID를 참조하여 그룹에 연락처를 삽입
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
Account[] accounts = AccountManager.get(getActivity()).getAccounts(); 
String accountName = null; 
String accountType = null; 
String account = "[email protected]"; 
    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)}) 
     .withExpectedCount(1) 
     .build()); 

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

try{ 
    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
}catch(Exception e){ 
    Log.e("ERROR", e.toString()); 
} 

답변