Choose the Extract Android String option.
Chapter 7
Turning Your Application into a Home-Screen Widget
In This Chapter
▶
Seeing how app widgets work in Android
▶
Understanding pending intents
▶
Building an App Widget Provider
▶
Putting your widget on the home screen
U
sability is the name of the game in regard to all disciples of application development. When it comes down to it, if your application is not usable,
users will not use it. It’s that simple. You’ve built the Silent Mode Toggle application, and it works great and it’s very
usable. Unfortunately, if this application were published to the Android Market, the application would not be very popular. Why? In short, the user is required
to open the app and then click a button to silence the phone. If the user has not created a home-screen shortcut to the application, and the app is buried
in the application launcher with thirty other applications, that means taking a few extra steps: unlocking the phone, opening the launcher, finding the appli-
cation, opening the app, and then clicking the Silent button. At this point, the user might as well use the up and down volume keys found on most phones to
silence the phone. Pressing the down volume key numerous times results in the phone eventually being set into silent mode. Therefore, the application’s
usability is not very good. So how would you make this application more usable and feasible for the end user? Simple: Turn it into a home-screen widget.
In this chapter, I demonstrate how to build a home-screen widget for your application. App widgets normally resemble small icons or very small views
that exist on your home screen. This widget allows users to interact with your application by simply tapping an icon the home-screen widget. When
you tap this icon, core functionality kicks in and toggles the silent mode for the user. In this chapter, you are introduced to the following classes:
164
Part II: Building and Publishing Your First Android Application
✓ The Intent
✓ The BroadcastReceiver
✓ The AppWidgetProvider
✓ The IntentService
✓ The AppWidgetProviderInfo
Each of these classes plays a vital role in Android as well as in the app widget framework.
Working with App Widgets in Android
Home-screen widgets in Android are miniature applications that can be embedded within other applications such as the home screen. These are also
known as app widgets. These app widgets can accept user input through click events and can update themselves on a regular schedule. App widgets are
applied to the home screen by long-pressing pressing on the screen for a couple of seconds and then selecting widgets, as shown in Figure 7-1.
Figure 7-1:
The dialog box that
shows up after you
long-press the home
screen.
To make the Silent Mode Toggle application more usable, I’m going to show you how to build a home-screen widget for the application so that users can
add it to their home screen. After adding the widget, the user can tap it — this will change the phone’s ringer mode without having to open the application.
The widget also updates its layout to inform the user what state the phone is in, as shown in Figure 7-2.
165