Chapter 5. Content Providers
5.1. Introduction: Content Providers
Discussion
The content provider is one of Android’s more clever ideas. It allows totally unrelated applications to share data, which is usually stored in an SQLite database, without prior arrangement, knowing only the names of the tables and fields in the data.
One widely used content provider is the Android Contacts provider. The first recipe in this chapter shows how easy it is to make an initial selection of data (this is done using an intent, as you might guess, but it returns a URI, not the actual data). You then drill down using an SQLite cursor or two.
Then we have a recipe that shows you how to create your own content provider. Again as you might expect, “there’s an interface for that.”
Finally, while it’s not directly related to content providers, Android also offers a more general remote procedure mechanism layered on AIDL (the Android Interface Definition Language), and the recipe for that is at the end of this chapter because it’s a similar topic.
5.2. Retrieving Data from a Content Provider
Problem
You want to read from a content provider such as Contacts.
Solution
Create a PICK
URI, open it in an intent
using startActivityForResult
, extract
the URI from the returned intent, use
Activity.getContentProvider()
, and process the data using SQLite Cursor
methods.
Discussion
This is part of the contact selection code from TabbyText, my SMS text message sender for WiFi-Only Honeycomb tablets (the rest ...
Get Android Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.