We can start by accessing a particular resource of the system, in this case, the contacts, using a URI. A URI (Uniform Resource Identifier) is a short string of characters that identifies a resource in a unique way.
URIs have a particular structure, in our case, all URIs begin with the string content://. We can create URIs by writing them by ourselves (Knowing what we are writing) or we can use the constants that Android provides for every resource.
URIs Example:
"content://sms/inbox"
This URI is used to get the SMS that are in the inbox.
URI constants Examples:
Contacts.People.CONTENT_URI
Calendar.CONTENT_URI
The first one provides us the URI of the “people” in the phone via the the phone book. The second one provides us with the information found via the Google Android calendar.
So, lets create our URI object in Android:
Uri myContactsURI = Contacts.People.CONTENT_URI;
Remember that, internally, we are working with databases. In all Android databases we can find a field called “ID”, this is the unique identifier for the row in that table. In our case, every contact will have an unique number and this number will be used in other tables to relate them with that particular contact. So, for example if we get the information of the contact “James”, in our device, we can get its ID. With this ID we can create other query to retrieve, for example, the messages that this person has written to you, because this ID is present in the “SMS” table and both tables are related.
Suppose now, that we have that ID, we have the contact ID (How to get this ID will be shown in next part), and imagine, that you want to create a URI, with this particular information, an URI just to our contact James. This can be done with:
Uri myContactURI = Uri.withAppendedPath(Contacts.People.CONTENT_URI, userID);