SlideShare ist ein Scribd-Unternehmen logo
1 von 33
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 ☺

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

WPF
WPFWPF
WPF
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
What happens when you type google com into your browser and press enter
What happens when you type google com into your browser and press enterWhat happens when you type google com into your browser and press enter
What happens when you type google com into your browser and press enter
 
virtual hosting and configuration
virtual hosting and configurationvirtual hosting and configuration
virtual hosting and configuration
 
File permission of linux
File permission of linuxFile permission of linux
File permission of linux
 
Files in php
Files in phpFiles in php
Files in php
 
Web Server And Database Server
Web Server And Database ServerWeb Server And Database Server
Web Server And Database Server
 
ADO .Net
ADO .Net ADO .Net
ADO .Net
 
jQuery PPT
jQuery PPTjQuery PPT
jQuery PPT
 
Php Error Handling
Php Error HandlingPhp Error Handling
Php Error Handling
 
Cookies & Session
Cookies & SessionCookies & Session
Cookies & Session
 
Database Security And Authentication
Database Security And AuthenticationDatabase Security And Authentication
Database Security And Authentication
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Php session
Php sessionPhp session
Php session
 
File handling
File handlingFile handling
File handling
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Less17 moving data
Less17 moving dataLess17 moving data
Less17 moving data
 
Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 

Ähnlich wie Custom content provider in android

Day 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts APIDay 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts APIAhsanul Karim
 
Content provider in_android
Content provider in_androidContent provider in_android
Content provider in_androidPRITI TELMORE
 
Android contentprovider
Android contentproviderAndroid contentprovider
Android contentproviderKrazy Koder
 
Android content providers
Android content providersAndroid content providers
Android content providersKurt Mbanje
 
android content providers
android content providersandroid content providers
android content providersDeepa Rani
 
Data Transfer between Activities & Databases
Data Transfer between Activities & DatabasesData Transfer between Activities & Databases
Data Transfer between Activities & DatabasesMuhammad Sajid
 
Android Training (Content Provider)
Android Training (Content Provider)Android Training (Content Provider)
Android Training (Content Provider)Khaled Anaqwa
 
CrossRef Text & Data Mining - UKSG 2015
CrossRef Text & Data Mining - UKSG 2015CrossRef Text & Data Mining - UKSG 2015
CrossRef Text & Data Mining - UKSG 2015Crossref
 
Android Application Components-BroadcastReceiver_Content Provider.pptx
Android Application Components-BroadcastReceiver_Content Provider.pptxAndroid Application Components-BroadcastReceiver_Content Provider.pptx
Android Application Components-BroadcastReceiver_Content Provider.pptxKNANTHINIMCA
 
UKSG Conference 2015 - CrossRef Text and Data Mining Services: one year in Ra...
UKSG Conference 2015 - CrossRef Text and Data Mining Services: one year in Ra...UKSG Conference 2015 - CrossRef Text and Data Mining Services: one year in Ra...
UKSG Conference 2015 - CrossRef Text and Data Mining Services: one year in Ra...UKSG: connecting the knowledge community
 
Development of Web Services for Android Applications
Development of Web Services for Android ApplicationsDevelopment of Web Services for Android Applications
Development of Web Services for Android ApplicationsMd Ashraful Haque
 
Content package - Mobile computing
Content package - Mobile computingContent package - Mobile computing
Content package - Mobile computingPriyanka Rana
 
Data Transfer between Activities & Databases
Data Transfer between Activities & DatabasesData Transfer between Activities & Databases
Data Transfer between Activities & DatabasesMuhammad Sajid
 
Android session 4-behestee
Android session 4-behesteeAndroid session 4-behestee
Android session 4-behesteeHussain Behestee
 
Android101 - Content Providers
Android101 - Content ProvidersAndroid101 - Content Providers
Android101 - Content Providersjromero1214
 

Ähnlich wie Custom content provider in android (20)

Android Insights - 3 [Content Providers]
Android Insights - 3 [Content Providers]Android Insights - 3 [Content Providers]
Android Insights - 3 [Content Providers]
 
Day 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts APIDay 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts API
 
Content provider in_android
Content provider in_androidContent provider in_android
Content provider in_android
 
Android contentprovider
Android contentproviderAndroid contentprovider
Android contentprovider
 
Android content providers
Android content providersAndroid content providers
Android content providers
 
android content providers
android content providersandroid content providers
android content providers
 
Data Transfer between Activities & Databases
Data Transfer between Activities & DatabasesData Transfer between Activities & Databases
Data Transfer between Activities & Databases
 
Android Training (Content Provider)
Android Training (Content Provider)Android Training (Content Provider)
Android Training (Content Provider)
 
CrossRef Text & Data Mining - UKSG 2015
CrossRef Text & Data Mining - UKSG 2015CrossRef Text & Data Mining - UKSG 2015
CrossRef Text & Data Mining - UKSG 2015
 
Bn1 1020 demo android
Bn1 1020 demo  androidBn1 1020 demo  android
Bn1 1020 demo android
 
Android Application Components-BroadcastReceiver_Content Provider.pptx
Android Application Components-BroadcastReceiver_Content Provider.pptxAndroid Application Components-BroadcastReceiver_Content Provider.pptx
Android Application Components-BroadcastReceiver_Content Provider.pptx
 
UKSG Conference 2015 - CrossRef Text and Data Mining Services: one year in Ra...
UKSG Conference 2015 - CrossRef Text and Data Mining Services: one year in Ra...UKSG Conference 2015 - CrossRef Text and Data Mining Services: one year in Ra...
UKSG Conference 2015 - CrossRef Text and Data Mining Services: one year in Ra...
 
Jisc Publications Router poster
Jisc Publications Router posterJisc Publications Router poster
Jisc Publications Router poster
 
Development of Web Services for Android Applications
Development of Web Services for Android ApplicationsDevelopment of Web Services for Android Applications
Development of Web Services for Android Applications
 
Content package - Mobile computing
Content package - Mobile computingContent package - Mobile computing
Content package - Mobile computing
 
Data Transfer between Activities & Databases
Data Transfer between Activities & DatabasesData Transfer between Activities & Databases
Data Transfer between Activities & Databases
 
Android session 4-behestee
Android session 4-behesteeAndroid session 4-behestee
Android session 4-behestee
 
Android101 - Content Providers
Android101 - Content ProvidersAndroid101 - Content Providers
Android101 - Content Providers
 
NISO Open Discovery Initiative, ALA Midwinter
NISO Open Discovery Initiative, ALA MidwinterNISO Open Discovery Initiative, ALA Midwinter
NISO Open Discovery Initiative, ALA Midwinter
 
Patham "NISO-ODI (Open Discovery Initiative) Standards Update"
Patham "NISO-ODI (Open Discovery Initiative) Standards Update"Patham "NISO-ODI (Open Discovery Initiative) Standards Update"
Patham "NISO-ODI (Open Discovery Initiative) Standards Update"
 

Kürzlich hochgeladen

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 

Kürzlich hochgeladen (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 

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.