Using a Content Resolver to access another app's database

  • Written by  Clive

Accessing the database from another app

Query app iconYou use a content provider to make your database accessible to other apps.

You use a content resolver to access a database in another app.

Our query app

In previous tutorials you created a database and a content resolver.

We’re now going to access that database using this simple demo app.

Start the content provider

Make sure that you run the app that registers the content provider. It initiates the content provider on start-up.

The content provider must be initialised for any app to be able to access the database.

The content resolver

Any app wanting to access data in another app must use a content resolver.

Our app has two files:


The Constants

The Constants file contains the relevant constants:

The Constants file code

The constants needed to access the database

The constants include the table name, database column titles and the Uri of the data.

The MainActivity

All the action happens in the MainActivity.

It displays a button which when pressed, displays the names and email addresses retrieved from the database.

Essentially, this is what happens in the MainActivity:

  • We empty the existing database
  • We load 5 rows of data into the database
  • Pressing a button gets all the names and corresponding email addresses out of the database and displays them in the LogCat

Delete all data

The Content Resolver delete method

Delete all the rows in the database

We get an instance of the content resolver.

Then we set the selection and selectionArguments parameters to null as we want to delete all the rows in the database.

We call delete(), passing the parameters. The first parameter creates a Uri address from the string constant, CONTENT_URI.

This content resolver delete() method is received by the Android system. The system uses the Uri to find out which content provider should handle the request. It then passes the request to the correct provider.

The content provider uses the Uri in its own delete() method to process the delete request. It deletes all the rows and informs the content resolver how many rows were deleted.

The number of rows deleted is displayed in the LogCat.

Load 5 rows of data into the database

Load the database records

Load 5 rows of data into the database

We get an instance of the content resolver.

Then we create a ContentValues object, values. This stores data in a key/value format.

Using the database column titles as the keys, we load the data that we want to save into the values object. (We get the data from a String array of names, email addresses and telephone numbers).

Then we call the content resolver’s insert() method and pass the parameters to it.

The content provider executes its corresponding insert() method, inserts the data into the database and returns the Uri of each row inserted.

We display the Uri’s of the inserted rows in the LogCat.

Showing the names and email addresses

Retrieving data out of the database using the content resolver

Show the names and email addresses

We get an instance of the content resolver and then we set up the parameters.

Set up the parameters
  • projection – a String array of column titles, the values of which we want returned
  • selection – a String WHERE clause. A filter for a specific value that we may be looking for. We’re not looking for a specific value but all values so we pass null
  • selectionArguments – a String array of value arguments to match the selection wildcards. There is no selection so we also pass null here
  • sortOrder – whether the returned data should be sorted in any way. We’re going for the default so we pass null
  • uri – we get the Uri from the string constant CONTENT_URI
Get the MIME type

For interest sake, we call getType() to get the database MIME type.

The MIME type is displayed in the LogCat.

Do the query

We call query() and pass it the parameters.

The content provider uses its corresponding method to do the query and returns the result as a Cursor.

We use a while loop to loop through the cursor, displaying in the LogCat the name and email address for each row of data contained in the cursor.

Note that moveToFirst() positions the cursor's pointer at the first row of data that it contains, moveToNext() moves the cursor's pointer one row forward and isAfterLast() indicates whether the cursor's pointer is pointing to a position past the last row of data.

The LogCat

Display the logcat of database queries using the content resolver

The display in the LogCat

I hope that you have found this tutorial helpful.

You may also be interested in the tutorial on using Loaders to get the data out of the database. And why not check out our our Firebase tutorial, Using a Firebase Realtime Database in your app.

This tutorial was created using Android Studio. You can download the project files here download35

Are you using Eclipse or another IDE? Here's how you can use this project's Android Studio files