What are Content Providers in Android

In today’s android tutorial, we will learn about the concept of Content Provider in the world of android development, like what are content providers, why you should use them, how you should create & use content providers in your android applications. A content provider simply supplies data from one application to another application, when an application asks for the data.

Content Providers are used extensively by the Android, giving you the opportunity to enhance your android applications with native Content Providers, including contacts, calendar, etc. Also in this tutorial, you will learn how to store and retrieve data from these core Android Content Providers to provide your app users with a consistent, and supreme user experience.

Why you should use Content Providers?

The biggest reason to use a Content Provider in your android applications is to share data sharing across other android applications installed on your android device. Any application can easily query, add, remove, or update data provided by another application if it has the required permissions through a Content Provider.

Content Providers seperates your apps from the data layer by encapsulating the database. This approach is useful in making your applications data-source agnostic, such that your data storage method can be modified without affecting your application layer.

Creating a new Content Provider

To create a new Content Provider, we need to extend the ContentProvider class:

public class MyContentProvider extends ContentProvider {
  @Override
  public boolean onCreate() {
    return false;
  }
  @Nullable
  @Override
  public Cursor query(@NonNull Uri uri, 
                      @Nullable String[] projection, 
                      @Nullable String selection, 
                      @Nullable String[] selectionArgs, 
                      @Nullable String sortOrder) {
    // TODO: Perform a query and return Cursor.
    return null;
  }
  @Nullable
  @Override
  public String getType(@NonNull Uri uri) {
    // TODO: Return the mime-type of a query.
    return null;
  }
  @Nullable
  @Override
  public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
    // TODO: Insert the Content Values and return a URI to the record.
    return null;
  }
  @Override
  public int delete(@NonNull Uri uri, 
                    @Nullable String selection, 
                    @Nullable String[] selectionArgs) {
    // TODO: Delete the matching records and return the number
    //       of records deleted.
    return 0;
  }
  @Override
  public int update(@NonNull Uri uri, 
                    @Nullable ContentValues values, 
                    @Nullable String selection, 
                    @Nullable String[] selectionArgs) {
    // TODO: Update the matching records with the provided 
    //       Content Values, returning the number of records updated.
    return 0;
  }
} 

Creating Content Provider Database

First we need to initialize the data source we want to access through Content Provider. Let’s consider we are using SQLite database, we can initialize the data source by using the SQLite Open Helper implementation as you can see below.

private DBOpenHelper dbOpenHelper;
@Override
public boolean onCreate() {
  // Creating the underlying database.
  // a query or transaction.
  mHoardDBOpenHelper = 
    new HoardDB.HoardDBOpenHelper(getContext(), DBOpenHelper.DATABASE_NAME, null, DBOpenHelper.DATABASE_VERSION);
  return true;
} 

REGISTERING CONTENT PROVIDERS

Content Providers must be registered in your application manifest like activities and services before the Content Resolver can discover and use them. we can do this by using a provider tag that includes a name attribute, describing the Provider’s class name, and an authorities tag. Use the authorities tag to define the base URI of the Provider. A Content Provider’s authority is used by the Content Resolver as an address to find the database to interact with. Keep in mind, it’s a good practice that each Content Provider authority is unique.

<provider android:name=".MyContentProvider"
          android:authorities="com.codepoint.provider.myProvider"/> 

Leave a Reply

Your email address will not be published. Required fields are marked *