For more information about the previous configurations such as mainfest. XML, XML/widget, Res/layout/my_widget, see the API documentation.
I. Configure widgets.
You need to pay attention to the following points for setting the pluate of widgets:
Intent intent = new intent (); <br/> intent. putextra (appwidgetmanager. extra_appwidget_id, mappwidgetid); <br/> intent. setaction (appwidgetmanager. action_appwidget_update); <br/> intent. putextra (appwidgetmanager. extra_appwidget_ids, <br/> New int [] {mappwidgetid}); <br/> context. sendbroadcast (intent); <br/> setresult (result_ OK, intent );
1. send the current intent through sendbroadcast. The action must be appwidgetmanager. action_appwidget_update; in this way, the widgetprovider you write can be connected, because widgetprovider itself is reciver, and action is appwidgetmanager. action_appwidget_update to implement call widgetprovider
2.Be sure to write setresult (result_ OK, intent); otherwise the widget will not be placed on the desktopGo (I forgot to test it for a long time. Very depressing)
E/androidruntime (1785): caused by: Java. Lang. nullpointerexception
E/androidruntime (1785): At com. Android. launcher. launcher. completeaddappwidget (launcher. Java: 657)
E/androidruntime (1785): At com. Android. launcher. launcher. onactivityresult (launcher. Java: 352)
E/androidruntime (1785): at Android. App. activity. dispatchactivityresult (activity. Java: 3595)
E/androidruntime (1785): at Android. App. activitythread. deliverresults (activitythread. Java: 3001)
Because the corresponding widget ID is added to workspace. Java in the corresponding laucher.
II. Write widgetprovidrer
1. Write widgetprovider
Public void onupdate (context, appwidgetmanager, <br/> int [] appwidgetids) {<br/> // todo auto-generated method stub <br/> super. onupdate (context, appwidgetmanager, appwidgetids); <br/> defaultappwidget (context, appwidgetids); <br/> context. startservice (updateintent); </P> <p>}
The processing of defaappappwidget is mainly to get the current remoteviews and then set the listener for the buttons and so on.
Pendingintent is generally used for listener settings. Because remoteviews only supports setonclickpendingintent (ID, pendingintent );
The pendintent can be obtained in two ways. One is pendingintent. getactivity (); that is, jump to the corresponding activity to process your affair.
Pendingintent = pendingintent. getactivity (context,
0/* No requestcode */, intent, 0/* no flags */);
Views. setonclickpendingintent (R. Id. ivcover, pendingintent );
The second is to call servers for processing.
Pendingintent = pendingintent. getservice (context,
0/* No requestcode */, intent, 0/* no flags */);
Views. setonclickpendingintent (R. Id. btnnext, pendingintent );
3. Write Server
Oncreate is generally used to instantiate something during the first call.
Generally, the action you passed is processed in onstart ().
After processing the corresponding servers, call the code for updating the UI in widgetprovider.
References:
Music Widgets
Search Widgets
If you are interested, you can view the source code directly!