Frank Cheng
Frank Cheng

Reputation: 6186

Can`t insert a new record into contacts

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();
        }

Upvotes: 0

Views: 451

Answers (2)

Nibha Jain
Nibha Jain

Reputation: 8141

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);

Upvotes: 1

Khawar
Khawar

Reputation: 5227

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.

Upvotes: 1

Related Questions