Android App Widget

Android app widgets are often called widget by Android users, but different from widgets for developers which are visual components used to make apps.

  • One fundamental idea of Android is loose coupling.  This is reflected in Android app widgets.  The components involved in a widget are very loosely coupled.
  • Usually an app widget consists of two components: a subclass of AppWidgetProvider and an app widget configuration activity which is just like any other activities; five new files: two java files for the subclass of AppWidgetProvider and app widget configuration activity, and two layout files for them, one file for AppWidgetProviderInfo
  • The most confusing part is the widget adding and updating process. When a widget is added, it seems that onUpdate of AppWidgetProvider is called before the configuration activity is started.  Apparently onUpdate usually cannot do anything useful before the configuration is finished.  The configuration activity usually needs to use PendingIntent to update the widget (it cannot invoke onUpdate directly). This almost always leads to a static updating method used by both the configuration activity and onUpdate.
  • onUpdate is given only a few seconds to finish its work, so anything lengthy work should be done in a subclass of Service. This service class runs on the UI thread, so any network process should be put into a separate background thread.  In many cases, the bulk of the work is done in this service class.
  • All the newly created files should be in the same package as the app, so all the preferences and private files are available to the widget components.