What is an app Widget?
Appwidget is an application widget that is a miniature application view that can be embedded into a desktop application and receive periodic updates.
How to implement Appwidget?
1. Create a new Appwidget class to inherit Appwidgetprovider (Appwidgetprovider inherit broadcastreceiver) and implement the related life cycle method.
OnUpdate ()
Executed when the widget is updated.
Note: OnUpdate () will also be called when the widget is first added by the user. However, if you define the Configure property of the widget (that is, android:config), OnUpdate () will not be called, and onUpdate () will not be called until the widget is updated.
Onappwidgetoptionschanged ()
This method is executed when the widget is first added or when the widget size is changed.
Ondeleted ()
Executes when the widget is deleted.
Onenabled ()
Executes when the first instance of the widget is created.
Ondisabled ()
Executes when the last widget instance is deleted.
Onreceiver ()
Executes when a broadcast is received.
2. Configuring the Appwidgetprovice implementation class in the manifest file
< receiver android:name="Com.itcast.weibo.ui.WeiboWidget"> < Intent-filter > < Action android:name="Android.appwidget.action.APPWIDGET_UPDATE"/> </ Intent-filter > < Meta-data android:name="Android.appwidget.provider" Android:resource="@xml/weibo_widget"/> </ receiver > |
3. Create a new XML folder in the Res directory and create a new XML description file for the widget
<? XML version="1.0"encoding="Utf-8"?> < Appwidget-provider xmlns:android="Http://schemas.android.com/apk/res/android" android:minwidth="300dip" android:minheight="72dip" Android:updateperiodmillis="0" android:initiallayout="@layout/weibo_widget"> </ Appwidget-provider > |
Property Description:
MinWidth and the MinHeight
Specify the minimum area required for the app widget layout
Minresizewidth and the Minresizeheight
Specifies the minimum size of the widget.
Updateperiomillis
Defines how often the widget is updated.
Attention:
n when the value of Updateperiodmillis is less than 30 minutes, the system automatically sets the update frequency to 30 minutes.
n If you need to update frequently, or if you do not want the device to hibernate, you can use an alarm-based update instead of the widget's own refresh mechanism. Setting the alarm type to elapsed_realtime or RTC will not wake the dormant device, and the Updateperiodmillis should be set to 0.
Initiallayout
The layout resource file that points to the widget
Configure
An optional attribute that defines the configuration activity for the widget. If the item is defined, the activity is started automatically when the widget is created.
Previewimage
Specifies the preview, which appears when the user selects the widget and, if not, displays the icon for the app.
Autoadvanceviewid
Specifies a child view ID that indicates that the child view is automatically updated.
ResizeMode
Specifies the rules for the widget to adjust dimensions. (optional value: "Horizontal" [Horizontal stretch], "vertical" [vertical stretch], "none" [cannot be stretched])
widgetcategory
Specify where widgets can be displayed, such as Home screen, lock screen (optional value: "Home_screen", "Keyguard")
Initialkeyguardlayout
Point to the layout resource file that the widget is located on the lock screen.
"Android" Appwidget Desktop widget