Joel
Joel

Reputation: 1321

On Android, what is the correct way to open add new contact with a prefilled contact sheet?

I'd like to open the "save new contact" dialog with the sheet prefilled with street addess, phone number, email etc. Then the user can modify, add to or save.

I've seen a few methods.

1) this method is deprecated, but works for the most part (can't get to work with compound types like address).

Intent i = new Intent(Intent.ACTION_INSERT_OR_EDIT);
                        i.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
                        i.putExtra(Insert.NAME,"Name");
                        i.putExtra(Insert.PHONE,"Number");
                        startActivity(i);

2) This method seems like a good solution, but I can't attach this data to an intent to open the contacts app. It is only for programatically inserting the new contact automatically.

ArrayList<ContentProviderOperation> op=new ArrayList<ContentProviderOperation>();
                    /*  ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                    .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, mSelectedAccount.getType()) 
                    .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, mSelectedAccount.getName()) 
            .build()); */
     op.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
             .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
             .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
             .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
             .build());
     op.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
             .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
             .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
             .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,phone).build());

            getContentResolver().applyBatch(ContactsContract.AUTHORITY, op);

What is the best practice to solve this issue?

thank you.

Upvotes: 3

Views: 1276

Answers (1)

Xaver Kapeller
Xaver Kapeller

Reputation: 49837

The problem is you are using the wrong constants. The Insert.NAME which you are using is from the class

Contacts.Intents.Insert

which was deprecated in API Level 5. It was replaced with the class

ContactsContract.Intents.Insert

If you use the constants from this class you should be fine! I have updated your code accordingly:

Intent i = new Intent(Intent.ACTION_INSERT_OR_EDIT);
                        i.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
                        i.putExtra(ContactsContract.Intents.Insert.NAME,"Name");
                        i.putExtra(ContactsContract.Intents.Insert.PHONE,"Number");
                        startActivity(i);

You can find documentation for the ContactsContract.Intents.Insert class here and some general information about modifying contacts using intents here

Upvotes: 3

Related Questions