Skip to main content

Use a content provider

Its like bridge/proxy between java db and our/other app.

 

if content provider is there for a db then its really easy to access data from the db without coding directly for the db.

Content resolver is provides the info for Content provider for where to access the info. A URI is needed to connect between the two.

 

Initial steps are to create three classes: Contract, Database and Provider

Database class handles the actual db i.e CRUD operations

Contract class contains all the info that is required by the Content Provider. Each content provider shold have a separate contract class.

 

Contract class explanation

so its contains the interface containg column names of the table which are supposed to be accessed.

then the name of Content authority for the Content provider (e.g org.example.myapp.provider)

then the URI is to built which starts as “content://+CONTENT_AUTHORITY” as the base URI

then the path i.e the table name

then we have array of strings containing all top level paths

the we make an inner class relating the info from the db and implement the interface created in the first step and BaseColumns (have two standard columns that every db has id and count)

in this inner class make a Uri from base uri and the path to acesss the friends component of the created content provider.

mime types are labels for a given set of data. So content provider needs to define mime types for the data its working with.

so create a mime type , and we have two types of them. One which provides access to group of records and one for single item e.g vnd.android.cursor.dir/+”vnd.”+CONTENT_AUTHORITY+”.”+tablename and vnd.android.cursor.item/+”vnd.”+CONTENT_AUTHORITY+”.”+tablename

a method for building a valid Uri for getting a particular record having an id

a getter to get an id from the Uri built in last step

 

Provider class explanation

extend the class with ContentProvider

make a variable for the DB class that was made

make a UriMatcher method that returns a matcher object, which basically tells that the content provider is able to access the data only for those matching Uris.

then we have integer codes fo the two different requests ie. one for all records and one for single record. These are the part of the Urimatcher.

override the oncreate method in which the db variable is initialised

a deletedatabse method which deletes the present databse after closing it and creates a fresh db

override the gettype(Uri) method which matches the Uri passed with the help of Urimatcher object and returns string to indicate which type of uri was passed (to get single or all records)

add query method i.e override the query method

add insert method i.e override the insert method

similarly override update and delete method

 

Changes in manifest file

make a custom permission so that other apps are able to accesss the data through content provider

add the same permission as uses permission

add the provider tag and add the fileds like name,authority,exported ,readpermission and writepermission in it.

Comments