Enter contact data from the application

0

I wonder if you can enter some data in the phone contact, for example the email. Exemplifying: When you select a contact, from the application, when you return to activity, you are prompted to enter the email and it will be saved in the contact of the phone.

Is this possible? If yes, is there an example?

Thank you.

    
asked by anonymous 13.07.2016 / 03:32

1 answer

1

You can use the following approach:

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

        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.GIVEN_NAME,
                        edtNome.getEditText().getText().toString()).build());

        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.ADDRESS,
                        edtEmail.getEditText().getText().toString()).build());

        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,
                        edtTelefone.getEditText().getText().toString())
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                        ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build());

        try {
            ContentProviderResult[] contentProviderResult = contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
            if (contentProviderResult[0].uri == null) {
                throw new Exception("Erro ao incluir usuário");
            } else {
                new SnackBar.Builder(this)
                        .withMessage("Usuário incluído aos contatos com sucesso!")
                        .withStyle(SnackBar.Style.DEFAULT)
                        .withDuration((short) 3000)
                        .show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

You must pass the Contact_ID as a parameter to know that it is an issue, if it is included, it will be 0.

    
13.07.2016 / 13:01