Londroid Android Home Screen Widgets - Presentation Transcript
Android Home Screen Widgets London Android User Group 22 nd June 2009 Richard Hyndman
Widgets
BatteryWidget
If you want to follow along I’ll be using
BatteryWidget as an example:
http://batterywidget.googlecode.com Or http://geekyouup.com/batterywidget.zip
How?
To create an App Widget, you need 3 things:
AppWidgetProviderInfo xml
Describes the metadata for an App Widget, such as layout, update frequency, and the AppWidgetProvider class. Defined in XML.
AppWidgetProvider class implementation
Defines the basic methods that allow you to programmatically interface with the App Widget, based on broadcast events. Through it, you will receive broadcasts when the App Widget is updated, enabled, disabled and deleted.
View layout xml
Defines the initial layout for the App Widget, defined in XML.
The AppWidgetProvider This tells Android O/S that your application includes a Widget and wants updates, also that the widget is defined in the file xml/widget_def.xml Declare the AppWidgetProvider class in your application's AndroidManifest.xml file.
AppWidgetProviderInfo
XML file that defines the widget size and update frequency
BatteryWidget/res/xml/widget_def.xml
To calculate sizes: Minimum size in dip = (Number of cells * 74dip) - 2dip
initalLayout: The layout resource xml that is used when the widget loads
Don’t update more than necessary, battery life issues
At the update interval, your AppWidgetProvider.onUpdate() method is called
What is a DIP?
Android documentation mentions dip’s or dp’s instead of pixels for layout, but what it is a DIP?
One DIP is one pixel on a 160 dpi screen
(for example a 240x320, 1.5"x2" screen is approx 1dip=1px)
Problem: When your Widgets AppWidgetProvider.onUpdate(..) method is called the response is subject to the ANR (Application Not Responding) timer. If your time runs out then the user gets the “Force Close or Wait” dialog, this is not good.
Solution: If you need some time, start a service to process your update. It is much easier than you may think. A service just needs an onStart() method. (see BatteryWidget.java UpdateService inner class)
RemoteViews
Problem: If Widget views aren’t dynamic and have no associated Activity, how can you update them? How can the BatteryWidget’s percentage change?
Solution: RemoteViews!
When an event triggers an update, you can attach to the Widget’s view remotely and update it using the RemoteViews methods
Widget Interactions
Problem: If the Widget has no associated Activity, how can you make it interactive?
Solution: PendingIntents!
When using RemoteViews to update the widget layout, you can set PendingIntents on elements. For example using setOnClickPendingIntent() on the BatteryWidget starts an Activity
Touch
BatteryWidget Events
For the sake of efficiency and good coding practise the BatteryWidget does not poll the battery state. It registers as a Receiver for the Intent: Intent. ACTION_BATTERY_CHANGED
This intent can only be registered for in code, not as usual in the AndroidManifest.xml
When the intent fires in BatteryInfo.java it includes extra data: intent.getIntExtra("level", 0) intent.getIntExtra("status“) which describe the current charge and charging states
Display / GPS / Wifi
Display Settings
Intent defineIntent2 = new Intent("com.android.settings.DISPLAY_SETTINGS");
0 comments
Post a comment