Content Providers

Content providers share your data safely and efficiently across app boundaries by abstracting underlying data source, be it like SQLite or files, etc. This way, other apps can access your data without really need to understand how you stored it.

Ex: Calendar, SMS, Contacts APIs work that way, using Shared content provider.

—>>
By using Content providers, its easier for you to potentially switch out the data source.

On the UI layer, it’s a generic mechanism to return cursors.

As far as the framework is concerned, all data is handled through content providers. So, if you want to interact with anything outside of your app, such as sending data to a widget, or returning search results from the newer app you need a content provider for that too.

SyncAdapters and CursorLoaders also expect content provider for data.

Create a Content Provider

Graphic: 4 Steps to Building a ContentProvider

If you’re new to ContentProviders, check out our gnarlicious background lesson

Building a Content Provider involves several steps :

  • Determining the URIs
  • Update Contract class
  • Fill out URIMatcher
  • Implement six required content provider functions

What’s a URI ??

A Content provider allows us to think of the data associated with our views in terms of Universal Resource Identifiers (URIs).

They identify a resource, in our case, a row or rows in a database.

Content provider returns a cursor containing the database rows that correspond to the URI.

Each URI can contain the content scheme.

Example:
content://com.example.android.sunshine.app/weather/506001?Date=04302016

Scheme : first part of the URI that precedes the colon.It identifies the protocol that the URI will be using. Here, ‘content:’ implies that the URI refers to a Content Provider.

Authority :  com.example.android.sunshine.app — which is a unique string used to locate your content provider. Almost always be the package name of the application.

Location : weather — typically points to database table within the application.

Query : optional — this query can either be part of the URI path or can take the form of a question mark.

These Content Provider URIs used for in android is to notify the UI that a piece of data it is displaying has changed.

Learn about Content Providers

https://classroom.udacity.com/nanodegrees/nd801/parts/8011345403/modules/432468910275460/lessons/3599339441/concepts/36598388540923

Documentation on UriBuilder

Understand the UriMatcher

Content Providers implement functionality based upon URIs passed to them.

For ease of implementation, content providers typically tie each URI type internally to an integer constant.

Android provides a UriMatcher class to help match incoming URIs to the content provider integer constants.

Documentation on UriMatcher

Register the Content Provider

Documentation on provider
ContentProvider basics

We need to register Content provider to the AndroidManifest.xml file, so that Android knows about it.

The ContentResolver class locates our class using the Content Authority and makes direct calls to the provider on our behalf.

Code the ContentProvider

Documentation on parseId

 

Note:

Doing operations like queries on the UI thread cause Android to not be able to draw frames fast enough with introduces frame rater jitter.

Fortunately,  Android offers a pattern for that known as Loaders.