Figure 7-3: The New Java Class dialog box.
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 New⇒Class.
The New Java Class dialog box opens, as shown in Figure 7-3.

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access