1. receive broadcasts from appwidget
1) register a new intent-filter for appwidgetprovider in androidmanifest. xml.
2) use the getbroadcast () method to create a pendingintent
3) register a processor for the controls in the appwidget.
4) receive broadcast messages in the onreceive () method
2. Update the status of controls in the appwidget.
Note: Because the appwidget and the main program are not in the same process, you cannot use a common method for operations!
1) there are a series of methods in the remoteviews class to update controls
2) After using remoteviews to update the control status, you must use appwidgetmanager to notify appwidget to update the status.
The following is an example: the appwidget contains three controls: textview, imageview, And button. Our purpose is to use the button to change the content of textview and imageview.
First look at the layout file: Res/layout Folder:
Main. XML content:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
</Linearlayout>
Test. XML content:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Press upload"
Android: Id = "@ + ID/button"
/>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "hello"
Android: Id = "@ + ID/textview"
/>
<Imageview
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/Hong"
Android: Id = "@ + ID/imageview"
/>
</Linearlayout>
The info. XML content in the Res/XML folder is as follows:
<Appwidget-provider xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: minwidth = "294dp"
Android: minheight = "72dp"
Android: updateperiodmillis = "30000"
Android: initiallayout = "@ layout/test"
>
</Appwidget-provider>
Add the following content to the application node of the androidmanifest. xml file:
<Cycler Android: Name = ". appwidgetprovidertest">
<Intent-filter>
<Action Android: Name = "android. appwidget. Action. appwidget_update"/>
</Intent-filter>
<Intent-filter>
<Action Android: Name = "weiyong. Huang. appwidget_update"/>
</Intent-filter>
<Meta-data Android: Name = "android. appwidget. provider"
Android: Resource = "@ XML/INFO"> </meta-data>
</Cycler>
Add two images in the Res/drawable folder. I will name them mongo.png and hong.png.
The configuration file ends here. Let's look at the Java code below:
Create an appwidgetprovidertest class that inherits from the appwidgetprovider class.
Import Android. App. pendingintent;
Import Android. appwidget. appwidgetmanager;
Import Android. content. componentname;
Import Android. content. context;
Import Android. content. intent;
Import Android. widget. remoteviews;
Public class appwidgetprovidertest extends Android. appwidget. appwidgetprovider {
// Custom intent action
Private Static final string app_action = "weiyong. Huang. appwidget_update ";
@ Override
Public void onreceive (context, intent ){
If (app_action.equals (intent. getaction ())){
System. Out. println ("weiyong. Huang. appwidget_update ");
Remoteviews = new remoteviews (context. getpackagename (), R. layout. test );
Remoteviews. settextviewtext (R. Id. textview, "Haha ");
Remoteviews. setimageviewresource (R. Id. imageview, R. drawable. Bai );
Appwidgetmanager = appwidgetmanager. getinstance (context );
Componentname Cn = new componentname (context, appwidgetprovidertest. Class );
Appwidgetmanager. updateappwidget (CN, remoteviews );
} Else {
Super. onreceive (context, intent );
}
}
@ Override
Public void onupdate (context, appwidgetmanager,
Int [] appwidgetids ){
Intent intent = new intent ();
Intent. setaction (app_action );
Pendingintent = pendingintent. getbroadcast (context, 0, intent, 0 );
Remoteviews = new remoteviews (context. getpackagename (), R. layout. test );
Remoteviews. setonclickpendingintent (R. Id. Button, pendingintent );
Appwidgetmanager. updateappwidget (appwidgetids, remoteviews );
Super. onupdate (context, appwidgetmanager, appwidgetids );
}
@ Override
Public void ondeleted (context, int [] appwidgetids ){
Super. ondeleted (context, appwidgetids );
}
@ Override
Public void onenabled (context ){
Super. onenabled (context );
}
@ Override
Public void ondisabled (context ){
Super. ondisabled (context );
}
}
This example is complete. Through the above three articles, we have basically learned how to create app widgets.