SlideShare a Scribd company logo
Android Content Provider
What is a Content Provider?
• Store and retrieve data and make it accessible
to all applications
• Only way to share data across applications
o There's no common storage area that all Android
packages can access
• Two types of content providers
o Android's built-in content providers
o Custom content providers we create
Why?
• Sometimes it is required to share data across
applications. This is where content providers
become very useful.
Content Provider
Android's Built-in Content Providers
• Android ships with a number of content
providers for common data types
o audio, video, images, personal contact
information, and so on
o android.provider package
• We can query these providers for the data
they contain
Android's Built-in Content Providers
• Browser
o Browser bookmarks, browser history
• CallLog
o Missed calls, call details
• Contacts
o Contact details
• MediaStore
o Media files
• Settings
o Device settings and preferences
How do you make your data public?
• Two options
o You can create your own content provider
(extending ContentProvider class) or
o You can add the data to an existing provider — if
there's one that controls the same type of data
and you have permission to write to it.
Content Provider Implementation &
Usage Model
• All content providers implement a common
interface for
o querying the provider and returning results
o adding
o altering
o deleting
• How a content provider actually stores its data
under the cover is up to its designer.
• Clients access content providers indirectly through
ContentResolver
Data Model
• Content providers expose their data as a
simple table (like in a database) model
o Each row is a record and each column is data of a
particular type and meaning
o Every record includes a numeric _ID field that
uniquely identifies the record within the table
Content Provider
• Each content provider exposes a public URI
(wrapped as a Uri object) that uniquely
identifies its data set.
o A content provider that controls multiple data
sets (multiple tables) exposes a separate URI for
each one.
• All URIs for providers begin with the string
"content://".
o The “content:” scheme identifies the data as
being controlled by a content provider.
Built-in URI Definitions
• Android defines CONTENT_URI constants for
all the providers that come with the platform.
• For example, the URI for the table that
matches phone numbers to people and the
URI for the table that holds pictures of people
(both controlled by the Contacts content
provider) are:
android.provider.Contacts.Phones.CONTENT_URI
android.provider.Contacts.Photos.CONTENT_URI
URI
• The URI constant is used in all interactions
with the content provider
o Every ContentResolver method takes the URI as
its first argument.
• It's what identifies which provider the
ContentResolver should talk to and which
table of the provider is being targeted.
URI Structure
A: Standard prefix indicating that the data is
controlled by a content provider. It's never
modified.
B: The authority part of the URI; it identifies the
content provider.
C: The path that the content provider uses to
determine what kind of data (which table) is being
requested.
D: The ID of the specific record being requested
What a Query Returns
• A query returns a set of zero or more records
• The retrieved data is exposed by a Cursor
object that can be used to iterate backward or
forward through the result set.
o You can use Cursor object only to read the data.
o To add, modify, or delete data, you must use a
ContentResolver object.
Custom Content Provider
Custom Content Provider
Custom Content Provider
Developing a Custom Content Provider
1. Extend the
ContentProvider
class.
2. In the onCreate()
method, create a
new instance of the
database helper
class.
Custom Content Provider...
Custom Content Provider...
Suppose, we need to provide access to 2
tables through this single content provider. As
we have only one method per CRUD
operation, we need a way to differentiate
between accesses to these two tables.
3. We need to define content URI paths to each
table. These are defined in a public final class
which can be used by both provider and user
as a contract: (see next slide)
Custom Content Provider...
Custom Content Provider...
Now comes the issue of differentiating
between paths. The idea is to match a URI and
then taking appropriate actions for the
corresponding table path.
4. Add a UriMatcher to the provider and add
expected URI patterns to it.
5. In the query() method, get the appropriate
table name from the URI.
Custom Content Provider...
Custom Content Provider...
6. Now write the actual query method:
• You should add this URI to notification
observables by calling setNotificationUri() so that
if this cursor is directly used in a ListView,
updating or inserting or deleting data in the table
represented by this URI would notify the ListView
of this data change
Custom Content Provider...
7. insert, update and delete methods are
similar.
• insert() returns the Uri with the newly inserted ID
appended.
• update() and delete() returns the number of
rows affected.
• You should call
notifyChangeToContentObservers(uri); before
returning from these methods.
Custom Content Provider...
We need to provide MIME type of the data returned by a URI.
8. The overridden method getType(Uri uri) needs to be
filled-in.
– For content URIs that point to a row or rows of table data, getType()
should return a MIME type in Androids vendor-specific MIME format:
• Type part: vnd
• Subtype part:
– If the URI pattern is for a single row: android.cursor.item/
– If the URI pattern is for more than one row: android.cursor.dir/
• Provider-specific part: vnd.<name>.<type>
– You supply the <name> and <type>.
– The <name> value should be globally unique, and the <type> value should be
unique to the corresponding URI pattern.
– A good choice for <name> is your companys name or some part of your
applications Android package name.
– A good choice for the <type> is a string that identifies the table associated with
the URI.
Custom Content Provider...
Custom Content Provider...
Custom Content Provider...
9. We need to declare the provider in the
manifest.xml file:
Custom Content Provider...
10. Finally, we need to define permissions for
applications who wish to access the
provider.
Different forms of permissions:
• Single read-write provider-level permission
– One permission that controls both read and write access to the
entire provider, specified with the android:permission attribute of the
<provider> element in manifest.xml.
• Separate read and write provider-level permission
– A read permission and a write permission for the entire provider.
– Specified with the android:readPermission and
android:writePermission attributes of the <provider> element.
– They take precedence over the permission required by
android:permission.
Custom Content Provider...
• Path-level permission
– Read, write, or read/write permission for a content
URI in your provider.
– You specify each URI you want to control with a
<path-permission> child element of the <provider>
element.
DEMO TIME
THANK YOU ☺

