"Android Notes" Widget

Source: Internet
Author: User

What is a widget?The App widget is a desktop gadget from Android that can be embedded on the desktop and regularly updated with its own data.
As shown in the following:

How do I create widgets?
Creating a widget requires the following components: 1AppWidgetProviderInfo: This class provides widget metadata such as widget layout, update frequency, size, and so on. It is usually defined in XML with a location of Res/xml. 2.AppWidgetProvider: This class is a broadcast recipient that receives some broadcast information, such as whether the widget is available, the widget is updated, the widget is deleted, and so on. In general, we need to inherit this class and replicate the life-cycle methods such as onxxx. 3.Widget layout: Creating a widget requires specifying its layout, which is defined under Res/layout. 4.AppWidgetManager: This class updates the state of the widget and can obtain information about the Appwidgetprovider that have been registered.
steps to create the widget: 1. Define a class to inherit Appwidgetprovider:
Package Com.example.widget;import Android.appwidget.appwidgetmanager;import Android.appwidget.AppWidgetProvider; Import Android.content.context;public class Mywidgetprovider extends appwidgetprovider{@Overridepublic void onenabled (Context context) {super.onenabled (context);} @Overridepublic void OnUpdate (context context, Appwidgetmanager appwidgetmanager,int[] appwidgetids) {super.onupdate ( Context, Appwidgetmanager, appwidgetids);} @Overridepublic void ondisabled (context context) {super.ondisabled (context);}}
2. Define the layout of the widget under Res/layout:For example, called Widget_layout.xml.
<?xml    Version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "Http://schemas.android.com/apk/res/android" Android:layout_width= "Match_parent" android:layout_height= "match_parent" android:padding= "2DP" > <Linea Rlayout android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "Horizo Ntal "> <button android:id=" @+id/btn "android:layout_width=" Wrap_cont Ent "android:layout_height=" wrap_content "android:text=" to open the Software "android:textcolor=" @android : Color/black "/> <textview android:layout_width=" Wrap_content "Android:layo ut_height= "Wrap_content" android:text= "This is a widget"/> </linearlayout></framelayout> ;
3. Create an XML resource under the Res/xml directory to define the basic information for the widget:For example, called: App_widget_info.xml
<appwidget-provider xmlns:android= "http://schemas.android.com/apk/res/android"    android:initiallayout= "@ Layout/widget_layout "    android:minheight=" 50DP "    android:minwidth=" 290DP "    Android:updateperiodmillis = "86400000" ></appwidget-provider>
There are some other tags that you use, see documentation.
4. Configure Appwidgetprovider in the manifest file:
<receiver android:name= "Com.example.widget.MyWidgetProvider" >            <intent-filter><!--action Name is fixed--                <action android:name= "Android.appwidget.action.APPWIDGET_UPDATE"/>            </ intent-filter>            <meta-data                android:name= "Android.appwidget.provider"                android:resource= "@xml/ App_widget_info "/></receiver>
At this point, the widget is created with the following effects:

This time the button is no click Effect, then how to increase the click Response to the event? It's going to be used to Appwidgetmanager. Before we do that, let's start by introducing the life cycle of the widget, you can hit log in the above Mywidgetprovider, by analyzing the log we can draw the following conclusions: 1. The first widget is dragged onto the desktop: onenabled ()--->onupdate () 2. After each widget is dragged to the desktop: onUpdate () 3. Delete a widget:ondeleted () 4. The last widget was deleted: ondeleted ()-->onenabled ()In addition, if you make a copy of the OnReceive method, the OnReceive method is executed first (ensure that you are calling Super.onreceive). In fact, we analyze the Appwidgetprovider source code is very easy to see, appwidgetprovider in the OnReceive method by judging the type of action, to perform different business methods, and the specific business method is written by the subclass.
5. Add Response EventsSince each widget is dragged to the desktop and calls OnUpdate, we'll add a listener to the button in this method:
@Overridepublic void OnUpdate (context context, Appwidgetmanager appwidgetmanager,int[] appwidgetids) {log.d (TAG, " OnUpdate ... "); Remoteviews views = new Remoteviews (Context.getpackagename (), r.layout.widget_layout); Pendingintent pendingintent = pendingintent.getactivity (context, 0, new Intent (Context,mainactivity.class), 0); Views.setonclickpendingintent (R.ID.BTN, pendingintent); Appwidgetmanager.updateappwidget (appWidgetIds, views);}
This requires the Appwidgetmanager Updateappwidget method, which needs to pass in the widget ID and remoteviews. This remoteviews is critical, and we have to invoke the Setonclickpendingintent method of this class to add a response event to the button, of course, it also provides many other methods, such as Settextviewtext, Setprogressbar and so on, these methods have one thing in common, that is, the ID of the view must be passed in. For more usage, see the documentation. By the above method, when we click on the button, we can open the main interface!
Widgets typically need to be used with service. Because widgets need to update information regularly, a backend service is essential. At this time we can start the service in onenabled, and then add a timer (timer or Alarmmanager) to the service, update the widget regularly, this time can be
Appwidgetmanager.getinstance (context);
Gets the Appwidgetmanager instance.

The above is the basic use of widgets, see the documentation for more usage.










"Android Notes" Widget

Related Article

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.