Home » Android » The process of creating Widget

The process of creating Widget

follows the Android document


1. writes a class that inherits MyWidget AppWidgetProvider and registers

in the manifest file
<receiver android:name=, "ExampleAppWidgetProvider", " >"
<action android:name=, "android.appwidget.action.APPWIDGET_UPDATE", " />"
<meta-data android:name=, "android.appwidget.provider",
android:resource= "@xml/example_appwidget_info" />


, where resource is the configuration file for XML

2. new folder XML creates example_appwidget_info.xml

<appwidget-provider xmlns:android=, "http://schemas.android.com/apk/res/android",
Android:minWidth= "294dp" // minimum width
Android:minHeight= "72dp" // minimum height
Android:updatePeriodMillis= "86400000" // update interval time (millisecond value) updated every 24 hours, the shortest half hour
Android:previewImage= "@drawable/preview" // preview picture, default is logo
Android:initialLayout= @layout/example_appwidget // initializes layout file
The android:configure= "com.example.android.ExampleAppWidgetConfigure" // configuration page hits the access page ""
Android:resizeMode= "horizontal|vertical" > // zoom mode
Android:widgetCategory= "home_screen|keyguard" // supports type keyguard lock screen page more than 4.2 support
The layout of the android:initialKeyguardLayout= @layout/*** // lock screen window supports more than 4.2

projects in general use a few can

<appwidget-provider xmlns:android=, "http://schemas.android.com/apk/res/android", 
Android:minWidth= "294dp" // minimum width
Android:minHeight= "72dp" // minimum height
Android:updatePeriodMillis= "0"
Android:initialLayout= @layout/example_appwidget // initializes layout file

, where android:updatePeriodMillis= "0"

Update time, if half an hour, certainly can not achieve the purpose of

In the background, a service timer updates the widget

at each specified time

3.widget life cycle

OnReceive ()  //, so the  is called when the declared cycle is called
OnEnabled () // calls when the first widget is created
OnUpdate () //, when new widget is added, is called when the call is updated automatically
Call when OnDeleted () // widget is deleted
when the last widget of OnDisabled () // is removed
When the width of OnAppWidgetOptionsChanged () //widget varies,


Write a service and configure
in the manifest file
OnCreate ()

OnDestroy ()

When the desktop has widget, the startup service starts the service in the Widget's OnEnabled method, and in order to prevent the service from being killed, Yi is in

Boot service is also started in OnUpdate () and multiple starts does not cause any impact on

Stop service disDabled in

Update Widget
in service
With AppWidgetManager.updateAppWidget ()

Widget added click event

SetOnClickPendingIntent ()

Monitor screen off, turn on or off timer