Add widgets on the screen: or tap and hold the screen blank, or find the Widgetpreview app selection. Native system 4.0 or less using long press, 4.0 and above open widgets
General steps for creating widgets:
In the Menifest
<receiver android:name= "Com.stone.ui.TimerWidgetProvider" > <intent-filter> <action Android:name= "Android.appwidget.action.APPWIDGET_UPDATE"/> <!--custom action-- <action Android : Name= "Com.stone.action.start"/> </intent-filter> <meta-data android:name= " Android.appwidget.provider " android:resource=" @xml/timer_widget_provider "/> <!-- Android: Resource= "" Defines the widget's information using the Timer_widget_provider.xml description--></receiver>
Widget description File: timer_widget_provider.xml
<?xml version= "1.0" encoding= "Utf-8"? ><appwidget-provider xmlns:android= "http://schemas.android.com/apk/ Res/android " android:minwidth=" 180DP " android:minheight=" 40DP " android:updateperiodmillis=" 5000 " android:previewimage= "@drawable/ic_launcher" android:initiallayout= "@layout/timer_widget" Android : resizemode= "horizontal|vertical" ><!--formula for calculating size: (70*n) -30 n the size required for the part (a few cells) the current is 3x1minresizewidth minresizeheight can be adjusted to the minimum width of height, if greater than minwidth minheight ignore label Select part when you see the tag icon to see icons updateperiodmillis update interval When you select a part previewimage the image that is displayed when you select a part 3.0 use the initiallayout layout file ResizeMode Adjust size mode Configure if you need to start an activity to set up before starting, give the full class name of activity here [email protected]/ XX is used in conjunction with the Assembly assembly to specify the view ID that should be automatically advanced by the host of the part : 3.0. View:listview, GridView, StackView, Adapterviewfilpper--></appwidget-provider>
Layout used by widget: Timer_widget.xml
<?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:o rientation= "Horizontal" android:background= "@drawable/widget_background" > <textview android: Id= "@+id/counter" android:layout_width= "0DP" android:layout_height= "match_parent" android:layout_ weight= "1" android:gravity= "center" android:text= "00:00" android:singleline= "true" Android: Textappearance= "? android:attr/textappearance"/><imageview android:id= "@+id/start_stop" Android : layout_width= "0DP" android:layout_height= "match_parent" android:layout_weight= "1" android: Scaletype= "center" android:src= "@android:d rawable/ic_media_play"/></linearlayout>
Handling widget-related events Appwidgetprovider
Package Com.stone.ui;import Java.util.arrays;import Com.stone.r;import com.stone.service.timerwidgetservice;import Android.app.pendingintent;import Android.appwidget.appwidgethost;import Android.appwidget.AppWidgetManager; Import Android.appwidget.appwidgetprovider;import Android.content.componentname;import android.content.Context; Import Android.content.intent;import android.text.format.dateutils;import android.widget.remoteviews;/* * Appwidgetprovider must receive Android.appwidget.action.APPWIDGET_UPDATE broadcast * */public class Timerwidgetprovider extends Appwidgetprovider {@Override//update part is called and public void OnUpdate is also called when the 1th time the part is added (context context, Appwidgetmanager Appwidgetmanager,int[] appwidgetids) {super.onupdate (context, Appwidgetmanager, appwidgetids); System.out.println ("OnUpdate widget:" + arrays.tostring (appwidgetids));/* Construction pendingintent Broadcast, onreceive () according to the received broadcast, Update */intent Intent = new Intent (context, timerwidgetservice.class); Pendingintent pendingintent = pendingintent.getservice (context, 0, intent, 0); Remoteviews RV = new Remoteviews (Context.getpackagename (), r.layout.timer_widget); Rv.setonclickpendingintent ( R.id.start_stop, pendingintent); Appwidgetmanager.updateappwidget (Appwidgetids, RV);} @Override//Parts removed from host public void ondeleted (context context, int[] appwidgetids) {super.ondeleted (context, Appwidgetids); System.out.println ("ondeleted widget");} The @Override//1th time the call is created, and then created without calling public void onenabled (context context) {super.onenabled (context); System.out.println ("onenabled widget");} @Override//Call to clear onenabled execution when the last part instance is deleted public void ondisabled (context context) {super.ondisabled (context); System.out.println ("ondisabled widget");} @Override//public void OnReceive (context context, Intent Intent) {super.onreceive (context, Intent); System.out.println ("OnReceive widget");/* * Receive <action android:name= "Com.stone.action.start"/> In other components or Activ ity or service sends these broadcast */if (Intent.getaction (). Equals ("Com.stone.action.start")) {long time = intent.getlongextrA ("Time", 0); Updatewidget (context, time); System.out.println ("Receive Com.stone.action.start");}} private void Updatewidget (context context, long time) {//remoteviews handles Viewremoteviews RV = new Remoteviews in an XOR process ( Context.getpackagename (), r.layout.timer_widget); System.out.println ("time=" + Time); Rv.settextviewtext (R.id.counter, Dateutils.formatelapsedtime (time/1000)); Appwidgetmanager am = appwidgetmanager.getinstance (context); int[] Appwidgetids = am.getappwidgetids (New ComponentName (context, timerwidgetprovider.class)); Am.updateappwidget (Appwidgetids, RV);//Update All instances}}
The timerwidgetservice used in Appwidgetprovider
Package Com.stone.service;import Android.app.service;import Android.content.intent;import android.os.IBinder; public class Timerwidgetservice extends Service {@Overridepublic ibinder onbind (Intent Intent) {return null;} @Overridepublic void OnCreate () {super.oncreate ();} @Overridepublic int Onstartcommand (Intent Intent, int flags, int startid) {/* * do some thing *///send broadcast notification widget update status SENDB Roadcast (New Intent ("Com.stone.action.start"). PutExtra ("Time", System.currenttimemillis ()); return Service.start_ STICKY;}}