O'Reilly logo

Android Application Development For Dummies, 2nd Edition by Donn Felker, Michael Burton

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

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 ...

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