1

I'm trying to open contacts and let user decide if he/she want to create or select a contact to use it on my app.

I need something like this: I need something like this

Where on the first option the user can add a new contact.

I tried the following code:

Intent i = new Intent(Intent.ACTION_INSERT_OR_EDIT);
i.setType(ContactsContract.Contacts.CONTENT_TYPE);
i.putExtra("finishActivityOnSaveCompleted", true); // Fix for 4.0.3 +
startActivityForResult(i, CARREGA_CONTATOS);

But a got this error on Logcat (right after startActivityForResult):

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.INSERT_OR_EDIT typ=vnd.android.cursor.dir/contact (has extras) }

What I'm doing wrong?

EDIT:

I tried this code also with similar result:

Intent intent = new Intent(
    Intent.ACTION_INSERT_OR_EDIT,
    ContactsContract.Contacts.CONTENT_URI
);
startActivityForResult(intent, CARREGA_CONTATOS);

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.INSERT_OR_EDIT dat=content://com.android.contacts/contacts }

EDIT 2: Almost what I need:

Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
i.putExtra("finishActivityOnSaveCompleted", true); // Fix for 4.0.3 +
startActivityForResult(i, CARREGA_CONTATOS);

But this code opens this image, and the user must select "Contacts" before it goes to where I want. But this code opens this image

Antonio Carelli
  • 53
  • 1
  • 2
  • 9

3 Answers3

1

Did you try this: https://stackoverflow.com/a/12722921/4029129

Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
startActivity(intent);
Community
  • 1
  • 1
G_C
  • 26
  • 2
0

The documentation seems to suggest using:

// 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);
miki
  • 695
  • 3
  • 8
  • That code goes directly to **"New Contact"**. I need an option to let user select from an existing contact OR create a new one. – Antonio Carelli May 01 '15 at 16:05
  • Have you tried it? The doc says it handles that for you. – miki May 01 '15 at 16:09
  • Yes, i tried this... This [documentation](https://developer.android.com/training/contacts-provider/modify-data.html#EditContact) just edit an existing contact. I want to SELECT one or create a new. On my app I just need the name of that contact... – Antonio Carelli May 01 '15 at 16:27
  • Then I think you'll have to do that yourself. Show a pop up where they do the selection, and based on that fire off one intent (retrieve the list and select) or the other (create new). – miki May 01 '15 at 16:31
  • I'm avoiding this approach because the user might not know if the contact is already registered – Antonio Carelli May 01 '15 at 16:45
  • Then you might be stuck having to build a complete UI yourself. – miki May 01 '15 at 16:50
0

Usually you have to add the default category intent.addCategory(Intent.CATEGORY_DEFAULT) to make it match an an action.

dangVarmit
  • 5,641
  • 2
  • 22
  • 24
  • I tried this code `Intent intent = new Intent( Intent.ACTION_INSERT_OR_EDIT, ContactsContract.Contacts.CONTENT_URI ); intent.putExtra("finishActivityOnSaveCompleted", true); // Fix for 4.0.3 + intent.addCategory(Intent.CATEGORY_DEFAULT); startActivityForResult(intent, CARREGA_CONTATOS);` but does not work – Antonio Carelli May 01 '15 at 16:36