0
votes

I can successfully insert a new record using People.CONTENT_URL according to http://developer.android.com/guide/topics/providers/content-providers.html#addingrecord. But the People class is deprecated, So i would like to using ContentProviderOperation and Data.CONTENT_URL to insert record. here is my code.

        super.onCreate(savedInstanceState);
        ArrayList operations = new ArrayList();
        operations.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValue(Phone.CONTACT_ID, "23").withValue(CommonDataKinds.Phone.NUMBER,
                        "13412341234123412341234").build());
        try {
            getContentResolver().applyBatch(ContactsContract.AUTHORITY, operations);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
2
I think i have done the same with it.Frank Cheng
i find the cause. when insert a new entry, i must first insert a new RawContacts using ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null) .build());.then use withValueBackReference to get the RawContact_idFrank Cheng

2 Answers

1
votes

According to my understanding, you simply want to add a new contact, right?

I have answered the question Here. I have used the same piece of code and it works for me.

1
votes

If you are trying to add a new contact try this code :

        Intent intent = new Intent(Intent.ACTION_INSERT); 
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);  
        intent.putExtra(ContactsContract.Intents.Insert.NAME, "name"); 
        intent.putExtra(ContactsContract.Intents.Insert.PHONE, "123456");  
        startActivity(intent);