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 );
}
}