Writing your own content provider

Published On: 18 January 2017.By .
  • Mobile

What is Content Provider.?? 

A content provider manages access to a central repository of data. A provider is part of an Android application, which often provides its own UI for working with the data. However, content providers are primarily intended to be used by other applications, which access the provider using a provider client object.

Content providers let you centralize content in one place and have many different applications access it as needed. A content provider behaves very much like a database where you can query it, edit its content, as well as add or delete content using insert(), update(), delete(), and query() methods. In most cases, this data is stored in an SQLite database.

Content Uris

To query a content provider, you specify the query string in the form of a URI which has the following format −

<prefix>://<authority>/<data_type>/<id>

prefix

This is always set to content://

Authority

This specifies the name of the content provider, for example, contacts, browser etc. like com.example.app.provider

Data Type

This indicates the type of data that this particular provider provides. Best practice is use table name here.

id

This specifies the specific record requested. like prefix://Authority/id/5

 

How to implement content provider.

ContentProvider is a class in Android SDK, You need to extend this then code look like.

Now you need to override method according to need.

Query()

Retrieve data from your provider.

insert()

Insert a new row into your provider.

 

 

For getting Id you take from here

 

update()

Update existing rows in your provider

delete()

Delete rows from your provider.

 

getType()

onCreate()

Initialize your provider. The Android system calls this method immediately after it creates your provider. Notice that your provider is not created until a ContentResolver object tries to access it.

 

For URI  Table Mapping you can use

Now Final code like this

Use case of  retrieving data

 

Use case of inserting data

Use case of updating data

Use case of deleting data 

 

 

 

 

Related content

That’s all for this blog