Android Easy ContentProvider Part1

Now i am creating a tutorial on ContentProvider, this tutorial has two parts. For this tutorial i am using Android Studio 0.8.6 Beta.


When mostly people start work on Android. First they see, there are four basic components in Android namely Activity, Service, Broadcast Receiver and Content Provider. And mostly developers learn Activity , Service and Broadcast receiver easily and when thay start to learn ContentProvider they afraid about that component and some developers say “What is that”. And they stop to learn about this component because without this we can make any type of app.

So  now i am trying to make that component easy, hmmm i think i would say very easy.

Because they are very powerfull and they did magic in code, on many places without any extra effort.


Hmmm guys one more thing. We use many times Uri in ContentProvider and mostly that API also create difficulty for many developers. So i decided before start on ContentProvider, I will share some things about Uri, in this way you are familiar with Uri and you easily get the concept of ContentProvider.

OK one more question mostly developer raise. What is the difference between URI and Uri? 🙂

Ans: URI basically import from package and that is mutable . On the other hand Uri import from package and that is immutable and more efficient. So one question we resolve.

Now next about Uri or URI.

I will refer two links, if you see these links you easily grasp the concept of Uri. I am also start discussion on Uri, So if you did not see these links than no problem you will grasp the concept of Uri.

URI means “A Uniform Resource Identifier that identifies an abstract or physical resource”.

A URI is composed of many parts. One simple example (This example only define things which i require in ContentProvider 🙂 ).

As we say URI is composed of many parts, now we are starting to decompose the URI and in this way you easily grasp the concept of URI.

1 – http         2 – //       3 – android_content_provider

1 is called Scheme/Protocol.

2 is called Authority.

3 is called Path.

Now complete. I think now you know what is URI if you don’t know than no problem but only try to remember this small information about URI. If you remember this information than ContentProvider may be piece of Cake for you.



0. Now i am creating an app with name of UWantToLearnContentProvider. Here we create a Database with name of UWantToLearn.db. Here we have only one simple table with name of UWantToLearn_Info_Table.

Our table schema looks like:

Database Schema

Database Schema

1. Create a project on Android Studio.

Android Studio Project

2. Creating two new packages.

Two new packages

3. Create two new files one is DBContract and the second is DBHelper.

Two new files

3.1- Now discuss our DBContract file.

When create DB in any application, always create a File with name of Contract, where add all of your table’s meta data in static classes like Table name, Column names,  CONTENT_URI and CONTENT_TYPE etc.

Now here first i tell you about easy things as you see on line 21 to below, we have some constants like Table and Columns names, after that we have Table creating and deleting query in Info Class. Basically that class containing our meta data of table which we show in start.

Now line 12, 13, 15 and 18 are more easy than above constants if you remember our URI small tutorial.

OK don’t take tension i am discussing these Strings. 🙂

First i copy and paste some information from start of the tutorial.

1 – http         2 – //       3 – android_content_provider

1 is called Scheme/Protocol.

2 is called Authority.

3 is called Path.

As we know about Authority that is just like a Domain name “” and we also know that is unique on Internet.

Using same concept, we define our Authority for ContentProvider which is “com.uwanttolearn” because using ContentProvider we also expose our DB to other apps, So we need some unique string. For that we use Authority. I also want to mention here, when we create a ContentProvider in next step we mention that also in manifest and at that time we also give our Authority string in ContentProvider Component.


As we know about Path it is just like a some page or directory on a server against the Domain name  like we mention page “android_content_provider”.

Using same concept, we define our tables as path like on line 13.


As we know about Schema/Protocol is used to know how our communication take place as in our example we use http. Hyper Text Transfer Protocol.

In Android we have different schema’s like http, file, content etc.

Now for ContentProvider we use content Scheme because we are working with content means data. Which is available in DB.

Now i think we need some rest so rest for 1 min.

60 59 58 57 56 55 ……. 5 4 3 2 1.

OK back .

Now on line 15 we have a BASE_URI which is equal to “content://com.uwanttolearn” like we have “ base url for Internet.

So we use that BASE_URI every where in our ContentProvider with appending some extra PATH. Like we use our Domain name with append some path. Example or

Now last thing which is on the line of 18. I think now that String not require any explanation. You can think easily, what is that.

I know, you know about that line but i want to explain, so please don’t mind.

CONTENT_URI = “content://com.uwanttolearn/info” that line saying hey i am in Info Class, So if you want to do any thing about Info Table Data use this URI.

And how we call that URI from any other class, we call that using Info.CONTENT_URI.

3.2 – DBHelper class.

