AppWidget is usually called a desktop plug-in. It is an important part of the Android Application Development layer. It is a method to embed a process control into a window of a process other than a process, it is a component program that interacts with the client program.
Let's take a look:
The following example records how to create an AppWidget and how it interacts with the client program:
Step 1: Create an xml folder under the res directory and create <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + eXd4X2FwcHdpZGdldF9pbmZvLnhtbDwvcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;">
Step 2: Create the layout file ywx_appwidget.xml
Step 3: Create ExampleAppWidgetProvider. java to implement the method in AppWidgetProvider
Package com. ywx. appwidget_04; import android. app. pendingIntent; import android. appwidget. appWidgetManager; import android. appwidget. appWidgetProvider; import android. content. componentName; import android. content. context; import android. content. intent; import android. widget. remoteViews; public class ExampleAppWidgetProvider extends AppWidgetProvider {private static final String UPDATE_ACTION = "com. ywx. appwidget_04.UPDATE_APP_WIDGET "; // receives broadcast events @ Overridepublic void onReceive (Context context, Intent intent) {super. onReceive (context, intent);} // this method is called after the specified Update time is reached or when the user adds an App Widget to the desktop @ Overridepublic void onUpdate (Context context, appWidgetManager appWidgetManager, int [] appWidgetIds) {super. onUpdate (context, appWidgetManager, appWidgetIds);} // this method is called when an AppWidget is deleted @ Overridepublic void onDeleted (Context context, int [] appWidgetIds) {super. onDeleted (context, appWidgetIds);} // this method is called when an AppWidget instance is created for the first time @ Overridepublic void onEnabled (Context context) {super. onEnabled (context);} // call this method after the last AppWidget instance is deleted @ Overridepublic void onDisabled (Context context) {super. onDisabled (context );}}
Step 4: declare in AndroidManifest. xml: There are two actiong actions, one is System and the other is custom. An event is triggered if any action is satisfied.
After completing the above four steps, you can complete the simple AppWidget example.
PendingIntent and RemoteViews are mainly used in AppWidget development. The usage record is as follows:
PendingIntent has three main methods:
1. getActivity (Context context, int requestCode, Intent intent, int flags)
2. getBroadcast (Context context, int requestCode, Intent intent, int flags)
3. getService (Context context, int requestCode, Intent intent, int flags)
RemoteViews: indicates that a series of view objects and the indicated objects run in another process.
The appwidget component program is not in the current process, so the listener binding event is different from the original one:
Add in onUpdate () method
Intent intent = new Intent (); intent. setAction (UPDATE_ACTION); // use getBroadcast to generate PendingIntent. When the object is executed, send broadcast PendingIntent pendingIntent = PendingIntent. getBroadcast (context, 0, intent, 0); RemoteViews remoteViews = new RemoteViews (context. getPackageName (), R. layout. ywx_appwidget); remoteViews. setOnClickPendingIntent (R. id. widgetBut, pendingIntent); appWidgetManager. updateAppWidget (appWidgetIds, remoteViews );
Add
String action = intent. getAction (); if (UPDATE_ACTION.equals (action) {RemoteViews remoteViews = new RemoteViews (context. getPackageName (), R. layout. ywx_appwidget); remoteViews. setImageViewResource (R. id. imageId, R. drawable. upload); remoteViews. setTextViewText (R. id. widgetText, "Wahaha"); AppWidgetManager appWidgeManager = AppWidgetManager. getInstance (context); ComponentName componentName = new ComponentName (context, ExampleAppWidgetProvider. class); appWidgeManager. updateAppWidget (componentName, remoteViews);} else {super. onReceive (context, intent );}