More Related Content

PDF
Presentation on linux
PDF
Introduction to fragments in android
PDF
Apresentação-Resumo sobre o Java Swing.
PDF
Unit 4- Software Engineering System Model Notes
PPTX
Session bean
PPTX
SHELL PROGRAMMING
PDF
Shell scripting
PPT
Engenharia Requisitos - Aula4 06 03 2006
Presentation on linux
Introduction to fragments in android
Apresentação-Resumo sobre o Java Swing.
Unit 4- Software Engineering System Model Notes
Session bean
SHELL PROGRAMMING
Shell scripting
Engenharia Requisitos - Aula4 06 03 2006

What's hot (20)

PPTX
Linux commands
PDF
Shell scripting
PPT
SQLITE Android
PPTX
Class object method constructors in java
PPTX
Windowforms controls c#
PPTX
Topic #3 of outline Server Environment.pptx
PPTX
GUI programming
PDF
Course 102: Lecture 19: Using Signals
PPTX
Linux standard file system
PPT
Linux commands and file structure
PDF
Modelos e etapas do processo de software.pdf
PPTX
04 activities and activity life cycle
PDF
Implementation of Pipe in Linux
PPT
Intent, Service and BroadcastReciver (2).ppt
PPT
Android - Android Intent Types
PDF
desafios na implementacao de sistemas distribuidos
PPTX
Anatomy of android aplication
PPTX
System testing
PPTX
Software Cost Estimation Techniques
PPT
Linux command ppt
Linux commands
Shell scripting
SQLITE Android
Class object method constructors in java
Windowforms controls c#
Topic #3 of outline Server Environment.pptx
GUI programming
Course 102: Lecture 19: Using Signals
Linux standard file system
Linux commands and file structure
Modelos e etapas do processo de software.pdf
04 activities and activity life cycle
Implementation of Pipe in Linux
Intent, Service and BroadcastReciver (2).ppt
Android - Android Intent Types
desafios na implementacao de sistemas distribuidos
Anatomy of android aplication
System testing
Software Cost Estimation Techniques
Linux command ppt
Ad

