Chapter 12. Content Providers

Content providers are Android building blocks that can expose data across the boundaries between application sandboxes. As you recall, each application in Android runs in its own process with its own permissions. This means that an application cannot see another app’s data. But sometimes you want to share data across applications. This is where content providers become very useful.

Take your contacts, for example. You might have a large database of contacts on your device, which you can view via the Contacts app as well as via the Dialer app. Some devices, such as HTC Android models, might even have multiple versions of the Contacts and Dialer apps. It would not make a lot of sense to have similar data live in multiple databases.

Content providers let you centralize content in one place and have many different applications access it as needed. In the case of the contacts on your phone, there is actually a ContactProvider application that contains a content provider, and other applications access the data via this interface. The interface itself is fairly simple: it has the same insert(), update(), delete(), and query() methods we saw in Chapter 9.

Android uses content providers quite a bit internally. In addition to contacts, your settings represent another example, as do all your bookmarks. All the media in the system is also registered with MediaStore, a content provider that dispenses images, music, and videos in your device.

Creating a Content Provider ...

Get Learning Android 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.