3

I'm making an application in which user can add users from the application into his contacts.

Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.PHONE, bean.getMobileNo());
intent.putExtra(ContactsContract.Intents.Insert.NAME, bean.getName());
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, bean.getEmailID());
startActivity(intent);

so adding it is not the issue the issue when it goes to add contact screen and if the user presses back button the contact is getting saved even if the user doesn't want to save the contact.

I want to do this using Intent only not through app. Is there any solution for this or is it device specific?

Harshad Pawar
  • 56
  • 1
  • 8
  • @Shailesh The requirement for adding the contact is from intent, I can add contact from the app – Harshad Pawar May 09 '16 at 11:07
  • 1
    your code is right ,no problem in code some device default contact app did this when you press back button and some app ask you for discard or save. – Bhavin Patel Apr 26 '17 at 10:05

3 Answers3

5

Try this

/** * Open the add-contact screen with pre-filled info */

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

intent.putExtra(ContactsContract.Intents.Insert.NAME, bean.getName());
intent.putExtra(ContactsContract.Intents.Insert.PHONE, bean.getMobileNo());
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, bean.getEmailID());

context.startActivity(intent);
Shailesh
  • 442
  • 7
  • 14
1

Try this,

// Declare
static final int PICK_CONTACT = 1;

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

startActivityForResult(intent, PICK_CONTACT);

//code 
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode) {
        case (PICK_CONTACT):
            if (resultCode == Activity.RESULT_OK) {

                Uri contactData = data.getData();
                Cursor c = managedQuery(contactData, null, null, null, null);
                if (c.moveToFirst()) {
                    String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                    String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                    if (hasPhone.equalsIgnoreCase("1")) {
                        Cursor phones = getContentResolver().query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
                                null, null);
                        phones.moveToFirst();
                        cNumber = phones.getString(phones.getColumnIndex("data1"));
                        System.out.println("number is:" + cNumber);
                    }
                    String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


                }
            }
            break;
    }
}

This may helps you.

Dhruv
  • 1,801
  • 1
  • 15
  • 27
Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
0

If I understand your post properly, the issue is that regardless if the user wants to add a contact or not, the contact information gets added anyway.

As I noticed, you are already calling startActivity(intent), which saves the contact. Only run startActivity(intent) if the user has already approved/verified that he wants to add the contact. I suggest using a Dialog for the confirmation. If the user accepts, run startActivity(intent), if not, then simply disregard the action. Like so:

        if (true){ // user accepts
            Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
            intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
            intent.putExtra(ContactsContract.Intents.Insert.PHONE, bean.getMobileNo());
            intent.putExtra(ContactsContract.Intents.Insert.NAME, bean.getName());
            intent.putExtra(ContactsContract.Intents.Insert.EMAIL, bean.getEmailID());
            startActivity(intent);
        } else {
            // your code here.
        }

I think this post might also be useful for you. Cheers! :D

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • Yes that can be a case.. But in phones when i press back it gives a confirmation to discard and in some phones it directly saves the contact. – Harshad Pawar May 09 '16 at 16:58
  • That is odd. Even if you use the post that I link? Using a `ContentResolver` ? – AL. May 09 '16 at 23:09