That is very simple class i am not explaning that class here. If you feel any difficulty please read out the 3.2 of my tutorial “EasySQLite With Unit Tests”.

4. Create a new file which is our ContentProvider.

5.  Also mention in Manifest.


Now here you see Arrow 1 we are giving our Authority. Which is unique in all apps which are available on Android mobile.

And about Arrow 2 that is Saying hey i am not available for other apps. Mean that ContentProvider only available in our app. 🙂

Hmmm i think you have a Question, if we are not exposing our DB to other apps why we are creating that ContentProvider and my answer is MAGIC.

As i already discuss ContentProvider do magic in application and they are great in efficiency point of view and many more benefits. Which we will discuss when we start work on Live application example.

Now we start working on ContentProvider.

6.  UWantToLearnContentProvider.class




OK Gusy that is our ContetProvider class. And i think, if you read these lines of code, you easily determine what these lines do. Simple SQLite operations.

Now here one thing always create ambiguity, what is UriMatcher. So i am starting from that API.

I am giving my own definition for UriMatcher for learning purpose. How many people know about switch statement?

Great every one. Then i think UriMatcher is a piece of cake for you.

Only think UriMatcher is just like a switch statement. So its mean we give any value to that UriMatcher and it give us a result. Now see code from line 24 to 33.

Here we simple initialize our UriMatcher or in my words i write a switch statement for my Uri’s. 🙂

On line 32 i am adding one Uri in UriMatcher. Imagine It is just like a case statement in switch.

mUriMatcher.addURI(DBContract.AUTHORITY,DBContract.PATH_INFO_TABLE, ALL);

It take three argument.

1 – Authority

2 – Path

3 – Unique Code

So first two are easy. Here basically we are making our switch case statement like hey when i send you Authority+Any Path + UniqueCode than do Insert operation.

And as we know we have four methods query, insert, delete, update. So i can define one UriMacther statement and use in four methods because that is unique methods. For example when i call that in query method its mean return all rows but when i call in delete than its mean delete one row. Any ambiguity.






Yes sure.

Ok  great.

And at this time i am only using one operation so we only add one UriMatcher.

Opps Sorry we forgot the unique code. Unique code basically decides what operation you want, like i have same Authority and Path. So i write unique code 1 for get all rows. And when UriMatcher return 2 its mean give me data against unique id.

Here first i decide did not tell you one  more thing about UriMatcher but now i change my decision. So now i am telling you one more thing but that is not added in our live app which i show you in next part.

As i say in above paragraph “UriMatcher return 2 its mean give me data against that id”. Question When UriMatcher return 2?

Like i add one more UriMacher to handle that case.


Second argument little confusing.

Now there are two wild card characters which we use.

*: Matches a string of any valid characters of any length.

#: Matches a string of numeric characters of any length.

Now i want to get a user data having id 3. Now in that case i send a Uri like.

Uri uri = ContentUris.withAppendedId(DBContract.Info.CONTENT_URI, 3);

Now i get a new Uri which have Authority+PathInfo+3. Now when i send that Uri to our query method. Our UriMatcher compare that Uri and as we add second case. So that case is true because ‘#’ means, here any integer value attached with that Uri. So now i got 2.

Huffffff Guys any ambiguity.




I think UriMatcher is complete. Now move to next part.

Guys here i only explain query method and all other methods are easy, so please read on your own. And i am 100% sure you easily know, what they are doing.

As we know in ContentProvider we work with Uri’s. So user send us a Uri.

So from line 44 to 57 if you see i got a Uri, after that i send that Uri to our UriMatcher, which work for us as a switch statement and that return us a result like our unique code and after that we use that unique code in our switch statement and do the operation according to that unique code. Line 55 is very important that line is a MAGIC Line which i will discuss in our app example in part2 but basically that line saying if any change occur against this Uri in DB than notify the caller.

Part 1 Complete guys if you feel any ambiguity you can email me on my email id mention below, i will try to give you response as early as possible.


Facebooktwitterredditpinterestlinkedinmailby feather

5 thoughts on “Android Easy ContentProvider Part1

  1. I have been exploring for a little bit for any high-quality articles
    or blog posts on this kind of area . Exploring in Yahoo I at last stumbled upon this
    website. Reading this information So i’m happy to convey
    that I’ve an incredibly excellent uncanny feeling I came upon just what I needed.
    I most no doubt will make sure to don?t overlook this web site and provides
    it a look regularly.

  2. May I just say what a relief to uncover somebody who really knows what they’re discussing over the
    internet. You certainly know how to bring a problem to light and make
    it important. More and more people really need to look at this and
    understand this side of the story. I was surprised you’re not more popular given that you most
    certainly have the gift.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.