Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Module 7

Content Providers
What are Content Providers?
• Store and retrieve data and make it accessible to all
applications.
• Only way to share data across applications.
Examples of Android Content Providers

• The Gallery that contains images.


• Contact lists that contain Contact details.
• A dictionary that has collections of all the words that are
used.
• The music playlist has a list of songs.
• Call logs contain the call details.
Android’s Built-in Content Providers
• Browser
– Browser bookmarks,browser history
• CallLog
– Missed call,call details
• Contacts
– Contact details
• MediaStore
– Media files
• Settings
– Device settings and preferences
Content Provide Implementation and Data Model

• Client access the content providers indirectly through


ContentResolver.
• Content providers expose their data as a simple
table(like in a database) model
– Each row is a record and each column is data of a particular
type and meaning.
– Every record includes a numeric _ID filed that uniquely
identifies the record within the table.
Content URIs

• Each content provider exposes a public URI that uniquely


identifies its data set.
• All URIs for provider begins with the string “content://”
content://….
• Android defined CONTENT_URI constant for all the providers
that come with the platform.
android.provide.Contacts.Phones.CONTENT_URI
android.provide.Contacts.Photos.CONTENT_URI
URI Structure
Operations in Content Provider

• our fundamental operations are possible in Content


Provider namely Create, Read, Update, and Delete.
These operations are often termed as CRUD
operations.
• Create: Operation to create data in a content provider.
• Read: Used to fetch data from a content provider.
• Update: To modify existing data.
• Delete: To remove existing data from the storage.
CallLog Content Provider
• Used for Accessing the call log on the handset via the class
android.providerCallLog.
• Use to filter the recently dialled calls,received calls and
missed calls.
• The date and duration of each call are logged and tied back to
the Contacts application for caller identification purposes.
Example
Example
• Right click on Package Name
• Select New Java Class
• Create CallLogHelper class
Example
• insertPlaceholderCall() method

Call above two method into your


Main method for getting and
inserting the call logs
six abstract methods of
ContentProvider class
Custom Content Provider
1. Create a class that extends ContentProvider

2.Define your content provider URI address which will be used to access
the content.

3. Create your own database to keep the content.


• Android uses SQLite database and framework needs to
override onCreate() method which will use SQLiteOpenHelper method to
create or open the provider's database.
4.Implement Content Provider queries to perform different database
specific operations.
5. Register your Content Provider in your activity file using <provider> tag.
Example
Step 1
• Modify main activity file MainActivity.java to add two new
methods onClickAddName() and onClickRetrieveStudents().
Step 2
• Create new file StudentsProvider.java under com.example.MyApplication package
which will extends ContentProvider class. It will asks to override methods.
Override all six methods of ContentProvider class.
StudentProvider.java
DatabaseHelper class
StudentProvider.java
override OnCreate() method of ContentProvider
StudentProvider.java
override insert() method of ContentProvider
StudentProvider.java
override query() method of
ContentProvider
StudentProvider.java
override update() method of ContentProvider
StudentProvider.java
override getType() method of ContentProvider
Registering ContentProvider
• Modified content of AndroidManifest.xml file. Here we have
added <provider.../> tag to include our content provider:
THANK YOU

You might also like