A content provider manages access to a central repository of data. A provider is part of an Android application, which often provides its own UI for working with the data. However, content providers are primarily intended to be used by other applications, which access the provider using a provider-client object. Together, providers and provider clients offer a consistent, standard interface to data that also handles inter-process communication and secure data access.
Typically you work with content providers in one of two scenarios; you may want to implement code to access an existing content provider in another application, or you may want to create a new content provider in your application to share data with other applications. This topic covers the basics of working with existing content providers. To learn more about implementing content providers in your own applications, see Creating a content provider.
This topic describes the following:
- How content providers work.
- The API you use to retrieve data from a content provider.
- The API you use to insert, update, or delete data in a content provider.
- Other API features that facilitate working with providers.
A content provider presents data to external applications as one or more tables that are similar to the tables found in a relational database. A row represents an instance of some type of data the provider collects, and each column in the row represents an individual piece of data collected for an instance.
A content provider coordinates access to the data storage layer in your application for a number of different APIs and components as illustrated in figure 1, these include:
- Sharing access to your application data with other applications
- Sending data to a widget
- Returning custom search suggestions for your application through the search framework using
- Synchronizing application data with your server using an implementation of
- Loading data in your UI using a
Figure 1. Relationship between the content provider and other components.
- Accessing a provider
When you want to access data in a content provider, you use the object in your applications to communicate with the provider as a client. The
ContentResolver object communicates with the provider object, an instance of a class that implements
ContentProvider. The provider object receives data requests from clients, performs the requested action, and returns the results. This object has methods that call identically-named methods in the provider object, an instance of one of the concrete subclasses of
ContentResolver methods provide the basic "CRUD" (create, retrieve, update, and delete) functions of persistent storage.
A common pattern for accessing a
ContentProvider from your UI uses a
CursorLoader to run an asynchronous query in the background. The
Fragment in your UI call a
CursorLoader to the query, which in turn gets the
ContentProvider using the
ContentResolver. This allows the UI to continue to be available to the user while the query is running. This pattern involves the interaction of a number of different objects, as well as the underlying storage mechanism, as illustrated in figure 2.
Android app activities
Contains classes for accessing and publishing data on a device. It includes three main categories of APIs:
- Content sharing (
- For sharing content between application components. The most important classes are:
ContentResolver for managing and publishing persistent data associated with an application.
IntentFilter, for delivering structured messages between different application components—allowing components to initiate other components and return results.
- Package management (
- For accessing information about an Android package (an
.apk), including information about its activities, permissions, services, signatures, and providers. The most important class for accessing this information is
- Resource management (
- For retrieving resource data associated with an application, such as strings, drawables, media, and device configuration details. The most important class for accessing this data is