SoFunction
Updated on 2025-03-11

Appwidget usage analysis for Android development

This article describes the usage of Android Appwidget. Share it for your reference, as follows:

App Widgets is a view of a small application. It can be embedded in other applications (such as desktop programs) and can be refreshed periodically.

Before creating an App Widget, you need to understand the following concepts

AppWidgetProviderInfo object

It is a description of App Widget metadata, such as AppWidget layout, refresh frequency, and AppWidgetProvider class. These metadata are all defined in XML.

Implementation of AppWidgetProvider class

For App Widget, some basic methods are defined (callback interface), which are based on broadcast events, through which you can receive broadcasts when the App Widget is updated, created, invalid, and uninstalled.

View layout

Define the initial layout for the App Widget and use XML to layout

In addition, you can implement a configured activity for the App Widget, which is an optional activity. When the user adds your App Widget and is ready to create it, he can modify or configure the App Widget.

Let's start creating an App Widget

① Declare App Widget in Manifest

First, declare AppWidgetProvider in

<receiver android:name="ExampleAppWidgetProvider" >
      <intent-filter>
       <action android:name=".APPWIDGET_UPDATE" />
      </intent-filter>
      <meta-data android:name=""
        android:resource="@xml/example_appwidget_info" />
</receiver>

The <receiver> element requires the Android:name attribute, and the AppWidgetProvider is used as a reference
The <intent-filter>  Element must contain the <action> element. Then there must be an android:name attribute in the Action. This attribute needs to specify the AppWidgetProvider that can receive ACTION_APPWIDGET_UPDATE broadcast, that is, the ExampleAppWidgetProvider. This broadcast must be unique and must be explicitly declared, because AppWidgetManager will automatically send all other applications' broadcasts to AppWidgetProvider, so it is very important.

The <meta-data> element specifies that the AppWidgetProviderInfo resource requires the following properties
Android:name - specifies the name of the metadata, and describes it as AppWidgetProviderInfo.
Android:resource - Location of reference to AppWidgetProviderInfo resource
In short, in the main statement concept AppWidgetProvider and AppWidgetProviderInfo

②Add AppWidgetProviderInfo metadata

AppWidgetProviderInfo defines some of the most basic data for AppWidgeet, such as the minimum size of the layout, initializing the layout resources, how to update the App Widget, and the configurable activity when creating the app Widget (optional)

To define an AppWidgetProviderInfo object, you can use an XML resource file to define it, save it in res/xml/its own file name, and use a single element in the XML file <appwidget-provider>. Please see the following example:

<appwidget-provider xmlns:andro
  android:minWidth="294dp"
  android:minHeight="72dp"
  android:updatePeriodMillis="86400000"
  android:previewImage="@drawable/preview"
  android:initialLayout="@layout/example_appwidget"
  android:configure=""
  android:resizeMode="horizontal|vertical">
</appwidget-provider>

initialLayout: Specify the App Widget layout resource file
Configure: The activity to configure the properties for the App Widget when creating it
updatePeriodMillis: appwidget update frequency

③ Create App Widget Layout

You must define an initialization layout file for your App Widget, and you can place the layout file in res/layout/directory. You can design your App Widget as listed below
View object, but before your design begins, please read App Widget Design Guidelines.

If you are familiar with XML Layouts, it will be simple to create an App Widget, however, you must realize that the App Widget layout is based on RemoteViews, which does not support every layout and View widget.

A RemoteView object can only support the following layout classes:
FrameLayout  LinearLayout   RelativeLayout

Supports the following widget classes:
AnalogClock   Button  Chronometer  ImageButton  ImageView  ProgressBar  TextView  ViewFlipper  ListView  GridView
StackView  AdapterViewFlipper

PS: The inheritance classes of these classes are not supported either.

Below is the Appwidget from the demo I wrote this time

The function is simple just to understand AppWidget first and respond to button events on Appwidget

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class ExampleAppWidgetProvider extends AppWidgetProvider{
  private static final String ACTION = "";
  private int id;
  @Override
  public void onDeleted(Context context, int[] appWidgetIds) {
    ("onDeleted");
    (context, appWidgetIds);
  }
  @Override
  public void onDisabled(Context context) {
    // TODO Auto-generated method stub
    ("onDisabled");
    (context);
  }
  @Override
  public void onEnabled(Context context) {
    // TODO Auto-generated method stub
    ("onEnabled");
    (context);
  }
  @Override
  public void onReceive(Context context, Intent intent) {
    ("onReceive");
    // Defined ACTION    if(().equals(ACTION))
    {
      RemoteViews remoteView = new RemoteViews((),.appwidget_layout);
      (, "setText", "22222222222222");
      AppWidgetManager appWidgetManager = (context);
      ComponentName componentName = new ComponentName(context, );
      (componentName , remoteView);
      ("Custom ACTION received");
    }else{
      (context, intent);
    }
  }
  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
      int[] appWidgetIds) {
    int N = ;
    for(int i = 0; i&lt;N; i++)
    {
      int appWidgetId = appWidgetIds[i];
      Intent intent = new Intent(ACTION);
      PendingIntent pendingIntent = (context, 0, intent, 0);
      RemoteViews remoteViews = new RemoteViews((),.appwidget_layout);
      (, pendingIntent);
      (appWidgetId, remoteViews);
      (appWidgetId);
    }
    (context, appWidgetManager, appWidgetIds);
  }
}

Every time you add an Appwidget instance, the onUpdate() method will be called and the Button Onclick event will be registered. When you click the button, a broadcast will be sent. This broadcast is defined by yourself. Then the onReceive method will receive the broadcast and then make some reactions. I am modifying the text on the TextView.

Since Appwidget and its own application are in a program, they are not in a process when running, so there is some limitation on calling methods and modifying interfaces. It is not as free as ordinary (some operations on Views in Activity). So if you want to do some operations on the View on Appwidget or update Appwidget, you generally need to use RemoteViews. AppWidgetManager may also use ComponentName

Personal understanding: RemoteViews is mainly used to represent a collection of Views in an Appwidget instance when created, while ComponentName represents an entire Appwidget instance.

Click here for the full example codeDownload this site

For more information about Android related content, please check out the topic of this site:Android programming activity operation skills summary》、《Android resource operation skills summary》、《Android file operation skills summary》、《Summary of Android's SQLite database skills》、《Summary of Android data skills for operating json format》、《Android database operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android development introduction and advanced tutorial》、《Android View View Tips Summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.