[Android Notes] widgets and Android Widgets

Source: Internet
Author: User

[Android Notes] widgets and Android Widgets
What is a Widget? The App Widget is a desktop tool provided by android. It can be embedded into the desktop and can regularly update its own data.
As shown in:

How to Create widgets?
The following components are required to create a Widget: 1 AppWidgetProviderInfo: This class provides metadata for widgets, such as Widget layout, update frequency, and size. It is usually defined in xml, and its location is res/xml. 2. AppWidgetProvider: this class is a broadcast receiver used to receive broadcast information, such as whether the widget is available, update the widget, and delete the widget. Generally, we need to inherit this class and rewrite the life cycle methods such as onXXX. 3. Widget layout: to create a Widget, you must specify its layout. You can define the layout under res/layout. 4. AppWidgetManager: This class updates the Widget status and obtains the registered AppWidgetProvider information.
To create a Widget: 1. Define a class to inherit from 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 widget layout under res/layout:For example, 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"> <LinearLayout android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "horizontal"> <Button android: id = "@ + id/btn" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Open Software" android: textColor = "@ android: color/black"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "this is a widget"/> </LinearLayout> </FrameLayout>
3. Create an xml resource in the res/xml directory and define the basic information of the widget:For example, 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 also some other labels. For usage instructions, see the document.
4. Configure AppWidgetProvider in the configuration file:
<Cycler 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 "/> </er ER>
So far, the Widget has been created, and the effect is as follows:

At this time, the button does not have a click effect. How can I add a response event for the button to be clicked? You need to use AppWidgetManager. Before that, let's introduce Widget Lifecycle, You can log in MyWidgetProvider above. By analyzing the log, we can draw the following conclusion: 1. The first widget is dragged to the desktop: onEnabled () ---> onUpdate () 2. After each widget is dragged to the desktop: onUpdate () 3. delete a Widget: onDeleted () 4. The last widget is deleted: onDeleted () --> onEnabled ()In addition, if you re-write the onReceive method, the onReceive method is the first to be executed (make sure to call super. onReceive ). In fact, we can easily analyze the AppWidgetProvider source code. AppWidgetProvider can execute different business methods by judging the action type in the onReceive method, specific business methods are rewritten by sub-classes.
5. Add a response eventSince every widget is dragged to the desktop, onUpdate is called. In this method, we add a listener for the button:
@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);}
The updateAppWidget method of AppWidgetManager is used here. This method requires passing in the widget id and RemoteViews. This RemoteViews is critical. To add a response event to a button, you must call the setOnClickPendingIntent method of this class. Of course, it also provides many other methods, such as setTextViewText and setProgressBar, these methods share the same thing, that is, the view id must be passed in. For more information, see the document. Through the above method, when we click the button, we can open the main interface!
Widgets usually need to be used with the service. Because widgets need to update information regularly, a background service is essential. At this time, we can start the service in onEnabled, add a Timer (Timer or AlarmManager) to the service, and update the widget periodically.
AppWidgetManager.getInstance(context);
Obtain the AppWidgetManager instance.

The preceding figure shows the basic usage of Widgets. For more information, see the document.










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.