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

Chapter 7: Turning Your Application into a Home-Screen Widget

Figure 7-2: The two states of the app widget you’re about to build. Silent mode is enabled. Phone is in regular mode. Working with remote views When dealing with Android, you should remember that Android is based on the Linux 2.6 kernel. Linux comes with some of its very own idioms about security, and the Android platform inherits those idioms. For example, the Android security model is heavily based around the Linux user, file, and process security model. Each application in Android is usually associated with a unique user ID. All processes run under a particular user. This prevents one application from modifying the files of another application — which could result in a malicious developer injecting code into another app. Because the home screen is actually an application that is currently running on the Android device — hosted by the Android system, not you — it would not be feasible to allow you as a developer to modify actual running code on the home screen because a malicious developer could do some really evil things, such as shut down your home screen. How would you use your phone then? This is a big issue. The Android engineers still wanted to give you a way to access the home screen and modify the contents of a particular area of the home screen from your app- lication. Therefore, they decided to handle the problem by implementing what is known as the RemoteView architecture. This architecture allows you to run code inside your application, completely away from the home screen applica- tion, but still allowing you to update a view inside the home screen. The end result is that no arbitrary code can be run inside the home screen application — all your app widget code runs within your application. This app widget stuff may sound confusing, but imagine it like this: The user taps the home-screen app widget in this case, an icon on the home screen that he added. This action fires off a request to change the ringer mode. This request is addressed to your application. Android routes that request to your application, and it processes the request. During the processing of that request, your application instructs the Android platform to change the ringer mode as well as update the app widget on the home screen with a new image to indicate that the ringer mode has been changed. None of this code was run