How-to use Basic Content Providers Within Our Android Application Programming Environment
What information can we get from Content Providers? We can find the phone book, SMS, and the call log between others. In this article we are going to focus on the most used and simplest ones: The phone contacts.
Before beginning with code examples, lets refresh our memory. As I explained in my articles focusing on “How-to work with Android databases", Android uses internal databases: SQLite 3. So, all information can be stored and retrieved from this database and its tables. If you are curious about how data is stored, you can check it out byretrieving this databases from the emulator and opening them with any of the open source SQLite 3 database management programs. However, for now we are going to work with “contacts.db” (This is in a low level of the architecture, you can avoid this information if you have enough experience via Android).
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.
This URI is used to get the SMS that are in the inbox.
URI constants Examples:
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);
Cursors and Queries
Once we know which URI are we going to use, its time to create the query. For this Android give us the opportunity to work in a more high level structure, forgetting all SQL staff. To create queries we have to create Cursors, This “Cursors” will contain the tables information in a array-form (internally) where we can move trough it.
Cursor cursor = managedQuery(myContactURI, null, null, null, null);
Most important parameters of managedQuery:
The first parameter is the URI we have created before.
Second parameters is an String Array, where we set the columns we want to retrieve from the table (null if we want all columns)
Third parameter is a String, where we can insert in the query a “Where-like-sql”. This will be useful in cases like this: “Get the contact with the name ‘Jose’ “, for this, the String to insert here would be something like: “People.DISPLAY_NAME = Jose_”_
Once we have created the cursor and is not null, we can move through it and get the information.
int nameColumn =cursor.getColumnIndex(People.DISPLAY_NAME);
int phoneColumn = cursor.getColumnIndex(People.NUMBER);
String nameValue = cursor.getString(nameColumn);
String phoneNumberValue = cursor.getString(phoneColumn);
} while (cursor.moveToNext());
First we get the column number with getColumnIndex, then, with this column number we get the information inside that column. We will do this until we arrive to the last position in the cursor. In this case we get the name and the phone number of the contacts.
If you want to know when new articles are released, subscribe yourself to the Google Android RSS Otherwise, you can follow my research, articles and work in my professional twitter: jbeerdev
This post is part of the series: How to- Develop Applications on Android: More Functionality
This time we will learn more advanced functionality to add to our Android Applications.