APP widget for getting started with Android (1)

Source: Internet
Author: User

1. appwidgetproviderinfo
Provides metadata for app widgets, including layout and update frequency. This object is defined in an XML file.
2. appwidgetprovider
Define the basic lifecycle of an app widget

Steps for creating an app Widget:
1. Define appwidgetproviderinfo:
Define a file named example_appwidget_info.xml In the Res/XML Folder:
<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>
2. Specify the style and layout for the app widget
Define a new layout file example_appwidget.xml. In this example, just define a textview:
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "hello"
Android: Id = "@ + ID/widgettextld"
/>

3. inherit from appwidgetprovider
Onupdate: This method is called after the specified Update time or when an app widget is added to the desktop.
Ondelete: This method is called when the app widget is deleted.
Onenabled: This method is called when an app widget obtains an instance created for the first time.
Ondisabled: This method is called when the last app widget instance is deleted.
Onreveice: receives broadcast events

 

Example:

Add example_appwidget.xml to the res/layout directory.

<? 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"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "firstwidgettext"
Android: Id = "@ + ID/widgettextld"
Android: Background = "# 0000ff"
/>
</Linearlayout>

Add example_appwidget_info.xml to the res/XML directory.

<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>

Add the following code between the androidmanifest. xml file application nodes:

<Cycler Android: Name = ". appwidgetprovidertest">
<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"> </meta-data>
</Cycler>

Now, the configuration file is over. The following is the Java code:

Create an appwidgetprovidertest class that inherits the appwidgetprovider class. The content is as follows:

Import Android. appwidget. appwidgetmanager;
Import Android. appwidget. appwidgetprovider;
Import Android. content. context;
Import Android. content. intent;

Public class appwidgetprovidertest extends appwidgetprovider {

// Receives broadcast events
@ Override
Public void onreceive (context, intent ){
System. Out. println ("onreceive ");
Super. onreceive (context, intent );
}

// This method is called after the specified Update time or when the app widget is added to the desktop.
@ Override
Public void onupdate (context, appwidgetmanager,
Int [] appwidgetids ){
System. Out. println ("onupdate ");
Super. onupdate (context, appwidgetmanager, appwidgetids );
}

// This method is called when the app widget is deleted.
@ Override
Public void ondeleted (context, int [] appwidgetids ){
System. Out. println ("ondeleted ");
Super. ondeleted (context, appwidgetids );
}

// This method is called when an app widget obtains the instance created for the first time.
@ Override
Public void onenabled (context ){
System. Out. println ("onenabled ");
Super. onenabled (context );
}

// This method is called when the last app widget instance is deleted.
@ Override
Public void ondisabled (context ){
System. Out. println ("ondisabled ");
Super. ondisabled (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.