Enter contact data from the app

Asked

Viewed 248 times

0

I would like to know if it is possible to insert some data in the phone contact, for example e-mail. Exemplifying: When selecting a contact, from the application, when returning to Activity, is asked to enter the email and this stay recorded in the contact, from the phone.

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

Thank you.

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 have to pass Contact_id as a parameter to know that it is an edit, if it is an inclusion, it will go with 0.

  • if (contentProviderResult[0].Uri == null) { what I did is not required, in my case I treated it to see if there was success in the insertion.

  • The example above are some tips, email, you can study this excerpt: ops.add(Contentprovideroperation . newInsert(ContactsContract.Data.CONTENT_URI)&#xA; . 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());

  • Carlos, thank you. I will study yes and as soon as I do, I mark as an answer or I put my doubts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.