This article is an example of the app widget usage for Android development. Share to everyone for your reference, specific as follows:
Controls placed on the desktop are called--app widgets, such as buttons, pictures, and so on on the desktop, such as Control Panel of the desktop player
The Appwidgetproviderinfo object, which provides metadata for the app widget, including layout, update frequency, and so on, which is not generated by ourselves, but is defined by Android itself, and this object is defined in the XML file
1, define the Appwidgetproviderinfo object, define a file named Widget_config.xml in the Res/xml folder
<?xml version= "1.0" encoding= "Utf-8"?> <appwidget-provider xmlns:android=
"http://" Schemas.android.com/apk/res/android "
android:minwidth=" 300DP "
android:minheight=" 72DP "
android: updateperiodmillis= "0"
android:initiallayout= "@layout/widget_ui"
>
</appwidget-provider>
Note: The created folder name must be XML, because only then can be identified by R
2, Appwidgetprovider defines the basic life cycle of the app widget
public class Mywidgetprovider extends Appwidgetprovider {public static int Tag;
public int Max;
public int current;
@Override public void onenabled (context) {super.onenabled);
System.out.println (This method is invoked the first time it is created);
@Override public void ondisabled {System.out.println ("Call this method when the last app widget is deleted"); @Override public void OnReceive (context context, Intent Intent) {//OnReceive method of calling the parent class is not small, otherwise you cannot hear the OnUpdate event. Su
Per.onreceive (context, intent);
SYSTEM.OUT.PRINTLN ("Receive broadcast event");
@Override public void OnUpdate (context context, Appwidgetmanager Appwidgetmanager, int[] appwidgetids) {
SYSTEM.OUT.PRINTLN ("Call this method" after the specified update time is reached or when the user adds the app widget to the desktop);
for (int i = 0; i < appwidgetids.length i++) {Intent Intent = new Intent (context, hb.class);
Pendingintent pendingintent = pendingintent.getactivity (context, 0, intent, 0); R.LAYOUT.WIDGET_UI refers to the layout of controls that are displayed on the desktop remoteviews Remoteviews = new Remoteviews (Context.getpackagename (), r.layout.widget_ui);
R.id.widgetbutton refers to the binding event Remoteviews.setonclickpendingintent (R.id.widgetbutton, pendingintent) for the desktop control button;
Updateappwidget method to update remoteviews appwidgetmanager.updateappwidget (appwidgetids[i), remoteviews); @Override public void ondeleted (context context, int[] appwidgetids) {System.out.println ("App widget deleted
Call this method in addition to ");
}
}
3, add a layout file Res/layout/widget_ui.xml (content displayed on the desktop)
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/" Apk/res/android "
android:layout_width=" wrap_content "
android:layout_height=" wrap_content ">
< Button
android:id= "@+id/widget_bt_up"
android:layout_width= "wrap_content"
android:layout_height= " Wrap_content "
android:layout_weight=" 1 "
android:text=" value++ "/> <button android:id=
" @+id/ Widget_bt_down "
android:layout_width=" wrap_content "
android:layout_height=" Wrap_content
" android:text= "value--"
android:layout_weight= "1"/>
</LinearLayout>
4. Add Reseiver tags to androidmanifest.xml files
Android:resource= "@xml/widget_config" indicates that the Widget_config.xml property initialization setting is Appwidget
Android:name= "Android.appwidget.action.APPWIDGET_UPDATE" is an Android system that provides a way to determine how appwidget is handled.
Android:name= ". Mywidgetprovider "represents a class that is processed, that is, a class that inherits the Appwidgetprovider class
<receiver android:name= ". Mywidgetprovider "android:label=" Mywidget "android:icon=" @drawable/icon ">
<intent-filter>
< Action android:name= "Android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
< Meta-data android:name= "Android.appwidget.provider" android:resource= "@xml/widget_config"/> </receiver
>
Note: The app widget and our application run in different processes (the view in the app widget runs in the home screen process), so you use the two classes of remoteviews and pendingintent to manipulate the controls on the desktop
If your ondelete, OnUpdate and other events do not trigger, then an important reason is that you override the OnReceive event, but did not invoke super.onreceive (), so that the events after this will not trigger, The Appwidgetprovider event-handling mechanism is that onrecieve first triggers and then the onreceive triggers subsequent events.
For more information on Android-related content readers can view the site topics: "Android File Operation tips Summary", "Android programming development of the SD card operation Summary", "Android Development introduction and Advanced Course", "Android Resources Operating Skills summary", " Android View tips Summary and a summary of the use of Android controls
I hope this article will help you with the Android program.