AppWidget, such as the clock on the android desktop, appwidgetandroid
I,
1. The App widget is a space on the home desktop, such as a clock. You can click the next icon in the player to play the video.
2. AppWidgetProviderInfo object: provides metadata for App widgets, including layout, update frequency, and other data. This object is defined in an xml file.
3. AppWidgetProvider: defines the basic lifecycle function of an App Widget.
2. Steps for creating an App Widget:
1. Define AppWidgetProviderInfo: Define the example_appwidget_info.xml file in the res/xml folder.
Android: updatePeriodMills = "86400000" defines the Update Time
2. Add AndroidManifest. xml
<receiver android: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" /></receiver>
3. Implement AppWidgetProvider:
UpDatate: it is called after the specified Update time or when the user adds an App Widget to the desktop.
OnDeleted: called when the App widget is deleted.
OnEnabled: called when an App widget instance is created for the first time.
OnDisabled: called when the last Appp Widget instance is deleted.
OnReveice: receives broadcast events.
For example:
1. Create an xml folder under res and create the example_appwidget_info.xml file in it:
<span style="font-size:14px;"><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="294dp" android:minHeight="72dp" android:updatePeriodMillis="86400000" android:initialLayout="@layout/example_appwidget" ></appwidget-provider></span><pre class="java" name="code">
2. Add AndroidManifest. xm:
<Pre class = "html" name = "code"> <receiver android: 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 "/> </receiver>
3. Write the Code:
package mars.appwidget01;import android.appwidget.AppWidgetManager;import android.appwidget.AppWidgetProvider;import android.content.Context;public class ExampleAppWidgetProvider extends AppWidgetProvider{@Overridepublic void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {System.out.println("onupdate");super.onUpdate(context, appWidgetManager, appWidgetIds);}@Overridepublic void onDeleted(Context context, int[] appWidgetIds) {System.out.println("onDeleted");super.onDeleted(context, appWidgetIds);}@Overridepublic void onDisabled(Context context) {System.out.println("onDisabled");super.onDisabled(context);}@Overridepublic void onEnabled(Context context) {System.out.println("onEnabled");super.onEnabled(context);}}