Android Widget Development case implementation is introduced in this article, mainly to understand and learn about the development of Android widgets, today we want to write about the development of Android widget, as soon as the early morning, I will not say too specific, comrades to imitate it!
First, keep abreast of the main classes of the app widget framework:
Appwidgetprovider: Inherits from Broadcastreceiver, accepts notifications when app widget applies update,enable,disable and deleted. Among them, onupdate,onreceive is the most commonly used method.
Appwidgetproviderinfo: Describes the size of the appwidget, update frequency and the initial interface information, in the form of an XML file in the application of the Res/xml directory.
Appwidgetmanager: Responsible for managing Appwidget, sending notifications to Appwidgetprovider.
Remoteviews: A class that can run in other application processes and is the core of the construction appwidget.
The following starts the code to write, first establishes the myappwidetprovider.xml under the Res/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 "
>
The above is defined as the widget's width, height, update cycle, and layout widget layout.
Here 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/w IDGET_BG1 "android:gravity=" center "android:id=" @+id/layout "android:orientation=" vertical "> <textvie
W 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=" @+i D/txtday "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:textColor=" #9900 "Android:textsize=" 25DP "android:text=" "/> <textview android:id=" @+id/txtweekday "Android:l" Ayout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_margin= "2DP" Android:textCol Or= "#000000" android:text=""/> </LinearLayout>
Corresponding to the layout of the widget requirements are relatively high, we design, 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={"January", "February", "March", "April", "May", "
June ", July", "August", "September", "October", "November", "December"};
Private string[] days={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; @Override public void OnUpdate (context context, Appwidgetmanager Appwidgetmanager, int[] appwidgetids) {// TODO auto-generated Method Stub remoteviews remoteviews=new remoteviews (Context.getpackagename (), R.layout.mya
Ppwidget);
Time Time=new time ();
Time.settonow ();
String month=time.year+ "" +months[time.month];
Remoteviews.settextviewtext (R.id.txtday, New Integer (Time.monthday). toString ()); Remoteviews.settextviewtext (R.Id.txtmonth, month);
Remoteviews.settextviewtext (R.id.txtweekday, Days[time.weekday]);
Intent intent=new Intent ("Cn.com.karl.widget.click");
Pendingintent pendingintent=pendingintent.getbroadcast (context, 0, intent, 0);
Remoteviews.setonclickpendingintent (R.id.layout, pendingintent);
Appwidgetmanager.updateappwidget (Appwidgetids, remoteviews);
Super.onupdate (context, Appwidgetmanager, appwidgetids);
@Override public void OnReceive (context context, Intent Intent) {//TODO auto-generated stub
Super.onreceive (context, intent);
if (Intent.getaction (). Equals ("Cn.com.karl.widget.click")) {Toast.maketext (context, "clicked Widget Calendar", 1). Show ();
}
}
}
The above code forgot to do the annotation, in this kind of explanation, uses the Remoteviews class separately to load the corresponding ID of the layout file to set the good value, however pendingintent this has no good explanation.
Finally, add in the 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"/>
So it's done, run the project and look at the effect of running on the phone:
The above is our own definition of the Appwidget display effect, click on it:
In order to indicate click on it, use the toast Print information, of course, we can also click it to start the corresponding activity.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.