concept appwidgetprovider in category android

appears as: AppWidgetProvider, n AppWidgetProvider, The AppWidgetProvider, AppWidgetProviders, AppWidgetProvider
Android in Action, Third Edition

This is an excerpt from Manning's book Android in Action, Third Edition.

An AppWidget runs under another application, namely an AppWidgetHost, which is typically the device’s home screen. The AppWidget code is implemented in an instance of an AppWidgetProvider, which is an extension of the BroadcastReceiver class. Recall from prior chapters that a BroadcastReceiver is defined as a receiver in the AndroidManifest.xml file. The AppWidgetProvider is a BroadcastReceiver with a special IntentFilter and a metadata tag that further defines the AppWidgetProvider’s characteristics. An AppWidget may be implemented in code as a BroadcastReceiver alone, yet the AppWidgetProvider provides some convenience wrapper functionality and is the recommended means of coding an AppWidget.

AppWidgets are designed to be updated periodically. The stock implementation of an AppWidget automatically updates at an interval defined by the developer at design time. In general, this update is kept to a low frequency to conserve battery power. There are other mechanisms for updating an AppWidget on an as-needed basis through the use of Intents. The AppWidgetProvider extends BroadcastReceiver and therefore can receive different Intent Actions based on the defined IntentFilters. The common practice is to define an application-specific IntentFilter action and use the sendBroadcast() method to trigger an AppWidget update on an as-needed basis.

The details of the AppWidgetProvider, the special metadata in the AndroidManifest, IntentFilters, RemoteViews, and much more are all discussed in this chapter. Before we delve into the details of constructing an AppWidget, let’s consider the various design patterns an AppWidget can satisfy.

17.5. Implementing the AppWidgetProvider

The AppWidgetProvider for the SiteMonitor application is implemented in the file SiteMonitorWidgetImpl.java. The AppWidgetProvider is responsible for handling updates to the UI as well as responding to housekeeping events related to the AppWidget lifecycle, and is arguably the most important aspect of AppWidget programming to understand. Because of its centrality and importance to working with AppWidgets, we’re going to look at the code from two perspectives.