Near the team
1. Custom widgets must inherit Appwidgetprovider
2, must register in Androidmanifest.xml
<receiver android:name= ". Widgets. Appwidget ">
<meta-data
Android:name= "Android.appwidget.provider"
Android:resource= "@xml/guide_widget"/>
<intent-filter>
<action android:name= "Android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
</receiver>
(1) Receiver name must be the custom widget class name
(2) The name of the Meta-data must be Android.appwidget.provider
(3) Meta-data's resource is the widget's description file, which must be placed under the Res/xml path
(4) Need to add action to increase the listener receiver type Android.appwidget.action.APPWIDGET_UPDATE
3, Res/xml
<?xml version= "1.0" encoding= "Utf-8"?>
<appwidget-provider xmlns:android= "Http://schemas.android.com/apk/res/android"
android:initiallayout= "@layout/guide_widget"
android:minheight= "294.0dip"
Android:minwidth= "294.0dip"
android:updateperiodmillis= "1801000"/>
(1) The outermost label must be Appwidget-provider
(2) android:initiallayout specifies the layout file used by the widget
(3) android:minheight Specifies the height of the widget, android:minwidth specifies the width of the widget. These two values cannot be arbitrarily specified, preferably depending on the number of screen rows occupied by the widget you want to place. For example, if a widget is 3 rows and 2 columns, then minheight should be (3*74) -2=220,minwidth should be (2*74) -2=146
(4) Android:updateperiodmillis This value is the widget refresh time, preferably set more than 1 hours, otherwise it will seriously waste system resources, power consumption will be very large. Every fixed time, the OnUpdate method of the widget is called, and if the value is 0, the widget is not updated
4. The widget inherits from the parent class method
(1) onenabled (context context)
This method is called after the user adds the widget
(2) OnUpdate (context context, Appwidgetmanager
Appwidgetmanager,int[] appwidgetids)
This method is called after the user adds a widget to refresh the widget (called after onenabled) that the interface has already installed.
The Appwidgetmanager parameter is used to perform the Refresh interface operation
Appwidgetids is the ID of all widgets for the application (users may add multiple, so here is an array)
(3) ondeleted (context context, int[] appwidgetids)
This method is called after the widget has been deleted (note that after the deletion, that is, the method is not performing a delete action)
(4) OnReceive (context context, Intent Intent)
This method is inherited from receiver, try not to use, if you want to use, you need to handle some special code, otherwise the widget will be a problem
5, the interface widget refresh and click events
(1) in the widget, you can no longer use the Findviewbyid method to get components, you need to use other methods. Remoteviews RV = new Remoteviews (Context.getpackagename (),
R.layout.guide_widget);
Remoteviews is a fictional component that uses it to load layout.
(2) Click on a component in layout to start activity or service
Intent Intent = new Intent (context, widgetdemoactivity.class);
Pendingintent pendingintent = pendingintent.getactivity (context, 0, intent, 0);
Rv.setonclickpendingintent (R.id.guide_0, pendingintent);
(3) Interface Refresh
Appwidgetmanager.updateappwidget (Appwidgetid, RV);
6. Widgets that support the components used in layout
Framelayout, LinearLayout, Relativelayout
AnalogClock, Button, chronometer, ImageButton, ImageView, ProgressBar, TextView
7. Custom Widget Class complete code
public class Appwidget extends Appwidgetprovider {
Remoteviews RV;
@Override
public void OnUpdate (context context, Appwidgetmanager Appwidgetmanager,
Int[] appwidgetids) {
System.out.println (Context.getpackagename ());
Final int N = appwidgetids.length;
Because it is possible that the user has added multiple widgets, it is necessary to traverse the Appwidgetids
for (int i = 0; i < N; i++) {
System.out.println (Appwidgetids[i]);
int appwidgetid = Appwidgetids[i];
RV = new Remoteviews (Context.getpackagename (),
R.layout.guide_widget);
Intent Intent = new Intent (context, widgetdemoactivity.class);
Intent.setaction (Context.getpackagename () + Appwidgetid);
Pendingintent pendingintent = pendingintent.getactivity (context, 0,
Intent, 0);
Rv.setonclickpendingintent (R.id.guide_0, pendingintent);
Appwidgetmanager.updateappwidget (Appwidgetid, RV);
}
Super.onupdate (context, Appwidgetmanager, appwidgetids);
}
}
8. Widget life cycle (android1.6)
(1) When the user drags the widget to the interface, the following methods are called in sequence: onreceive-onenabled-onreceive-onupdate-onreceive-onreceive
(2) When the user drags the widget to delete, the following methods are called in sequence:
Onreceive-ondeleted-onreceive-ondisabled
Android App Development (10): Use of Widgets