Similar to Custom content provider in android (20)

PPTX
Android Insights - 3 [Content Providers]
PDF
Android contentprovider
PPTX
Day 15: Content Provider: Using Contacts API
PPT
android content providers
PPTX
Content provider in_android
PPTX
Android content providers
PDF
Android Jam - ContentProviders - Udacity Lesson 4b
PPTX
Android Training (Content Provider)
PPTX
Content provider
PPTX
Android101 - Content Providers
PPT
Level 3
PPTX
SQLite Opening .pptx
PPTX
Data Transfer between Activities & Databases
PPTX
Android session 4-behestee
ODP
Android App Development - 10 Content providers
PPTX
Mobile application Development-UNIT-V (1).pptx
PDF
Bn1 1020 demo android
DOCX
Show loader to open url in web view
PPT
Content Providers in Android
PPTX
Android Database
Android Insights - 3 [Content Providers]
Android contentprovider
Day 15: Content Provider: Using Contacts API
android content providers
Content provider in_android
Android content providers
Android Jam - ContentProviders - Udacity Lesson 4b
Android Training (Content Provider)
Content provider
Android101 - Content Providers
Level 3
SQLite Opening .pptx
Data Transfer between Activities & Databases
Android session 4-behestee
Android App Development - 10 Content providers
Mobile application Development-UNIT-V (1).pptx
Bn1 1020 demo android
Show loader to open url in web view
Content Providers in Android
Android Database
Ad

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Spectroscopy.pptx food analysis technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
Teaching material agriculture food technology
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Big Data Technologies - Introduction.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
20250228 LYD VKU AI Blended-Learning.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Spectroscopy.pptx food analysis technology
Encapsulation_ Review paper, used for researhc scholars
Teaching material agriculture food technology
Digital-Transformation-Roadmap-for-Companies.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Chapter 3 Spatial Domain Image Processing.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Programs and apps: productivity, graphics, security and other tools
Per capita expenditure prediction using model stacking based on satellite ima...
Assigned Numbers - 2025 - Bluetooth® Document
Diabetes mellitus diagnosis method based random forest with bat algorithm
Big Data Technologies - Introduction.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
sap open course for s4hana steps from ECC to s4
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
A Presentation on Artificial Intelligence
Building Integrated photovoltaic BIPV_UPV.pdf

