1. receive broadcasts from app Widgets
// The purpose of the file in androidmanifest. As long as there is an action that matches, the class inheriting appwidgetprovider will be called:
<Cycler Android: Name = "inherit the appwidgetprovider class">
<Intent-filter>
<Action Android: Name = "android. appwidget. Action. appwidget_updata"/>
</Intent-filter>
<Intent-filter>
<Action Android: Name = "Mars. appwidget03.update _ app_widget"/> // This is a custom action
</Intent-filter>
<Meta-data Android: Name = "android. appwidget. provider"
Android: Resource = "@ XML/example_appwidget_info"/>
</Cycler>
// The process of inheriting the appwidgetprovider class in the Code is as follows:
A.In the update method
Intent intent = new intent ();
Intent. setaction ("Mars. appwidget03.update _ app_widget ");
Use the getbroadcast method to obtain the pendingintent object
Pendingintent = pendingintent. getbroadcast (contect, 0, intent object, 0 );
B.Then, the onreciever method receives the broadcast sent by step.
2. Update the widget status in the app widget.
Public void onreceive (context, intent ){
String action = intent. getaction ();
If (Action = "Mars. appwidget03.update _ app_widget "){
Remoteviews = new remoteviews (context. getpackage, R. layout. example_appwidget );
// Modify the image
Remoteviews. setimageviewresource (R. Id. imageid, R. drawable. Ku );
Remoteviews. Settextviewtext (R. Id. widgettextid ,"");
// Obtain the appwidgetmanager object
Appwidgetmanager = appwidgetmanager. getinstance (context );
// Componentname refers to the entire app widget control, while remoteviews refers to the controls in the app widget.
Componentname = new componentname (context, exampleappwidgetprovider. Class );
// Update the appwidget
Appwidgetmanager. updateappwidget (componentname, remoteviews );
}
// Else statement is required. Otherwise, other functions such as onupdate and onenable will not be called.
Else {
Super onreceive (context, intent );
}
}