Contacts and Email
- Introduction
- The Android system maintains contacts in three tables
- A master "Contact" table that aggregates raw contacts.
Each row contains information for one person.
- "Raw Contacts" tables in which each row contains the information for one person
from one source.
- "Data" tables that hold the raw data.
- See the
details here
- Contact information can be obtained through content providers.
- Some of the common data kinds are Phone, Email, Photo, Website.
- Querying the Contacts
- Add permission to use contacts, in the manifest
<uses-permission android:name="android.permission.READ_CONTACTS"/>
and request permission from the user.
- The ContactsContract.CommonDataKinds.Email.CONTENT_URI can be queried directly,
to get a cursor into a table of email contacts.
- An ActivityResultLauncher with ActivityResultContracts.PickContact
starts the standard contacts app, and returns a Uri for a table of results.
The Uri can be queried to get a cursor into the table to extract some information ...
but not everything.
- A contact's id can be used when querying the
ContactsContract.CommonDataKinds.Email.CONTENT_URI to get what you need.
- Phone numbers are in the URI ContactsContract.CommonDataKinds.Phone
- Sending Data (e.g., Email) to a Contact
- An Intent with action Intent.ACTION_SEND is used to send data.
- Various extras can be added to specify email address, attachments, etc.
You can use ...
- Intent.EXTRA_SUBJECT to put a subject in the email.
- Intent.EXTRA_TEXT to put some text in the email.
- Intent.EXTRA_STREAM to put the content of a Uri in the email.
- Example:
MainActivity.java
activity_main.xml
strings.xml
AndroidManifest.xml