AppWidget, such as the clock on the android desktop, appwidgetandroid

Source: Internet
Author: User

AppWidget, such as the clock on the android desktop, appwidgetandroid

I,
1. The App widget is a space on the home desktop, such as a clock. You can click the next icon in the player to play the video.

2. AppWidgetProviderInfo object: provides metadata for App widgets, including layout, update frequency, and other data. This object is defined in an xml file.

3. AppWidgetProvider: defines the basic lifecycle function of an App Widget.

2. Steps for creating an App Widget:
1. Define AppWidgetProviderInfo: Define the example_appwidget_info.xml file in the res/xml folder.
Android: updatePeriodMills = "86400000" defines the Update Time

2. Add 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: it is called after the specified Update time or when the user adds an App Widget to the desktop.
OnDeleted: called when the App widget is deleted.
OnEnabled: called when an App widget instance is created for the first time.
OnDisabled: called when the last Appp Widget instance is deleted.
OnReveice: receives broadcast events.

For example:

1. Create an xml folder under res and create the example_appwidget_info.xml file in 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. Add 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);}}


 

 

 

 

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.