Custom content provider in android

  • 2. What is a Content Provider? • Store and retrieve data and make it accessible to all applications • Only way to share data across applications o There's no common storage area that all Android packages can access • Two types of content providers o Android's built-in content providers o Custom content providers we create
  • 3. Why? • Sometimes it is required to share data across applications. This is where content providers become very useful.
  • 5. Android's Built-in Content Providers • Android ships with a number of content providers for common data types o audio, video, images, personal contact information, and so on o android.provider package • We can query these providers for the data they contain
  • 6. Android's Built-in Content Providers • Browser o Browser bookmarks, browser history • CallLog o Missed calls, call details • Contacts o Contact details • MediaStore o Media files • Settings o Device settings and preferences
  • 7. How do you make your data public? • Two options o You can create your own content provider (extending ContentProvider class) or o You can add the data to an existing provider — if there's one that controls the same type of data and you have permission to write to it.
  • 8. Content Provider Implementation & Usage Model • All content providers implement a common interface for o querying the provider and returning results o adding o altering o deleting • How a content provider actually stores its data under the cover is up to its designer. • Clients access content providers indirectly through ContentResolver
  • 9. Data Model • Content providers expose their data as a simple table (like in a database) model o Each row is a record and each column is data of a particular type and meaning o Every record includes a numeric _ID field that uniquely identifies the record within the table
  • 10. Content Provider • Each content provider exposes a public URI (wrapped as a Uri object) that uniquely identifies its data set. o A content provider that controls multiple data sets (multiple tables) exposes a separate URI for each one. • All URIs for providers begin with the string "content://". o The “content:” scheme identifies the data as being controlled by a content provider.
  • 11. Built-in URI Definitions • Android defines CONTENT_URI constants for all the providers that come with the platform. • For example, the URI for the table that matches phone numbers to people and the URI for the table that holds pictures of people (both controlled by the Contacts content provider) are: android.provider.Contacts.Phones.CONTENT_URI android.provider.Contacts.Photos.CONTENT_URI
  • 12. URI • The URI constant is used in all interactions with the content provider o Every ContentResolver method takes the URI as its first argument. • It's what identifies which provider the ContentResolver should talk to and which table of the provider is being targeted.
  • 13. URI Structure A: Standard prefix indicating that the data is controlled by a content provider. It's never modified. B: The authority part of the URI; it identifies the content provider. C: The path that the content provider uses to determine what kind of data (which table) is being requested. D: The ID of the specific record being requested
  • 14. What a Query Returns • A query returns a set of zero or more records • The retrieved data is exposed by a Cursor object that can be used to iterate backward or forward through the result set. o You can use Cursor object only to read the data. o To add, modify, or delete data, you must use a ContentResolver object.
  • 18. Developing a Custom Content Provider 1. Extend the ContentProvider class. 2. In the onCreate() method, create a new instance of the database helper class.
  • 20. Custom Content Provider... Suppose, we need to provide access to 2 tables through this single content provider. As we have only one method per CRUD operation, we need a way to differentiate between accesses to these two tables. 3. We need to define content URI paths to each table. These are defined in a public final class which can be used by both provider and user as a contract: (see next slide)
  • 22. Custom Content Provider... Now comes the issue of differentiating between paths. The idea is to match a URI and then taking appropriate actions for the corresponding table path. 4. Add a UriMatcher to the provider and add expected URI patterns to it. 5. In the query() method, get the appropriate table name from the URI.
  • 24. Custom Content Provider... 6. Now write the actual query method: • You should add this URI to notification observables by calling setNotificationUri() so that if this cursor is directly used in a ListView, updating or inserting or deleting data in the table represented by this URI would notify the ListView of this data change
  • 25. Custom Content Provider... 7. insert, update and delete methods are similar. • insert() returns the Uri with the newly inserted ID appended. • update() and delete() returns the number of rows affected. • You should call notifyChangeToContentObservers(uri); before returning from these methods.
  • 26. Custom Content Provider... We need to provide MIME type of the data returned by a URI. 8. The overridden method getType(Uri uri) needs to be filled-in. – For content URIs that point to a row or rows of table data, getType() should return a MIME type in Androids vendor-specific MIME format: • Type part: vnd • Subtype part: – If the URI pattern is for a single row: android.cursor.item/ – If the URI pattern is for more than one row: android.cursor.dir/ • Provider-specific part: vnd.<name>.<type> – You supply the <name> and <type>. – The <name> value should be globally unique, and the <type> value should be unique to the corresponding URI pattern. – A good choice for <name> is your companys name or some part of your applications Android package name. – A good choice for the <type> is a string that identifies the table associated with the URI.
  • 29. Custom Content Provider... 9. We need to declare the provider in the manifest.xml file:
  • 30. Custom Content Provider... 10. Finally, we need to define permissions for applications who wish to access the provider. Different forms of permissions: • Single read-write provider-level permission – One permission that controls both read and write access to the entire provider, specified with the android:permission attribute of the <provider> element in manifest.xml. • Separate read and write provider-level permission – A read permission and a write permission for the entire provider. – Specified with the android:readPermission and android:writePermission attributes of the <provider> element. – They take precedence over the permission required by android:permission.
  • 31. Custom Content Provider... • Path-level permission – Read, write, or read/write permission for a content URI in your provider. – You specify each URI you want to control with a <path-permission> child element of the <provider> element.