APP Widget
All the controls are called Widgets,
1. What is an app Widget
2. Create an app Widget
The controls on the desktop. shortcuts, folders, Wallpapers
Basic concepts related to app widgets
1, Appwidgetproviderinfo object: For the app widget to provide metadata (describing data, relational database table structure is also metadata), including layout, update frequency and so on data. This object is defined in the XML file.
2, Appwidgetprovider: Defines the basic life cycle of the app widget.
1. Steps to create your first app widget
Here's how to write the steps:
1. Provide a meta-file layout appwidgetproviderinfo for Appwidget to display the widget's interface,
2. Create a widgetprovider inherit from Appwidgetprovider;
3. Creating a layout file for Widgetprovider can also be used directly with main.xml;
Code section:
1) Provide appwidgetproviderinfo layout file res/xml/example_appwidget_info. Xml
Note: This layout file is placed inside of layout, and is placed on Res/xml, creating its own XML.
<?XML version= "1.0" encoding= "Utf-8"?><Appwidget-providerxmlns:android= "Http://schemas.android.com/apk/res/android"Android:minwidth= "40DP"
Android:minheight= "40DP"Android:updateperiodmillis= "86400000"Android:previewimage= "@drawable/example_appwidget_preview"Android:initiallayout= "@layout/new_app_widget"Android:resizemode= "Horizontal|vertical"android:widgetcategory= "Home_screen"Android:initialkeyguardlayout= "@layout/new_app_widget"></Appwidget-provider>
Android:updateperiodmillis The update time, in milliseconds.
Android:initialkeyguardlayout initializes the layout, referencing a layout file, inside the layouts.
2) Specify the style and layout for the app widget:
Define a new layout file example_appwidget.xml. It's just a simple textview.
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:padding= "@dimen/widget_margin"Android:background= "#09C">
<TextViewAndroid:id= "@+id/appwidget_text"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_centerhorizontal= "true"android:layout_centervertical= "true"Android:text= "@string/appwidget_text"Android:textcolor= "#ffffff"android:textsize= "24SP"Android:textstyle= "Bold|italic"Android:layout_margin= "8DP"android:contentdescription= "@string/appwidget_text"Android:background= "#09C" /></Relativelayout>
3) Implement Appwidgetprovider this class
/*** Implementation of APP Widget functionality. * This is a request for distribution. */ Public classNewappwidgetextendsAppwidgetprovider {//This method is called after the specified update time is reached or when the user adds an app widget like a desktop. @Override Public voidOnUpdate (context context, Appwidgetmanager Appwidgetmanager,int[] appwidgetids) { //There may is multiple widgets active, so update all of them Final intN =appwidgetids.length; for(inti = 0; i < N; i++) {updateappwidget (context, Appwidgetmanager, appwidgetids[i]); } } //This method is called when the strength of the first app widget is created for the first time. @Override Public voidonenabled (Context context) {//Enter relevant functionality for if the first widget is created } //This method is called when the last app widget instance is deleted. @Override Public voidondisabled (Context context) {//Enter relevant functionality for if the last widget is disabled } //This method is called when the app Widgt is deleted. @Override Public voidOndeleted (Context context,int[] appwidgetids) { Super. ondeleted (context, appwidgetids); } //Accept broadcast events. @Override Public voidOnReceive (Context context, Intent Intent) {Super. OnReceive (context, intent); } Static voidUpdateappwidget (context context, Appwidgetmanager Appwidgetmanager,intAppwidgetid) {charsequence Widgettext=context.getstring (R.string.appwidget_text); //Construct the Remoteviews objectRemoteviews views =Newremoteviews (Context.getpackagename (), r.layout.example_appwidget); Views.settextviewtext (R.id.appwidget_text, Widgettext); //Instruct the widget Manager to update the widgetAppwidgetmanager.updateappwidget (Appwidgetid, views); }}
4, note that this is a broadcasting mechanism. Declare it inside the mainifest.xml.
<receiver android:name= ". Newappwidget "> <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 >
android--Common Controls--app widgets (i)