0

多くの検索の後、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,

このアカウント タイプとアカウント名で何を指定しますか?

4

1 に答える 1

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();
    }
于 2013-02-06T10:17:00.083 に答える