This article describes the usage of App widgets for Android development. Share it for your reference, as follows:
The controls placed on the desktop are called - App widgets. For example, you can add buttons, pictures and other controls on the desktop, such as the control panel of the desktop player.
AppWidgetProviderInfo object, it provides metadata for App Widget, including layout, update frequency and other data. This object is not generated by ourselves, but is defined by android itself. This object is defined in an XML file.
1. Define the AppWidgetProviderInfo object and define a file named widget_config.xml in the res/xml folder.
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:andro android:minWidth="300dp" android:minHeight="72dp" android:updatePeriodMillis="0" android:initialLayout="@layout/widget_ui" > </appwidget-provider>
Note: The folder name created must be xml, because only in this way can it be recognized by R
2. AppWidgetProvider defines the basic life cycle of App Widget
public class MyWidgetProvider extends AppWidgetProvider { public static int Tag; public int max; public int current; @Override public void onEnabled(Context context) { (context); ("This method is called when it is created for the first time"); } @Override public void onDisabled(Context context) { ("This method is called when the last App Widget is deleted"); } @Override public void onReceive(Context context, Intent intent) { //The onReceive method of the parent class cannot be missing, otherwise the onUpdate event cannot be listened to (context, intent); ("Receive broadcast events"); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { ("Call this method after the specified update time is reached or when the user adds an App Widget to the desktop"); for(int i = 0; i < ; i++){ Intent intent = new Intent(context, ); PendingIntent pendingIntent = (context, 0, intent, 0); //.widget_ui refers to the control layout displayed on the desktop RemoteViews remoteViews = new RemoteViews((), .widget_ui); //Refers to binding events for desktop control button (, pendingIntent); //UpdateAppWidget method updates remoteViews (appWidgetIds[i], remoteViews); } } } @Override public void onDeleted(Context context, int[] appWidgetIds){ ("This method is called when the App Widget is deleted"); } }
3. Add a layout file res/layout/widget_ui.xml (the content displayed on the desktop)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Value++"/> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Value--" android:layout_weight="1"/> </LinearLayout>
4. Add the reseiver tag to the file
android:resource="@xml/widget_config" indicates that widget_config.xml is the attribute initialization setting of appwidget
android:name=".APPWIDGET_UPDATE" is a processing method provided by the Android system to determine that it is an appwidget.
android:name=".MyWidgetProvider" represents the processed class, that is, the class inherits the AppWidgetProvider class
<receiver android:name=".MyWidgetProvider" android:label="myWIdget" android:icon="@drawable/icon"> <intent-filter> <action android:name=".APPWIDGET_UPDATE"/> </intent-filter> <meta-data android:name="" android:resource="@xml/widget_config"/> </receiver>
Remark:The App Widget and our application run in different processes (the View in the App Widget runs in the Home Screen process), so we need to use the two classes RemoteViews and PendingIntent to control the desktop controls.
If your onDelete, onUpdate and other events are not triggered, then an important reason is that you override the onReceive event, but do not call(), so the events after this will not be triggered. The event handling mechanism of AppWidgetProvider is that onRecieve is triggered first, and then onReceive will trigger subsequent events.
For more information about Android related content, please check out the topic of this site:Android file operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary》、《Android View View Tips Summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.