The previous section has implemented a simple app widget. Here we will continue to learn more about app widgets through an instance.
First, continue to understand the main classes of the app widget framework:
Appwidgetprovider: inherits from broadcastreceiver and receives notifications when Update, enable, disable, and deleted are applied to app widgets. Onupdate and onreceive are the most common methods.
Appwidgetproviderinfo: Describes the appwidget size, update frequency, initial interface, and other information. It is stored in the Res/XML directory of the application as an XML file.
Appwidgetmanager: Manages appwidgets and sends notifications to appwidgetprovider.
Remoteviews: a class that can be run in other application processes. It is the core of creating appwidgets.
Write the code below. First, create myappwidetprovider. XML,
<?xml version="1.0" encoding="utf-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="100dp" android:minHeight="72dp" android:updatePeriodMillis="86400000" android:initialLayout="@layout/myappwidget" ></appwidget-provider>
The preceding defines the widget width, height, update cycle, and layout widget layout.
The following is our layout file:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/widget_bg1" android:gravity="center" android:id="@+id/layout" android:orientation="vertical" > <TextView android:id="@+id/txtMonth" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:layout_margin="2dp" android:text="" /><TextView android:id="@+id/txtDay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#990033" android:textSize="25dp" android:text="" /><TextView android:id="@+id/txtWeekDay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:textColor="#000000" android:text="" /></LinearLayout>
The corresponding layout widgets have high requirements. You can design them on your own to provide a more beautiful interface.
Next is our core code exampleappwidgetprovider class:
Import android. app. pendingintent; import android. appwidget. appwidgetmanager; import android. appwidget. appwidgetprovider; import android. content. context; import android. content. intent; import android. text. format. time; import android. widget. remoteviews; import android. widget. toast; public class exampleappwidgetprovider extends appwidgetprovider {private string [] months = {"may", "may ", "may", "may"}; private string [] days = {"Sunday", "Monday ", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };@ overridepublic void onupdate (context, appwidgetmanager, int [] appwidgetids) {// todo auto-generated method stubremoteviews remoteviews = new remoteviews (context. getpackagename (), R. layout. myappwidget); time = new time (); time. settonow (); string month = time. year + "" invalid months%time.month%%%remoteviews.settextviewtext(r.id.txt day, new %month, month%%%remoteviews.settextviewtext(r.id.txt weekday, days [time. weekday]); intent = new intent ("cn.com. karl. widget. click "); pendingintent = pendingintent. getbroadcast (context, 0, intent, 0); remoteviews. setonclickpendingintent (R. id. layout, pendingintent); appwidgetmanager. updateappwidget (appwidgetids, remoteviews); super. onupdate (context, appwidgetmanager, appwidgetids) ;}@ overridepublic void onreceive (context, intent) {// todo auto-generated method stubsuper. onreceive (context, intent); If (intent. getaction (). equals ("cn.com. karl. widget. click ") {toast. maketext (context, "click widget Calendar", 1 ). show ();}}}
The above code has forgotten to make comments. In this type of interpretation, the remoteviews class is used to load the corresponding ID of the layout file to set the value, and then pendingintent is no good explanation.
Add the following to manifest:
<receiver android:name="ExampleAppWidgetProvider" > <intent-filter > <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> <action android:name="cn.com.karl.widget.click" > </action> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/myappwidetprovider" /> </receiver>
This completes. Run the project to see the effect of running the download on the mobile phone:
The above is the display effect of our custom appwidget. Click it:
Toast is used to print the information. You can also click it to start the corresponding activity.