Creating the App Widget

A lot is going on when it comes to interacting with an app widget. The process of sending messages between the home app widget and your application is handled through the Android messaging system, the PendingIntent class, and the AppWidgetProvider. In this section, I demonstrate how to build each component so that you can get your first app widget up and running on your Home screen.

Implementing the AppWidgetProvider

Implementing the AppWidgetProvider is a straightforward process. Just follow these steps:

  1. Open Eclipse and open the Screen Brightness Toggle application.
  2. Add a new class to the com.dummies.android.screenbrightnesstoggle package and provide a name; I prefer to use AppWidget.java.

    To add a new class, right-click com.dummies.android.screenbrightnesstoggle in the src/ folder and choose NewimageClass. This opens the New Java Class dialog box. In this dialog box, provide the name of the class and set its superclass to android.appwidget.AppWidgetProvider, as shown in Figure 8-3.

  3. Click Finish.

    A new class has been added to the selected package, and the code with the name you chose should now be visible.

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

    image

Communicating with the app widget

Right now, your AppWidgetProvider class has no code in it — it's an empty shell. ...

Get Android™ Tablet Application Development For Dummies® now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.