In appWidget, events can be divided into three types:
A. Enable Activity
B. Start Service
C. Send button Action
The following describes how to implement the analysis one by one.
2.1 enable Activity
2.11 first define an intent for enabling the Activity
Eg: Intent fullIntent = new Intent (this, FullScreen. class );
To pass data, use the intent. putExtra () method
Eg: fullIntent. putExtra ("isCircle", isCircle );
2.12 instantiate a PendingIntent with intent and call the getActicity method of pendingIntent to start another Activity
① If the Intent has data, set the value of the last parameter to FLAG_CANCEL_CURREN.
Eg: PendingIntent Pfullintent = PendingIntent. getActivity (this, 0, fullIntent, PendingIntent. FLAG_CANCEL_CURRENT );
② If the Intent does not contain data, the last parameter is set to 0.
Eg: PendingIntent Pfullintent = PendingIntent. getActivity (this, 0, fullIntent, 0 );
2.13 instantiate RemoteView and its corresponding Widget Layout
Eg: RemoteViews views = new RemoteViews (getPackageName (), R. layout. widget );
2.14 set Button events for buttons or ImageButton on RemoteView
Eg: views. setOnClickPendingIntent (R. id. IBfullscreen, Pfullintent );
2.15. Update the AppWidget Interface
① If the AppWidget interface is updated in the onUpdate () method
Eg: appWidgetManager. updateAppWidget (appWidgetIds, ActivityView );
② If the AppWidget interface is updated outside the onUpdate () method (usually within the Service), several variables need to be defined.
Eg: public RemoteViews views; RemoteView object
Public ComponentName thisWidget; component name
Public AppWidgetManager manager; AppWidget manager
ThisWidget = new ComponentName (this, PictureAppWidgetProvider. class );
Manager = AppWidgetManager. getInstance (this );
Manager. updateAppWidget (thisWidget, views );
2.2 enable Service
2.21 define an intent to enable the Service
Eg: Intent startServiceInten = new Intent ("zyf. temp. Service. START ");
Note: The parameter is the Service enabling action.
2.22 instantiate a PendingIntent with Intent and start a service using the getService method of PendingIntent
Eg: PendingIntent Pintent = PendingIntent. getService (context, 0, startServiceInten, 0 );
2.23 instantiate RemoteView and its corresponding Widget Layout
Eg: RemoteViews views = new RemoteViews (getPackageName (), R. layout. widget );
2.24 set Button events for buttons or ImageButton on RemoteView
Eg: views. setOnClickPendingIntent (R. id. IBfullscreen, Pfullintent );
2.25. Update the AppWidget Interface
① If the AppWidget interface is updated in the onUpdate () method
Eg: appWidgetManager. updateAppWidget (appWidgetIds, ActivityView );
② If the AppWidget interface is updated outside the onUpdate () method (usually within the Service), several variables need to be defined.
Eg: public RemoteViews views; RemoteView object
Public ComponentName thisWidget; component name
Public AppWidgetManager manager; AppWidget manager
ThisWidget = new ComponentName (this, PictureAppWidgetProvider. class );
Manager = AppWidgetManager. getInstance (this );
Manager. updateAppWidget (thisWidget, views );
2.3 send button Action
2.31 define an Intent to send the button Action.
Eg: Intent prevInten = new Intent ("PREV ");
Or Intent prevIntent = new Intent (Context, CalendarAppWidgetProvider. Class)
PrevIntent. setAction ("PREV"); (when a service is enabled, broadcast is sent on the service)
2.32 instantiate a PendingIntent with Intent and use the getBroadcast method of PendingIntent to send broadcasts.
Eg: PendingIntent Pprevintent = PendingIntent. getBroadcast (this, 0, prevInten, 0 );
2.33 instantiate RemoteView and its corresponding Widget Layout
Eg: RemoteViews views = new RemoteViews (getPackageName (), R. layout. widget );
2.34 set Button events for buttons or ImageButton on RemoteView
Eg: views. setOnClickPendingIntent (R. id. IBprev, Pprevintent );
2.35. Update the AppWidget Interface
① If the AppWidget interface is updated in the onUpdate () method
Eg: appWidgetManager. updateAppWidget (appWidgetIds, ActivityView );
② If the AppWidget interface is updated outside the onUpdate () method (usually within the Service), several variables need to be defined.
Eg: public RemoteViews views; RemoteView object
Public ComponentName thisWidget; component name
Public AppWidgetManager manager; AppWidget manager
ThisWidget = new ComponentName (this, PictureAppWidgetProvider. class );
Manager = AppWidgetManager. getInstance (this );
Manager. updateAppWidget (thisWidget, views );
2.36 receive this Action
① Receive in AppWidget's onReceive Method
Actions: Add Action to Manifest. xml.
Eg: <intent-filter>
<Action android: name = "android. appwidget. action. APPWIDGET_UPDATE> </action>
<Action android: name = "PREV"> </action>
</Intent-filter>
Compile the action to be implemented in the onReceive () method
Eg: if (intent. getAction (). equals ("PREV "))
{
Write the Action to be implemented after receiving the Action
} Www.2cto.com
② Receive in Service
Register a BroadcastReceive and declare the receiver.
Eg: IntentFilter filter = new IntentFilter ();
Filter. addAction ("PREV ");
RegisterReceiver (doCommand, filter );
Compile, write the action to be implemented in the onReceive method of the BroadcastReceive class
Eg: if (intent. getAction (). equals ("PREV "))
{
Write the Action to be implemented after receiving the Action
}
Author: fangchongbory