O'Reilly logo

Learning Android by Marko Gargenta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Content Providers Through Widgets

As mentioned before, content providers make the most sense when you want to expose the data to other applications. It is a good practice to always think of your application as part of a larger Android ecosystem and, as such, a potential provider of useful data to other applications.

To demonstrate how content providers can be useful, we’ll create a Home screen widget. We’re not using the term widget here as a synonym for Android’s View class, but as a useful embedded service offered by the Home screen.

Android typically ships with a few Home screen widgets. You can access them by going to your Home screen, long-pressing on it to pull up an Add to Home Screen dialog, and choosing Widgets. Widgets that come with Android include Alarm Clock, Picture Frame, Power Controls, Music, and Search. Our goal is to create our own Yamba widget that the user will be able to add to the Home screen.

The Yamba widget will be simple, displaying just the latest status update. To create it, we’ll make a new YambaWidget class that subclasses AppWidgetProviderInfo. We’ll also have to register the widget with the manifest file.

Implementing the YambaWidget class

YambaWidget is the main class for our widget. It is a subclass of AppWidgetProvider, a special system class that makes widgets. This class itself is a subclass of BroadcastReceiver, so our Yamba widget is a broadcast receiver automatically. Basically, whenever our widget is updated, deleted, enabled, or disabled, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required