1. Create an AppWidget layout that contains two textviews for displaying the content: Xml Code <? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <TextView android: layout_height = "wrap_content" android: layout_width = "fill_parent" android: id = "@ + id/tv1" android: textColor = "# FF0000" android: textSize = "24sp" android: textStyle = "bold" android: text = "-1"> </TextView> <TextView and Roid: layout_height = "wrap_content" android: id = "@ + id/tv2" android: textSize = "24sp" android: textStyle = "bold" android: textColor = "#00FF00" android: layout_width = "fill_parent" android: text = "-2"> </TextView> </LinearLayout> 2. create an xml directory under res, and then create the AppWidget information xml file in the xml directory: 2.1 when creating an xml file, select AppWidget Provider for type. 2.2 fill property: formula for calculating the width and height: number of occupied screen cells * 74-2 Update period millis: set to 0 and refresh manually. According to the experiment, when the value is not set to 0, at least on 2.2, the system does not refresh according to the set value, or it is better to control the refresh time. Initial layout: adds the layout to be used by the control. Configure is not used for the time being. leave it blank. 3. Create the AppWidgetDemo class: Reload all functions in AppWidgetProvider, and add output statements to each function to view the call sequence. Java code public class AppWidgetDemo extends AppWidgetProvider {@ Override public void onDeleted (Context context, int [] appWidgetIds) {// TODO Auto-generated method stub super. onDeleted (context, appWidgetIds); Log. e ("AppWidgetDemo", "onDeleted") ;}@ Override public void onDisabled (Context context) {// TODO Auto-generated method stub super. onDisabled (context); Log. e ("AppWidgetDemo", "onDisabled ");} @ Override public void onEnabled (Context context) {// TODO Auto-generated method stub super. onEnabled (context); Log. e ("AppWidgetDemo", "onEnabled") ;}@ Override public void onReceive (Context context, Intent intent) {// TODO Auto-generated method stub super. onReceive (context, intent); Log. e ("AppWidgetDemo", "onReceive, Action:" + intent. getAction () ;}@ Override public void onUpdate (Context contex T, AppWidgetManager appWidgetManager, int [] appWidgetIds) {// TODO Auto-generated method stub super. onUpdate (context, appWidgetManager, appWidgetIds); Log. e ("AppWidgetDemo", "onUpdate, Count:" + appWidgetIds. length) ;}} 4. in AndroidManifest. declare this Widget in the xml file: Add a handler whose name is the class name of the AppWidgetDemo class. Xml Code <er android: name = "AppWidgetDemo"> </receiver> adds an Intent filter to the receiver, and receives the android. appwidget. action. APPWIDGET_UPDATE Intent from the system. Xml Code <Cycler android: name = "AppWidgetDemo"> <intent-filter> <action android: name = "android. appwidget. action. APPWIDGET_UPDATE "> </action> </intent-filter> </receiver> In addition, meta-data information must be added to the receiver to inform the system of AppWidgetProvider information: xml Code <meta-data android: name = "android. appwidget. provider "android: resource =" @ xml/widget_info_demo "> </meta-data> the meta-data name is an agreed android. appwidget. provider, resource is the Ap created in step 1 PWidget information xml file. 5. Now the AppWidget is available. install it on the simulator and check the running process. 5.1 Add a Widget to the desktop: onEnabled is called: according to the instructions, this function is called when the first Widget instance appears on the desktop. OnReceive is called: onReceive, Action: android. appwidget. action. APPWIDGET_ENABLED onUpdate is called: onUpdate, Count: 1, and the number of appwidgets to be updated is 1 onReceive. Called: onReceive, Action: android. appwidget. action. APPWIDGET_UPDATE 5.2 adds a Widget to the desktop: onUpdate is called onUpdate, Count: 1, and the number of appwidgets to be updated is still 1, not 2. OnReceive is called: onReceive, Action: android. appwidget. action. APPWIDGET_UPDATE 5.3 remove a Widget from the desktop: onDeleted: onReceive is called when each instance is removed. Action: android. appwidget. action. APPWIDGET_DELETED 5.4 then removes a Widget from the desktop: onDeleted: still executes onReceive, Action: android. appwidget. action. APPWIDGET_DELETED onDisabled: called because the last active instance has been removed. OnReceive, Action: android. appwidget. action. APPWIDGET_DISABLED 6. refresh AppWidget 6.1 refresh in onUpdate (): onUpdate is called when AppWidget is placed on the desktop, and may be called when Update period millis is reached. Java code RemoteViews appWidgetView = new RemoteViews (context. getPackageName (), R. layout. widget_layout_demo); appWidgetView. setTextViewText (R. id. tv1, String. valueOf (mCount); appWidgetView. setTextViewText (R. id. tv2, String. valueOf (mCount); appWidgetManager. updateAppWidget (appWidgetIds, appWidgetView); obtains a RemoteViews, that is, the View corresponding to the AppWidget layout; updates the control to be updated using the specified Id; updates the entire RemoteViews, now you can update the AppWidget content. However, there is another problem with such code. When multiple controls are added to the desktop in turn, the following effect will appear: when updating, all appwidgets are not updated at the same time, this is because the array in onUpdate contains only one id. If you want to update multiple IDs at the same time, you can replace the update statement with the Java code appWidgetManager. updateAppWidget (new ComponentName (context, AppWidgetDemo. class), appWidgetView); by using the component name, you can update all appwidgets with the same name. 6.2 update 6.2.1 custom Action notification refresh in onReceive (): In AndroidManifest. add a custom Action: com. demo. appwidget. refresh Xml Code <er android: name = "AppWidgetDemo"> <intent-filter> <action android: name = "android. appwidget. action. APPWIDGET_UPDATE "> </action> <action android: name =" com. demo. appwidget. refresh "> </action> </intent-filter> <meta-data android: name =" android. appwidget. provider "andro Id: resource = "@ xml/widget_info_demo"> </meta-data> lt;/javaser> Intent containing this Action can be sent in the background service or Activity, the Receiver receives the message and triggers onReceive. In this example, intent: Java code btnSend is broadcast when a button is pressed. setOnClickListener (new OnClickListener () {public void onClick (View v) {Intent intent = new Intent (); intent. setAction ("com. demo. appwidget. refresh "); intent. putExtra ("value", teInput. getText (). toString (); SendMsgActivity. this. sendBroadcast (intent) ;}}); at the receiving end: Java code if (intent. getAction (). equals ("com. demo. appwidget. refresh ") {String value = intent. getStringEx Tra ("value"); RemoteViews appWidgetView = new RemoteViews (context. getPackageName (), R. layout. widget_layout_demo); appWidgetView. setTextViewText (R. id. tv1, value); appWidgetView. setTextViewText (R. id. tv2, value); AppWidgetManager. getInstance (context ). updateAppWidget (new ComponentName (context, AppWidgetDemo. class), appWidgetView);} determines whether it is an Action of interest. If yes, the value is set and updated. 6.2.2 receive system message Refresh: for example, add another action in intent-filter: "android. provider. telephony. SMS_RECEIVED ", in AndroidMenifest. add <uses-permission android: name = "android. permission. RECEIVE_SMS "/> set the permission to trigger onReceive when the system receives a short message. However, some actions are special, such as android. intent. action. TIME_TICK, according to android. content. description in the intent document: You can not receive this through components declared in manifests, only by exlicitly registering for it with Context. registerReceiver (). this Action is in AndroidManifest. it is useless to declare in xml. You must register a service provider to receive it, and then forward it to yourself. 6.3 refresh the Java code btnRefresh directly in an external Activity or Service. setOnClickListener (new OnClickListener () {public void onClick (View v) {String value = teInput. getText (). toString (); RemoteViews appWidgetView = new RemoteViews (SendMsgActivity. this. getPackageName (), R. layout. widget_layout_demo); appWidgetView. setTextViewText (R. id. tv1, value); appWidgetView. setTextViewText (R. id. tv2, value); AppWidgetManager. getInsta Nce (SendMsgActivity. this ). updateAppWidget (new ComponentName (SendMsgActivity. this, AppWidgetDemo. class), appWidgetView) ;}}; this code segment can directly refresh the content of the AppWidget without triggering its onUpdate (). 7. response click event because onUpdate is a function called when every AppWidget is placed on the desktop, the event Association is completed in this function: Java code Intent intent = new Intent (context, SendMsgActivity. class); PendingIntent pendingIntent = PendingIntent. getActivity (context, 0, intent, 0); appWidgetView. setOnClickPendingIntent (R. id. tv1, pendingIntent); note that this code segment must be in appWidgetManager. before updateAppWidget (), otherwise it will not take effect. After running, you can click the first control of AppWidget to call the specified Activity. 8. For more information about Config Activity, see Dev Guide> App Widgets in the SDK documentation.