Open main menu

CDOT Wiki β

MAP524/DPS924 Lecture 8

Revision as of 21:37, 2 August 2015 by Andrew (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Content Providers

This week we're looking at another means of sharing data between Android apps, this one is usually used for well structured data with a stable API for accessing it. But first..

A look back at Intents

Most of you have only used an intent to start an activity, and haven't thought much about the Intent object. Today let's use a couple of features available with an intent, specifically for passing data to the activity being started and getting data back from it.

This tutorial is pretty good, it talks about how to use the Intent to pass data to and receive it in an Activity. Android Studio autocomplete will help you see what other types of data you can pass.

To get data back from an activity you started you need to use startActivityForResult() instead of startActivity(), see here for an explanation and example.

Using an existing content provider

Android ships with many useful content providers, including the following:

  • Browser - Stores data such as browser bookmarks, browser history, and so on
  • CallLog - Stores data such as missed calls, call details, and so on
  • Contacts - Stores contact details
  • MediaStore - Stores media files such as audio, video, and images
  • Settings - Stores the device ’ s settings and preferences

You should know you can use the above but we won't be writing code to query them in this course, it's much more complicated than it needs to be. Instead we'll create a new content provider and query that.

Implementing a content provider

Some overwhelming documentation here.

A content provider looks very much like an SQLiteOpenHelper we've used in our SQLite lecture/lab. It's so similar it's easy to confuse the two.

The difference is:

  • Methods in an SQLiteOpenHelper are called directly in code.
  • Methods in a content provider are called via a content:// url.

The two are such a good match that most of the time if you have a content provider - you'll have an SQLiteOpenHelper paired with it. Implementing all the methods required for a content provider from scratch is too much work.

So what we need to do is:

  • Start from the example from the SQLite week (you can start with your lab).
  • Add a class extending ContentProvider.
  • Implement the required methods (Android Studio will help with all this).
  • Our ContentProvider will use the query method from the SQLiteExample and do little else.
  • Note that the ContenProvider is mentioned in the manifest in the <provider> tag.

Implementing a content resolver

Some overwhelming documentation here. Creating a Uri withUri.parse.

A content resolver is just the client for the content provider. To demonstrate how this system is supposed to work we'll have the content resolver in a different appliction.

  • Create a brand new app, called "Client".
  • Add a button and a callback.
  • When the button is pressed - send a trivial query to the content provider.

We only have time to scratch the surface of this topic so we won't bother with all the different parameters, but it's important for us to know generally how a content provider works and is implemented.