Creating the Home Screen Widget

The process of sending messages between the Home screen app widget and your application is handled via the Android messaging system, the PendingIntent class, and the AppWidgetProvider. In this section, you build each component to get your first app widget up and running on the Home screen.

Implementing the AppWidgetProvider

Implementing the AppWidgetProvider is fairly straightforward: Open Eclipse and open the Silent Mode Toggle application.

To add a new class to the com.dummies.android.silentmodetoggle package and provide a name, such as AppWidget.java, follow these steps:

1. Right-click com.dummies.android.silentmodetoggle in the src/ folder and choose NewClass.

The New Java Class dialog box opens, as shown in Figure 7-3.

Figure 7-3: The New Java Class dialog box.

9781118417454-fg0703.eps

2. Provide the name of the class and set its superclass to android.app widget.AppWidgetProvider .

3. Click Finish when you’re done.

The new class is added to the selected package.

The AppWidgetProvider does all the work of responding to events from the RemoteViews, but how so? If you look at the AppWidgetProvider Android documentation, you can see that it’s a direct subclass of BroadcastReceiver. At a high level, a BroadcastReceiver is a component that can receive broadcast messages from the Android system. When a user taps a clickable view in the RemoteViews on the Home screen ...

Get Android Application Development For Dummies, 2nd Edition 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.