Working with App Widgets in Android

A Home screen widget (or app widget) in Android is a special kind of view that can be embedded on your device’s Home screen. An app widget can accept user input via click events, and it can update itself regularly. A user can add an app widget to the Home screen by tapping the Applications button and then selecting Widgets. The result is shown in Figure 7-1.

Figure 7-1: Adding a widget to the Home screen.

9781118417454-fg0701.eps

To make the Silent Mode Toggle application more usable, build a Home screen widget for it so that users can add the widget to the Home screen. Tapping the widget changes the phone’s ringer mode automatically without having to open the application. The widget also updates its layout to indicate what state the phone is in, as shown in Figure 7-2.

Figure 7-2: The two states of the app widget.

9781118417454-fg0702.eps

Working with remote views

When you develop apps in Android, remember that it’s based on the Linux 2.6 kernel. Linux comes supplied with its own idioms (or “dialect”) about security, and the Android platform inherits them. For example, the Android security model is heavily based around the Linux user, file, and process security model.

remember.eps Because every Android application is (usually) associated with its own unique user, Android prevents applications from modifying the files of other applications. This prevents developers from attempting to inject malicious code into other apps.

Because the Home screen is its own application and thus has its own unique user, developers such as yourself aren’t allowed to directly run your application code on the Home screen for safety reasons. To provide a way to access the Home screen and modify the contents of a particular area on it from an application, the Android developers implemented the RemoteViews architecture: It lets you run code inside your application, in a separate process from the Home screen application, but it still allows a widget’s view to be updated on the Home screen. The result is that you can still have your widget but no arbitrary code needs to be run inside the Home screen application — all your app widget code runs within your application.

Suppose that a user taps the Home screen app widget (in this case, an icon she added to the Home screen). This action sends a request — addressed to your application — to change the ringer mode. Android routes the request to your application, and the application processes the request, instructing the Android platform to change the ringer mode and update the app widget on the Home screen with a new image. None of this code is run in the Home screen application — it’s all run remotely in your application, with Android messaging routing the message to the appropriate application.

A remote view combines a little magic with innovative engineering. Known as the RemoteViews class on the Android platform, it allows your application to programmatically supply a remote user interface to the Home screen in another process. The app widget code isn’t an actual activity (as in earlier chapters), but is an implementation of an AppWidgetProvider. When Android routes a message (as described in the preceding paragraph) to your application from the Home screen, the message is handled in your implementation of the AppWidgetProvider class.

Using AppWidgetProviders

The AppWidgetProvider class allows the developer to programmatically interact with the app widget on the Home screen. When this interaction takes place, messages are sent from the Home screen app widget to your application via broadcast events. Using these broadcast events, you can respond when the app widget is updated, enabled, disabled, or deleted. You can also update the look and feel of the app widget on the Home screen by providing a new view. Because this view is located on the Home screen and not within your running application, you use RemoteViews to update the Home screen layout. All the logic that determines what should happen is initiated via an implementation of AppWidgetProvider.

Picture the app widget framework as the translator of a conversation between two entities. If you need to speak to someone who knows Italian, but you don’t know how to speak Italian, you would find a translator who would accept your input, translate it into Italian, and relay the message to the native Italian speaker. The same process applies to the app widget framework: This framework is your translator.

When the Italian native (the Home screen, in this case) needs to let you know that something has happened (such as a user tapping a button), the translator (the app widget framework in the Android system) translates the action into a message that you can understand (tapping a particular button). At that time, you can respond with the action you want to take (such as change the app widget background color to lime green), and the translator (the app widget framework) relays the message to the native Italian speaker (to the Home screen via the Android system). The Home screen updates the background color of the view.

remember.eps App widgets can only accept input from tap-type events. When you’re working within an app widget, you have no access to other basic input views, such as an editable text box or drop-down lists.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset