One
1, the App widget is a space on the home desktop, such as the clock, the player when the small icon can click the next song and so on.
2. Appwidgetproviderinfo object: Provide metadata for the app Widget, including layout, update frequency and other data, this object is defined in the XML file.
3. Appwidgetprovider: Define the Basic life cycle function of an app Widget.
Second, create the app widget steps:
1. Define Appwidgetproviderinfo: Define the Example_appwidget_info.xml file in the Res/xml folder
android:updateperiodmills= "86400000" defines the time of the update
2. Join in Androidmanifest.xml
<receiver android:name= "Exampleappwidgetprovider" ><intent-filter><action android:name= " Android.appwidget.action.APPWIDGET_UPDATE "/></intent-filter><meta-data android:name=" Android.appwidget.provider "android:resource=" @xml/example_appwidget_info "/></receiver>
3. Implement Appwidgetprovider:
Updatate: Called after the specified update time is reached or when the user adds an app widget to the desktop.
Ondeleted: Called when the app widget is deleted.
Onenabled: Called when an instance of an app widget is created for the first time.
Ondisabled: Called when the last Appp widget instance is deleted.
Onreveice: Receive broadcast events.
For example:
1. Create an XML folder under Res and create a example_appwidget_info.xml file inside it:
<span style= "FONT-SIZE:14PX;" ><appwidget-provider xmlns:android= "http://schemas.android.com/apk/res/android" android:minwidth= " 294DP " android:minheight=" 72DP " android:updateperiodmillis=" 86400000 " android:initiallayout=" @ Layout/example_appwidget "></appwidget-provider></span><pre class=" java "name=" code ">
2. Join in ANDROIDMANIFEST.XM:
<pre class= "html" name= "code" > <receiver android:name= "Exampleappwidgetprovider" ><intent-filter> <action android:name= "Android.appwidget.action.APPWIDGET_UPDATE"/></intent-filter><meta-data Android:name= "Android.appwidget.provider" android:resource= "@xml/example_appwidget_info"/></receiver>
3. Write the Code:
Package Mars.appwidget01;import Android.appwidget.appwidgetmanager;import Android.appwidget.AppWidgetProvider; Import Android.content.context;public class Exampleappwidgetprovider extends appwidgetprovider{@Overridepublic void OnUpdate (context context, Appwidgetmanager appwidgetmanager,int[] appwidgetids) {System.out.println ("onUpdate"); Super.onupdate (context, Appwidgetmanager, appwidgetids);} @Overridepublic void ondeleted (context context, int[] appwidgetids) {System.out.println ("ondeleted"); super.ondeleted (context, appwidgetids);} @Overridepublic void ondisabled (context context) {System.out.println ("ondisabled"); super.ondisabled (context);} @Overridepublic void onenabled (context context) {System.out.println ("onenabled"); super.onenabled (context);}}
Appwidget, such as the clock on the Android desktop