English subtitles

← Why Content Providers Matter - Developing Android Apps

Get Embed Code
10 Languages

Showing Revision 15 created 05/24/2016 by Udacity Robot.

  1. Before we go ahead and build a content provider
  2. you wouldn't be out of line to ask, well why.
  3. The simplest answer is that it allows you to
  4. share your data safely and efficiently across app boundaries by
  5. abstracting the underlying data source, be it SQLite like
  6. this or files or really anything else. So that other
  7. apps can access it without really needing to understand how
  8. you stored it. In fact, the calendar, SMS, and contacts
  9. APIs work that way, using shared content providers.
  10. We're going to share our weather database later, but if
  11. you're not planning to expose your app's data, you're
  12. probably thinking you can skip this bit, right? Well,
  13. almost, in a lot of cases you could,
  14. but really shouldn't. For example, in Sunshine we're using
  15. SQ Lite. But you could be storing data in
  16. files, dynamic run-time data or even just a different
  17. database library. By using content providers, it's easier for
  18. you to potentially switch out the data source and
  19. much easier for someone other than you to manage
  20. the UI layer code without them having to understand
  21. the depths of your data storage implementation. On the
  22. UI layer, it's a generic mechanism that returns cursors.
  23. The same of those returned by SQLite databases. So,
  24. if your data layer implementation changes, then your content provider
  25. is effected. Still, it's just you writing the code right
  26. now and that's a lot of boilerplate for the sake of
  27. following a neat design pattern. Well, keep in mind that
  28. as far as the framework is concerned, all data is handled
  29. through content providers. So, if you want to interact with
  30. anything outside of your app, such as sending data to a
  31. widget or returning search results from the newer app, you'll need
  32. a content provider for that too. In fact, that's how the
  33. Google play store and Gmail widgets work. As well as the
  34. ability to get search results from Google Play. Similarly, there's a bunch
  35. of APIs designed to optimize the process of synching and querying data,
  36. and updating UI accordingly. And all of them also expect content providers.
  37. That includes sync adapters and cursor loaders. Which make your
  38. app able to efficiently sync with your server, load data in
  39. your UI layer, and which include built in content observers that
  40. will update your UI automatically when the underlying data changes. You
  41. could, of course, build all of that yourself but at
  42. a certain point the advantage you gained by not writing a
  43. content provider to begin with is lost in the process of
  44. having to recreate all of the useful clusters that utilize it.
  45. We'll take a look at publishing your content provider and using Lotus
  46. to access it efficiently later in this lesson. And we'll explore sync adapters
  47. in lesson six when we look at doing efficient background updates. But
  48. first, Dan is going to show you how to actually build a content provider.