Tuesday, 28 August 2012

Developing Android Widgets

Developing Android widget applications involves a slightly different process to creating standard apps. In this article we will go over the basics of what Android Widget development involves, outlining the main ways in which the task varies from other applications. If you already have experience developing a standard Android application, you will be able to approach the job of developing a widget app fairly easily. However, if you have not yet developed any Android applications, it is advisable to start by trying a standard app first rather than a widget app.

Launching and Running

Most Android apps are launched through the user device menu. Application shortcuts may be added to homescreens, with users launching the apps from the shortcut icons. However, widget apps are launched using a distinct process. When the user downloads a widget app from the Google Play store (formerly Android Market), the app installation process is exactly the same as for other apps. To run the widget, the user must add it to their device screen. Normally, this involves long-pressing (pressing and holding) an area of the homescreen. The user is then presented with options to add application shortcuts or widgets. Upon choosing widget, the user is presented with a list of those available, i.e. those widget applications they have installed.

When the user chooses a widget to add to the homescreen, the widget will be automatically added to the position they originally long-pressed. The user can then move the widget around the screen by pressing, holding and dragging it. Often, Android widgets allow users to choose configuration settings. This is particularly common with widget apps that update over time, sometimes fetching data over the Internet. In such cases you can provide users with the ability to set timing periods for these and other options, normally by pressing the widget while it is on the screen.

Developing

When you approach the task of developing Android widgets, there are a number of similarities and differences with other apps. Because the launch process is different, there are differences in your Manifest file, Resources directories and Java classes. For a widget app, you do not need to use a main Activity class to be launched when the widget runs. Instead, you provide a class that extends the "AppWidgetProvider" class. In addition to this, you need to declare this "AppWidgetProvider" class within your application Manifest file, in which you can define it as a receiver. In the Manifest, you also need to define metadata for the widget app, indicating an XML resource file in which you specify the basic properties of your widget.

The widget metadata is defined using the "AppWidgetProviderInfo" resource. Essentially, this involves creating a folder in your application resources directory named "xml" in which you save an XML file with the widget properties outlined in it. These properties include how often you want the widget to update, as well as default dimensions and a preview image. Within your "AppWidgetProvider" class, you can provide methods that will execute whenever the widget app updates, which will occur at the frequency you indicated in your XML metadata file. Within these methods, you can define the behaviour and appearance of your widget, optionally basing this on data fetched from elsewhere.

Rather than simply updating your widget apps at a fixed period, you can also set it up to receive system broadcast announcements. This is particularly useful for widgets that indicate information about the phone state, for example the current battery level. Some widget apps also use the "AlarmManager" class to register and receive announcements at custom periods. If a developer decides to allow the user to set configuration options for a widget app, they normally do this by implementing a click listener for the widget, detecting the user pressing it when it is on the homescreen and presenting a user interface for settings when this occurs.

Testing

Testing widgets is broadly similar to testing standard Android apps, although there are some differences depending on the purpose of the widget app in question. For example, when a widget is updating with information about the phone state, for example battery level, WiFi connectivity etc, you will not be able to carry out effective testing on the Android emulator. In such cases a widget app really needs to be tested on an actual Android device, as the device settings on virtual devices do not change in the same way as on real devices.

Conclusion

If you are thinking about attempting an Android widget application and have already developed one or more normal Android apps, you can easily make the required adjustments to your development approach. However, if you are thinking of a widget app as your first foray into Android development, you may have more success by focusing on a standard app first to give yourself a solid foundation on the platform.

Links:

No comments:

Post a Comment