Redeem Now
Read More
Read now

Chapter 1: Share data between applications using Content Providers


By using the content provider, data can be shared across applications. The content provider is a set of data wrapped up in a custom API to read and write. Applications/Processes have to register themselves as a provider of data. Other applications can request Android to read/write that data through a fixed API.
Content provider API has methods to perform operations(CRUD) on data.
Contacts are the best example – that exposes user information to other applications, Media store-Allows other applications to access, store media files.
There is some standard methods – insert(), query(), update(), delete(), to access application data. So it is easy to implement a content provider.
We need to assign a URI to each Content Provider, starting with “content://” and that will be recognized by applications.

Steps to writing a Content Provider:

1. Create a class for ContentProvider.
2. Define content URI
3. Implement all the unimplemented methods. insert(), update(), query(), delete(), getType().
4. Declare the content provider in AndroidManifest.xml


URI:  Content provider URI – content://authority/path/id

1. content:// –    The content provider URIs should start with this value.
2. ‘authority’ – This is Java namespace of the content provider implementation passes the Java package name.
3. ‘path’  –  This is a virtual directory within the provider that identifies, what kind of data being requested.
4. ‘id’ –  This is an optional part that specifies the primary key of a record being requested. This part can be ignored to access all data.


Applying CRUD operations:

Add data:
We need to override insert() method. If data inserted successful, it will return the URI value with the associated with ID.
For example: If we passed – content://packageName/sample
Method will return content://packageName/sample/1

Updating data:
update() method of the ContentProvider is used to update records.
This method will return the number of rows updated.

Deleting data:
delete() method of the ContentProvider will return the number of records deleted.


Registering the provider in AndroidManifest.xml:

We need to register the content providers in the AndroidManifest.xml.

. . .


Add Your Comment

Be the first to comment.

Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again