Appwidget like the clock on the Android desktop

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.