Android ContentProvider Tutorial

ContentProvider is a component that provides data control from one application to another application. It can choose many different way to store the data. The data can be store in 3 way which is files, SQLite database or over the internet (JSON). The content provider recommended use when share data among multiple application. For example, android operating system provide the contacts data which allow other application to access it by doing some implementation. If you don want to share data, you will be better use SQLite database in android. In this tutorial, I will give you some guideline on how to create a content provider in android which is just a easy method.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “ContentProviderExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Project” and Click Next button
5. Lastly, press finish button.

Create a new ContentProvider

Create a new content provider class. Right click package name > New > Other > Content Provider. It will show a authority URI. After create a new content provider, the Android Manifest file will automatically add the provider.

Edit MyContentProvider.java class

In your content provider class, add the following code in your activity.

Edit activity.xml layout

Go to your activity.xml file and add a button in your layout.

Edit MainActivity.java class

In your MainActivity.java, add the following code in your activity.

Create a new module

Add a new module in your project and name it as readcontentprovider.

Edit activity_main.xml

In the module “readcontentprovider”, go to activity_main.xml and add a textView in the layout.

Edit MainActivity.java class

After that, go to your MainActivity.java and add the following code.

Run your Project

Ok…. its finish. Now you can test your project now…. first you need to start the module “app” first to write data in your content provider. After that, you can start “readcontentprovider” module to read the data provided by the module “app”.

(Android ContentProvider)

Source Code

(Visited 439 times, 1 visits today)
Advertisements

Kristofer Ng

A crazy android and web developer. Passion on developing android application and exploring new technology as a hobby.

You may also like...

1 Response

  1. October 15, 2016

    […] Add a new class for contentprovider name as SyncProvider, this is a dummy contentprovider without any database. This is to easy for demonstrate the feature. You can add your SQLite to the ContentProvider in the following link. https://questdot.com/android-contentprovider/ […]

Leave a Reply

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