Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Custom content provider in android

Store and retrieve data and make it accessible to all applications
Only way to share data across applications
There's no common storage area that all Android packages can access
Two types of content providers
Android's built-in content providers
Custom content providers we create
Store and retrieve data and make it accessible to all applications
Only way to share data across applications
There's no common storage area that all Android packages can access
Two types of content providers
Android's built-in content providers
Custom content providers we create
Store and retrieve data and make it accessible to all applications
Only way to share data across applications
There's no common storage area that all Android packages can access
Two types of content providers
Android's built-in content providers
Custom content providers we create
• Android ships with a number of content providers for common data types
audio, video, images, personal contact information, and so on
android.provider package
• We can query these providers for the data they contain

  • Soyez le premier à commenter

Custom content provider in android

  1. 1. Android Content Provider
  2. 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. 3. Why? • Sometimes it is required to share data across applications. This is where content providers become very useful.
  4. 4. Content Provider
  5. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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.
  15. 15. Custom Content Provider
  16. 16. Custom Content Provider
  17. 17. Custom Content Provider
  18. 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.
  19. 19. Custom Content Provider...
  20. 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)
  21. 21. Custom Content Provider...
  22. 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.
  23. 23. Custom Content Provider...
  24. 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. 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. 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.
  27. 27. Custom Content Provider...
  28. 28. Custom Content Provider...
  29. 29. Custom Content Provider... 9. We need to declare the provider in the manifest.xml file:
  30. 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. 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.
  32. 32. DEMO TIME
  33. 33. THANK YOU ☺

×