SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

Android Training Session 1
Android Training Session 1Android Training Session 1
Android Training Session 1Shanmugapriya D
 
Android Trainning Session 2
Android Trainning  Session 2Android Trainning  Session 2
Android Trainning Session 2Shanmugapriya D
 
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 ProviderAvinash Sinha
 
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.pptxvishal choudhary
 
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 ListViewsAhsanul Karim
 
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 viewAravindharamanan S
 
Recovered file 1
Recovered file 1Recovered file 1
Recovered file 1Uthara Iyer
 
Asp net interview_questions
Asp net interview_questionsAsp net interview_questions
Asp net interview_questionsBilam
 
BioJS specification document
BioJS specification documentBioJS specification document
BioJS specification documentRafael C. Jimenez
 
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 APIsNishant Kumar
 
Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentalsMadhuri Kavade
 
Programming web application
Programming web applicationProgramming web application
Programming web applicationaspnet123
 

La actualidad más candente (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
 

Similar a Android Insights - 3 [Content Providers]

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
 
Android session 4-behestee
Android session 4-behesteeAndroid session 4-behestee
Android session 4-behesteeHussain Behestee
 
Data Transfer between Activities & Databases
Data Transfer between Activities & DatabasesData Transfer between Activities & Databases
Data Transfer between Activities & DatabasesMuhammad Sajid
 
LoCloud - D3.6: Wikimedia Application
LoCloud - D3.6: Wikimedia ApplicationLoCloud - D3.6: Wikimedia Application
LoCloud - D3.6: Wikimedia Applicationlocloud
 
Data Transfer between Activities & Databases
Data Transfer between Activities & DatabasesData Transfer between Activities & Databases
Data Transfer between Activities & DatabasesMuhammad Sajid
 
Android application development fundamentals
Android application development fundamentalsAndroid application development fundamentals
Android application development fundamentalsindiangarg
 
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 MongoDBMat Keep
 
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?DicodingEvent
 
Chapter 3 - Syndicating Content
Chapter 3 - Syndicating ContentChapter 3 - Syndicating Content
Chapter 3 - Syndicating Contentkwduncan
 
03 android application structure
03 android application structure03 android application structure
03 android application structureSokngim Sa
 
Chapter 03
Chapter 03Chapter 03
Chapter 03milerac3
 

Similar a 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)
 

Más de 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]
 

Último

[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 

Último (20)

[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 

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