Rewrite, original address: http://blog.csdn.net/android_tutor/article/details/5544471
Widgets are a pendant in Android. Once upon a time, the thing that was thought to be revolutionary is now slowly declining. There are many reasons for it. Flow, power consumption, memory ...
The following is still the use of the widget:
First Look at:
1. First create a new XML folder under the Res folder. Note is a folder. Then create a new XML file under this folder:widget_provider.xml. Resouce-type Choose Appwidget-provider,
The contents of the XML file are as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><appwidget-provider xmlns:android= "http://schemas.android.com/apk/ Res/android " android:minwidth=" 50DP " android:minheight=" 50DP " android:updateperiodmillis=" 10000 " android:initiallayout= "@layout/main"/>
2. Modify the Main.xml file for the layout folder:
<?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 " android:background=" @drawable/git " ><textview android:id=" @+id/wordcup " Android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:text= "Hello" android: Textsize= "12px" android:textcolor= "#ff0000" /></linearlayout>
3. Modify Widetdemo.java
Package Com.wf.mywidgetdemo;import Java.util.calendar;import Java.util.date;import Java.util.GregorianCalendar; Import Java.util.timer;import Java.util.timertask;import Android.appwidget.appwidgetmanager;import Android.appwidget.appwidgetprovider;import Android.content.componentname;import Android.content.Context;import Android.widget.remoteviews;public class Widetdemo extends appwidgetprovider{/** called when the activity is first created . */@Override public void onUpdate (context context, Appwidgetmanager Appwidgetmanager, int[] Appwidgetid s) {Timer timer = new timer (); Timer.scheduleatfixedrate (New MyTime (Context,appwidgetmanager), 1, 60000); Super.onupdate (context, Appwidgetmanager, appwidgetids); } private class MyTime extends timertask{remoteviews remoteviews; Appwidgetmanager Appwidgetmanager; ComponentName Thiswidget; Public MyTime (Context Context,appwidgetmanager appwidgetmanager) {This.appwidgetManager = Appwidgetmanager; Remoteviews = new Remoteviews (Context.getpackagename (), r.layout.main); Thiswidget = new ComponentName (context,widetdemo.class); @Overridepublic void Run () {Date date = new Date (); Calendar calendar = new GregorianCalendar (2018,06,8); Long days = ((((Calendar.gettimeinmillis ()-date.gettime ())/1000))/86400; Remoteviews.settextviewtext (R.id.wordcup, "World Cup from Russia" + days+ "Day"); Appwidgetmanager.updateappwidget (Thiswidget, remoteviews); } }}
4. Modify the Androidmanifest.xml file:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.wf.mywidgetdemo "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <application android:allowbackup=" tr UE "android:icon=" @drawable/ic_launcher "android:label=" @string/app_name "android:theme=" @style/appth EME "> <receiver android:name=". Widetdemo "android:label=" @string/app_name "> <intent-filter> < Action android:name= "Android.appwidget.action.APPWIDGET_UPDATE"/> </intent-filter> &l T;meta-data android:name= "Android.appwidget.provider" android:resource= "@xml/widget_provider" /> </receiver> </application></manifest>
Get.
Android widgets on the path of a pro