Basic concept of Content Provider

Definition :-
Content Provider is one of the four component of android. Content provider mainly used to share data with other applications. You can also use this Content Provider within your application.

What types of data can be shared through content provider's?
You can share two types of data using Content Provider :-
1) File Data such as video, audio, text, data that is stored on your private space.
2) Structured Data i.e. stored in your application database (sql), array or similar data structure.


Key Points:-
  • It is used for secure data access between applications using Inter Process Communication.
  • For accessing data from  provider of other applications, such as Contacts, Calendar, Browser, SearchRecentSuggestion Provider and so on.
  • Synchronizing data with your server by using AbstractThreadSynAdapter. 
  • You can load data in UI using Cursor Loader.
  • Sending data to widgets

How to access a Content Provider?
There are two ways to access content provider :-
1) Content Resolver
2)  Intent. The application provide ita own UI(activity) to other application. Other application can access data or modify data of that provider through it.
You cannot access content provider like other component such as activity by startActivity or service by startService. You need Content Resolver for accessing a Provider. By using content resolver you can perform CRUD operation on the data provided by Provider instance.
In UI like Activity or Fragment you can use Cursor Loader. It load data asynchronously by using content resolver.


When you need to create a Content Provider?
Basically lesser applications needs to create content provider unless they want to share their data among other application. Device pre-build applications such as Contact, Dictionary, Calendar have their Provider. So that other application can access data and use it in their application.


How to create a Content Provider ?
These are the basic steps to make Provider for your application.
  • Create a concrete class for Provider by extending ContentProvider class.
  • Define provide authority string, content uri and colums.
  • Define intent if you want provider application handle intent for performing operation on provider. Also define action, data, flag for that.
  • Also define permission if want to prevent some of your data access.
Content URI
To query a content provider, you need to specify a query string in the form of content uri.
Content URI is the uri that identifies data in a Content Provider.
Content Uri for content provider looks like this :-
content://authority/optionalPath/optionalId
It contains of four part schema, authority, an optional path and an optional id.
Schema for content provider is always  content. You have to used :// to separate content from authority.
Authority is unique identifier such like application package name. It should be used for identifying provider. Like for contacts authority is com.android.contacts
optionalPath is used to distinguish type of data your content provider offers.
optionalId This is also option you can access a single record(row) by using this.

That's all for content provider, but this is very basic you can further explore content provider here in google docs. Also you can go through my next blog to Create a Content Provider.

Comments

Popular posts from this blog

Software Design Principles (SOLID)

Draw Over Screen App Example Android

Sync Adapter Overview