SlideShare ist ein Scribd-Unternehmen logo
1 von 18
Android Insights - 3


  Content Providers
Topics to be Covered
• Content Provider Basics
• Accessing a Content Provider
• Developing a Custom Content Provider




2/11/2012          Android Insights - 3   2/18
Content Provider
• A content provider manages access to a central
  repository of data..
• The provider is part of an Android application, which
  often provides its own UI for working with the data.
• However, content providers are primarily intended to
  be used by other applications, which access the
  provider using a provider client object.
• Together, providers and provider clients offer a
  consistent, standard interface to data that also handles
  inter-process communication and secure data access.


2/11/2012               Android Insights - 3            3/18
Accessing a Content Provider
• A content provider offers methods which
  correspond to the basic CRUD functions of
  persistent storage.
• An application accesses the data from a content
  provider with a ContentResolver client object.
  This object has methods that call identically-
  named methods in the provider object.
• A content provider is identified by a content URI.


2/11/2012             Android Insights - 3         4/18
Accessing a Content Provider...
• Example of getting a list of words from the User
  Dictionary provider:




• The content URI of the words table is:
    content://user_dictionary/words
• Read permission for accessing the content
  provider is also needed in the manifest file:

2/11/2012              Android Insights - 3          5/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.

2/11/2012             Android Insights - 3   6/18
Developing a 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)

2/11/2012            Android Insights - 3        7/18
Developing a Custom Content Provider...




2/11/2012        Android Insights - 3     8/18
Developing a 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.

2/11/2012           Android Insights - 3      9/18
Developing a Custom Content Provider...




2/11/2012        Android Insights - 3     10/18
Developing a 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.

2/11/2012                   Android Insights - 3           11/18
Developing a 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.


2/11/2012                   Android Insights - 3   12/18
Developing a 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 common types of data such as as text, HTML, or JPEG, getType()
                should return the standard MIME type for that data.
     –          For content URIs that point to a row or rows of table data, getType()
                should return a MIME type in Android's 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 company's name or some part of your application's
                        Android package name.
                   –    A good choice for the <type> is a string that identifies the table associated with the URI.


2/11/2012                                      Android Insights - 3                                         13/18
Developing a Custom Content Provider...
• Content type defined in the contract class:




2/11/2012            Android Insights - 3       14/18
Developing a Custom Content Provider...
• getType() method in the provider class:




2/11/2012          Android Insights - 3     15/18
Developing a Custom Content Provider...
9. We need to declare the provider in the
   manifest.xml file:




2/11/2012           Android Insights - 3    16/18
Developing a 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.
• 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.
• Temporary permission
      – A permission level that grants temporary access to an application, even if the application
         doesn't have the permissions that are normally required.
2/11/2012                                Android Insights - 3                               17/18
Developing a Custom Content Provider...
• Permission defined in manifest.xml of the
  provider:



• Permission defined in manifest.xml of the
  user:




2/11/2012           Android Insights - 3      18/18

Weitere ähnliche Inhalte

Was ist angesagt?

Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 
Recovered file 1
Recovered file 1Recovered file 1
Recovered file 1
Uthara Iyer
 
Asp net interview_questions
Asp net interview_questionsAsp net interview_questions
Asp net interview_questions
Bilam
 
Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentals
Madhuri Kavade
 
Programming web application
Programming web applicationProgramming web application
Programming web application
aspnet123
 

Was ist angesagt? (20)

Android Training Session 1
Android Training Session 1Android Training Session 1
Android Training Session 1
 
Android Trainning Session 2
Android Trainning  Session 2Android Trainning  Session 2
Android Trainning Session 2
 
Secure Sharing PHI PCI PII -Android app_Content Provider
Secure Sharing PHI PCI PII -Android app_Content ProviderSecure Sharing PHI PCI PII -Android app_Content Provider
Secure Sharing PHI PCI PII -Android app_Content Provider
 
Android Services
Android ServicesAndroid Services
Android Services
 
