2013-02-06 130 views
0

經過大量搜索後,我發現我們如何在android中添加聯繫人程序。但我在添加聯繫人時遇到了一個小問題。這我使用代碼:在android中添加聯繫人編程

ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>(); 
    op_list.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) 
     .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "Phone") 
     .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "null") 
     //.withValue(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_DEFAULT) 
     .build()); 

// first and last names 
     op_list.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
    .withValueBackReference(Data.RAW_CONTACT_ID, 0) 
     .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE) 
     .withValue(StructuredName.GIVEN_NAME, name) 
     .withValue(StructuredName.FAMILY_NAME, name) 
     .build()); 

     op_list.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
       .withValueBackReference(Data.RAW_CONTACT_ID, 0) 
       .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 
       .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, mobileNo) 
       .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) 
       .build()); 
     op_list.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
       .withValueBackReference(Data.RAW_CONTACT_ID, 0) 

     .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) 
     .withValue(ContactsContract.CommonDataKinds.Email.DATA, emailAddr) 
     .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME) 
     .build()); 

try{ 
    ContentProviderResult[] results = getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list); 
    Toast.makeText(getApplicationContext(), "Contact Added Sucessfully..", Toast.LENGTH_LONG).show(); 
}catch(Exception e){ 
    Toast.makeText(getApplicationContext(), "Problem in Adding Contact ..", Toast.LENGTH_LONG).show(); 

    e.printStackTrace(); 
} 

當我的代碼運行時我得到了以下異常:

java.lang.IllegalArgumentException: Must specify both or neither of ACCOUNT_NAME and ACCOUNT_TYPE; URI: content://com.android.contacts/raw_contacts, 

誰能告訴我的這個解決方案。我在此帳戶類型和帳戶名稱中指定的內容。

回答

1

它爲我工作

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
    int rawContactInsertIndex = ops.size(); 

    Log.i("Line38", "Here"); 
     ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) 
        .withValue(RawContacts.ACCOUNT_TYPE, AccountManager.KEY_ACCOUNT_TYPE)   
        .withValue(RawContacts.ACCOUNT_NAME, AccountManager.KEY_ACCOUNT_NAME)   
        .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, "u232786seee") 
        .withValue(StructuredName.IN_VISIBLE_GROUP,true) 
        .build()); 

    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex) 
    .withValue(ContactsContract.Data.MIMETYPE, 
      ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 
    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,"23232343434") 
    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, "4343") 
    .build()); 

    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex) 
    .withValue(ContactsContract.Data.MIMETYPE, 
      ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) 
    .withValue(ContactsContract.CommonDataKinds.Email.DATA, "") 
    .withValue(ContactsContract.CommonDataKinds.Email.TYPE, "") 
    .build()); 

    //Log.i("Line43", Data.CONTENT_URI.toString()+" - "+rawContactInsertIndex); 

    try { 
      getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
    } catch (RemoteException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } catch (OperationApplicationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } 
+0

它爲我工作。 –

+0

好的..但最後謝謝。 –

+2

@GauravKumawat'ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) .withValue(RawContacts.ACCOUNT_TYPE,AccountManager.KEY_ACCOUNT_TYPE) .withValue(RawContacts.ACCOUNT_NAME,AccountManager.KEY_ACCOUNT_NAME) .build());' 你正在將此聯繫人添加到類型爲「accountType」的帳戶(AccountManager.KEY_ACCOUNT_NAME)我認爲這不是一個好的解決方案。最好使用'null' – sinisha

相關問題