0% found this document useful (0 votes)
22 views25 pages

Module 7

This document provides information about content providers in Android. It defines what content providers are, gives examples of built-in Android content providers like contacts, call logs and media files. It describes how content providers make data accessible across applications via a simple table model exposed through a ContentResolver. It explains how to perform CRUD (create, read, update, delete) operations on content provider data through content URIs and provides an example of implementing a custom content provider by extending the ContentProvider class and overriding its abstract methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views25 pages

Module 7

This document provides information about content providers in Android. It defines what content providers are, gives examples of built-in Android content providers like contacts, call logs and media files. It describes how content providers make data accessible across applications via a simple table model exposed through a ContentResolver. It explains how to perform CRUD (create, read, update, delete) operations on content provider data through content URIs and provides an example of implementing a custom content provider by extending the ContentProvider class and overriding its abstract methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.
[Link].CONTENT_URI
[Link].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
[Link].
• 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

[Link] 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.
[Link] 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 [Link] to add two new
methods onClickAddName() and onClickRetrieveStudents().
Step 2
• Create new file [Link] under [Link] package
which will extends ContentProvider class. It will asks to override methods.
Override all six methods of ContentProvider class.
[Link]
DatabaseHelper class
[Link]
override OnCreate() method of ContentProvider
[Link]
override insert() method of ContentProvider
[Link]
override query() method of
ContentProvider
[Link]
override update() method of ContentProvider
[Link]
override getType() method of ContentProvider
Registering ContentProvider
• Modified content of [Link] file. Here we have
added <provider.../> tag to include our content provider:
THANK YOU

You might also like