Create an android app for database creation using.pptx
Create an android app for database creation using.pptxCreate an android app for database creation using.pptx
Create an android app for database creation using.pptx
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
 
Show loader to open url in web view
Show loader to open url in web viewShow loader to open url in web view
Show loader to open url in web view
 
Social Aggregator Paper
Social Aggregator PaperSocial Aggregator Paper
Social Aggregator Paper
 
Recovered file 1
Recovered file 1Recovered file 1
Recovered file 1
 
Asp net interview_questions
Asp net interview_questionsAsp net interview_questions
Asp net interview_questions
 
Level 1 &amp; 2
Level 1 &amp; 2Level 1 &amp; 2
Level 1 &amp; 2
 
Level 3
Level 3Level 3
Level 3
 
Level 4
Level 4Level 4
Level 4
 
BioJS specification document
BioJS specification documentBioJS specification document
BioJS specification document
 
Cloud as a GIFT: Exploiting Personal Cloud Free Accounts via Rest APIs
Cloud as a GIFT: Exploiting Personal Cloud Free Accounts via Rest APIsCloud as a GIFT: Exploiting Personal Cloud Free Accounts via Rest APIs
Cloud as a GIFT: Exploiting Personal Cloud Free Accounts via Rest APIs
 
ODI User and Security
ODI User and Security ODI User and Security
ODI User and Security
 
Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentals
 
Ado.net
Ado.netAdo.net
Ado.net
 
Android adapters
Android adaptersAndroid adapters
Android adapters
 
Programming web application
Programming web applicationProgramming web application
Programming web application
 

Ähnlich wie Android Insights - 3 [Content Providers]

Chapter 3 - Syndicating Content
Chapter 3 - Syndicating ContentChapter 3 - Syndicating Content
Chapter 3 - Syndicating Content
kwduncan
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
milerac3
 

Ähnlich wie Android Insights - 3 [Content Providers] (20)

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
 
Bn1 1020 demo android
Bn1 1020 demo  androidBn1 1020 demo  android
Bn1 1020 demo android
 
Android session 4-behestee
Android session 4-behesteeAndroid session 4-behestee
Android session 4-behestee
 
Data Transfer between Activities & Databases
Data Transfer between Activities & DatabasesData Transfer between Activities & Databases
Data Transfer between Activities & Databases
 
LoCloud - D3.6: Wikimedia Application
LoCloud - D3.6: Wikimedia ApplicationLoCloud - D3.6: Wikimedia Application
LoCloud - D3.6: Wikimedia Application
 
Data Transfer between Activities & Databases
Data Transfer between Activities & DatabasesData Transfer between Activities & Databases
Data Transfer between Activities & Databases
 
Android application development fundamentals
Android application development fundamentalsAndroid application development fundamentals
Android application development fundamentals
 
Mobile web development
Mobile web developmentMobile web development
Mobile web development
 
Chemread – a chemical informant
Chemread – a chemical informantChemread – a chemical informant
Chemread – a chemical informant
 
10-Step Methodology to Building a Single View with MongoDB
10-Step Methodology to Building a Single View with MongoDB10-Step Methodology to Building a Single View with MongoDB
10-Step Methodology to Building a Single View with MongoDB
 
Fun Food
Fun FoodFun Food
Fun Food
 
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
 
Chapter 3 - Syndicating Content
Chapter 3 - Syndicating ContentChapter 3 - Syndicating Content
Chapter 3 - Syndicating Content
 
People aggregator
People aggregatorPeople aggregator
People aggregator
 
Content provider
Content providerContent provider
Content provider
 
03 android application structure
03 android application structure03 android application structure
03 android application structure
 
Best android classes in mumbai
Best android classes in mumbaiBest android classes in mumbai
Best android classes in mumbai
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
 
Sec presentation
Sec presentationSec presentation
Sec presentation
 
KMS (1)
KMS (1)KMS (1)
KMS (1)
 

Mehr von Sharafat Ibn Mollah Mosharraf

Mehr von Sharafat Ibn Mollah Mosharraf (7)

