This article translated from: http://developer.android.com/guide/topics/appwidgets/index.html
APP widgets is a small application window that can be embedded into other applications (such as the main screen window) and can accept periodic updates. These small windows can be used as a visual component in the user interface, and these visual components can also have their own app widget providers. Widgets that can hold other app widgets are called app widget owners. The following is the music app widget.
This article describes how to publish an app widget that uses the app widget provider. For creating your own appwidgethost to hold appwidgets, see appwidget
Host.
Widget Design
Read the widget Design Guide for how to design visual components for applications.
Basic
The following are the basic information for creating an app Widget:
1. appwidgetproviderinfo object
This object is used to describe the metadata of APP widgets, such as layout, update cycle, and appwidgetprovider class. It should be defined in the XML file.
2. Implement the appwidgetproder class
This class defines some basic broadcast-based programming interface methods of APP Widgets. Through this class, when app widgets are updated, enabled, disabled, and deleted, you will receive a broadcast event.
3. Window Layout
Initialize the layout of the app widget in XML.
In addition, you can implement an activity for app widget configuration. This is an optional activity that starts when the user adds the app widget and allows the user to edit the settings of the app widget during creation.
The following describes how to create an app widget.
Declare An appwidget in the list
First, declare the appwidgetprovider class in androidmanifest. xml of your application, for example:
<Receiverandroid: Name = "exampleappwidgetprovider">
<Intent-filter>
<Action Android: Name = "android. appwidget. Action. appwidget_update"/>
</Intent-filter>
<Meta-data Android: Name = "android. appwidget. provider"
Android: Resource = "@ XML/example_appwidget_info"/>
</Cycler>
The <Cycler> element needs to set the Android: Name attribute, which specifies the app
The appwidgetprovider class used by the widget.
The <intent-filter> element must contain an <action> element with the Android: Name attribute. This attribute specifies that the appwidgetprovider object will receive broadcasts of the action_appwidget_update type. This is the only broadcast you need to explicitly declare. If necessary, appwidgetmanager automatically adds all other apps
The widget broadcast is sent to appwidgetprovider.
The <meta-data> element specifies the resources required by the appwidgetproviderinfo object and the following attributes:
Android: Name-specifies the metadata name. Use Android. appwidget. provider as the descriptor of appwidgetproviderinof to identify data.
Android: Resource --- specifies the location of the appwidgetproviderinfo resource.
Add appwidgetproviderinfo metadata
Appwidgetproviderinfo defines the basic features of an app widget, such as its minimum layout size, initial layout resources, how to update an app widget, and an optional activity to be set when it is created. Use the <appwidget-provider> element to define the appwidgetproviderinfo object in an XML resource and save the resource in the Res/XML folder of the project.
For example:
<Appwidget-providerxmlns: Android = "http://schemas.android.com/apk/res/android"
Android: minwidth = "40dp"
Android: minheight = "40dp"
Android: updateperiodmillis = "86400000"
Android: previewimage = "@ drawable/preview"
Android: initiallayout = "@ layout/example_appwidget"
Android: Configure = "com. example. Android. exampleappwidgetconfigure"
Android: resizemode = "Horizontal | vertical"
Android: widgetcategory = "home_screen | keyguard"
Android: initialkeyguardlayout = "@ layout/example_keyguard">
</Appwidget-provider>
The following is an overview of <appwidget-provider> element attributes
The minwidth and minheight attribute values specify the default size of space occupied by appwidget. The size of the app widget in the main screen window is based on the height and width defined by a grid in the main screen. If the minimum width or height of an app widget does not match the grid size, the size of the app widget is rounded to the nearest cell size. For more information about the size of APP widgets, see appwidget
Design Guide.
Note: To adapt your app widget to more devices, the minimum size of the widget should not be larger than 4x4 cells.
The minresizewidth and minresizeheight attributes specify the absolute minimum size of the appwidget. These two values should be used when the size of the app widget is not determined or not used. These two attributes allow you to re-adjust the widget size. Their values can be smaller than the default widget size value defined by the minwidth and minheight attributes. They are introduced in android3.1.
For more information about how to adjust the size of an app widget, see the app widget design guide.
The updateperiodmillis attribute defines the update frequency of the appwidget framework. app widgets call the onupdate () callback method to request updates from appwidgetprovider. The actual update does not ensure that the time cycle defined by this value is accurate, and we recommend that you minimize the update frequency as much as possible-it is best not to exceed 1 hour, in order to save battery power. You can also configure to allow users to adjust the update frequency. Some people may want to view the stock price every 15 minutes or only four times a day.
NOTE: If an update occurs when the device is sleeping, the device will be awakened to perform the update. If the number of updates per hour is no more than one, the battery life will not be significantly affected. However, if you want to frequently and (or) do not need to update the device during sleep, you can use an alarm instead and it will not wake up the device. Use the alarmmanager object to set an intent object that your appwidgetprovider can receive for the alarm. The set alarm types can be elapsed_realtime or RTC. They only send alarms when the device is awake. Set the updateperiodmillis attribute to 0.
The initiallayout attribute specifies the layout resource that defines the layout of the app widget.
The Configure attribute defines the activity to be started when you add an app widget to set the appwidget attribute. This is optional.
The previewimage attribute specifies the preview image of the app widget after configuration, so that the user can first see the appearance when selecting the app widget. If this attribute is not set, the user will only see the startup icon of the application. This attribute corresponds to the Android: previewimage attribute in the <javaser> element of the androidmanifest. xml file. For more information about how to use previewimage, see setting preview images. This attribute is introduced in android3.0.
The autoadvanceviewid attribute specifies the ID of the Child view of the appwidget. It should be automatically generated by the owner of the widget, which is introduced in android3.0.
The resizemode attribute specifies the widget size adjustment rule. This property allows the widget on the home screen to be resized. You can hold down a widget to display a resize handle, and then drag the handle horizontally or vertically to change the widget size. The attribute values include horizontal, vertical, and none. To adjust both the horizontal and vertical dimensions, set the attribute value to "Horizontal | vertical ". This attribute is introduced in android3.1.
The minresizeheight attribute specifies the minimum height (in DPS) that the widget can adjust ). If the value is greater than the value of minheight, or the vertical size cannot be adjusted, this attribute is invalid. This attribute is introduced in android4.0.
The minresizewidth attribute specifies the minimum widget width (in DPS) that can be adjusted ). If the value is greater than the value of minwidth, or the horizontal size cannot be adjusted, this attribute is invalid. This attribute is introduced in android4.0.
The widgetcategory attribute declares whether your app widget can be displayed on the home screen or locked screen, or on both screens. Its Attribute values include home_screen and keyguard. To make widgets appear on this screen at the same time, make sure that the widget class complies with their design guide. For more information, see "Let app
Widgets can be displayed on the screen lock ". The default value is "home_screen ". This attribute is introduced in android4.2.
The initialkeyguardlayout attribute points to a layout resource, which defines the layout of the screen lock app widget. This attribute works in the same way as the Android: initiallayout attribute. It provides a layout that can be displayed immediately until the app widget is initialized and can update the layout. This attribute is introduced in android4.2.
For more information about attributes that the <appwidget-provider> element can receive, see the appwidgetproviderinfo class.