Insert a new contact intent

For one of my apps, I need the user to select one of his existing contacts or to create a new one. Picking one is clearly easy to do with the following code:

i = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(i, PICK_CONTACT_REQUEST );

Now I want to create a new contact. I tried to use that code but it doesn't trigger the activity result:

i = new Intent(Intent.ACTION_INSERT);
i.setType(Contacts.CONTENT_TYPE);
startActivityForResult(i, PICK_CONTACT_REQUEST);

The above code will start the contact adding form. Then when I validate it, it just asks me to open the contact list and the onActivityResult method is never triggered.

Could you help me to make it working ?

I read on some boards that this wasn't possible, and I had to create my own contact adding form. Could you confirm that ?

EDIT: Problem solved. Check my answer.


Solution 1:

You can choose whether you want to add the contact automatically, or open the add contact activity with pre-filled data:

/**
 * Open the add-contact screen with pre-filled info
 * 
 * @param context
 *            Activity context
 * @param person
 *            {@link Person} to add to contacts list
 */
public static void addAsContactConfirmed(final Context context, final Person person) {

    Intent intent = new Intent(Intent.ACTION_INSERT);
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);

    intent.putExtra(ContactsContract.Intents.Insert.NAME, person.name);
    intent.putExtra(ContactsContract.Intents.Insert.PHONE, person.mobile);
    intent.putExtra(ContactsContract.Intents.Insert.EMAIL, person.email);

    context.startActivity(intent);

}

/**
 * Automatically add a contact into someone's contacts list
 * 
 * @param context
 *            Activity context
 * @param person
 *            {@link Person} to add to contacts list
 */
public static void addAsContactAutomatic(final Context context, final Person person) {
    String displayName = person.name;
    String mobileNumber = person.mobile;
    String email = person.email;

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());

    // Names
    if (displayName != null) {
        ops.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,
                        displayName).build());
    }

    // Mobile Number
    if (mobileNumber != null) {
        ops.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, mobileNumber)
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                        ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build());
    }

    // Email
    if (email != null) {
        ops.add(ContentProviderOperation
                .newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
                .withValue(ContactsContract.CommonDataKinds.Email.TYPE,
                        ContactsContract.CommonDataKinds.Email.TYPE_WORK).build());
    }

    // Asking the Contact provider to create a new contact
    try {
        context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Toast.makeText(context, "Contact " + displayName + " added.", Toast.LENGTH_SHORT)
            .show();
}

Solution 2:

Finally found a solution, I'm sharing it with you. That's only a fix for Android version above 4.0.3 and sup. It doesn't work on 4.0 to 4.0.2.

i = new Intent(Intent.ACTION_INSERT);
i.setType(Contacts.CONTENT_TYPE);
if (Integer.valueOf(Build.VERSION.SDK) > 14)
    i.putExtra("finishActivityOnSaveCompleted", true); // Fix for 4.0.3 +
startActivityForResult(i, PICK_CONTACT_REQUEST);

Solution 3:

Intent intent = new Intent(
        ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,
        Uri.parse("tel:" + phoneNumber));
    intent.putExtra(ContactsContract.Intents.EXTRA_FORCE_CREATE, true);
    startActivity(intent);

this code might help you.

Solution 4:

 // Creates a new Intent to insert a contact

Intent intent = new Intent(Intents.Insert.ACTION);
 // Sets the MIME type to match the Contacts Provider

intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);

If you already have details for the contact, such as a phone number or email address, you can insert them into the intent as extended data. For a key value, use the appropriate constant from Intents.Insert. The contacts app displays the data in its insert screen, allowing users to make further edits and additions.

private EditText mEmailAddress = (EditText) findViewById(R.id.email);
private EditText mPhoneNumber = (EditText) findViewById(R.id.phone);

/*
 * Inserts new data into the Intent. This data is passed to the
 * contacts app's Insert screen
 */
 // Inserts an email address

 intent.putExtra(Intents.Insert.EMAIL, mEmailAddress.getText())
 /*
  * In this example, sets the email type to be a work email.
  * You can set other email types as necessary.
  */
  .putExtra(Intents.Insert.EMAIL_TYPE, CommonDataKinds.Email.TYPE_WORK)
 // Inserts a phone number
  .putExtra(Intents.Insert.PHONE, mPhoneNumber.getText())
 /*
  * In this example, sets the phone type to be a work phone.
  * You can set other phone types as necessary.
  */
  .putExtra(Intents.Insert.PHONE_TYPE, Phone.TYPE_WORK);

Once you've created the Intent, send it by calling startActivity().

/* Sends the Intent
 */
startActivity(intent);

Note : import "intents" of "ContactsContract"

Solution 5:

Try if you use Kotlin

fun Fragment.saveContact(name: String?, phone: String?) {
    if (name != null && phone != null) {
        val addContactIntent = Intent(Intent.ACTION_INSERT)
        addContactIntent.type = ContactsContract.Contacts.CONTENT_TYPE
        addContactIntent.putExtra(ContactsContract.Intents.Insert.NAME, name)
        addContactIntent.putExtra(ContactsContract.Intents.Insert.PHONE, phone)
        startActivity(addContactIntent)
    }
}