GRP GRM Development Progress Presentation
GRP GRM Development Progress PresentationGRP GRM Development Progress Presentation
GRP GRM Development Progress Presentation
 
Manners Matter: Learning Professional Etiquettes
Manners Matter: Learning Professional EtiquettesManners Matter: Learning Professional Etiquettes
Manners Matter: Learning Professional Etiquettes
 
SQL
SQLSQL
SQL
 
Hibernate collection & association mapping
Hibernate collection & association mappingHibernate collection & association mapping
Hibernate collection & association mapping
 
Effective Learning (& Teaching)
Effective Learning (& Teaching)Effective Learning (& Teaching)
Effective Learning (& Teaching)
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Android Insights - 1 [Intents]
Android Insights - 1 [Intents]Android Insights - 1 [Intents]
Android Insights - 1 [Intents]
 

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
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
Safe Software
 
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
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
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...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
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
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
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
 
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
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 

Android Insights - 3 [Content Providers]

  • 1. Android Insights - 3 Content Providers
  • 2. Topics to be Covered • Content Provider Basics • Accessing a Content Provider • Developing a Custom Content Provider 2/11/2012 Android Insights - 3 2/18
  • 3. Content Provider • A content provider manages access to a central repository of data.. • The provider is part of an Android application, which often provides its own UI for working with the data. • However, content providers are primarily intended to be used by other applications, which access the provider using a provider client object. • Together, providers and provider clients offer a consistent, standard interface to data that also handles inter-process communication and secure data access. 2/11/2012 Android Insights - 3 3/18
  • 4. Accessing a Content Provider • A content provider offers methods which correspond to the basic CRUD functions of persistent storage. • An application accesses the data from a content provider with a ContentResolver client object. This object has methods that call identically- named methods in the provider object. • A content provider is identified by a content URI. 2/11/2012 Android Insights - 3 4/18
  • 5. Accessing a Content Provider... • Example of getting a list of words from the User Dictionary provider: • The content URI of the words table is: content://user_dictionary/words • Read permission for accessing the content provider is also needed in the manifest file: 2/11/2012 Android Insights - 3 5/18
  • 6. Developing a Custom Content Provider 1. Extend the ContentProvider class. 2. In the onCreate() method, create a new instance of the database helper class. 2/11/2012 Android Insights - 3 6/18
  • 7. Developing a 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) 2/11/2012 Android Insights - 3 7/18
  • 8. Developing a Custom Content Provider... 2/11/2012 Android Insights - 3 8/18
  • 9. Developing a 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. 2/11/2012 Android Insights - 3 9/18
  • 10. Developing a Custom Content Provider... 2/11/2012 Android Insights - 3 10/18
  • 11. Developing a 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. 2/11/2012 Android Insights - 3 11/18
  • 12. Developing a 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. 2/11/2012 Android Insights - 3 12/18
  • 13. Developing a 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 common types of data such as as text, HTML, or JPEG, getType() should return the standard MIME type for that data. – For content URIs that point to a row or rows of table data, getType() should return a MIME type in Android's 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 company's name or some part of your application's Android package name. – A good choice for the <type> is a string that identifies the table associated with the URI. 2/11/2012 Android Insights - 3 13/18
  • 14. Developing a Custom Content Provider... • Content type defined in the contract class: 2/11/2012 Android Insights - 3 14/18
  • 15. Developing a Custom Content Provider... • getType() method in the provider class: 2/11/2012 Android Insights - 3 15/18
  • 16. Developing a Custom Content Provider... 9. We need to declare the provider in the manifest.xml file: 2/11/2012 Android Insights - 3 16/18
  • 17. Developing a 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. • 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. • Temporary permission – A permission level that grants temporary access to an application, even if the application doesn't have the permissions that are normally required. 2/11/2012 Android Insights - 3 17/18
  • 18. Developing a Custom Content Provider... • Permission defined in manifest.xml of the provider: • Permission defined in manifest.xml of the user: 2/11/2012 Android Insights - 3 18/18