FileDocCategorySizeDatePackage
AppWidgetProvider.javaAPI DocAndroid 5.1 API9480Thu Mar 12 22:22:10 GMT 2015android.appwidget

AppWidgetProvider

public class AppWidgetProvider extends android.content.BroadcastReceiver
A convenience class to aid in implementing an AppWidget provider. Everything you can do with AppWidgetProvider, you can do with a regular {@link BroadcastReceiver}. AppWidgetProvider merely parses the relevant fields out of the Intent that is received in {@link #onReceive(Context,Intent) onReceive(Context,Intent)}, and calls hook methods with the received extras.

Extend this class and override one or more of the {@link #onUpdate}, {@link #onDeleted}, {@link #onEnabled} or {@link #onDisabled} methods to implement your own AppWidget functionality.

Developer Guides

For more information about how to write an app widget provider, read the App Widgets developer guide.

Fields Summary
Constructors Summary
public AppWidgetProvider()
Constructor to initialize AppWidgetProvider.

    
Methods Summary
public voidonAppWidgetOptionsChanged(android.content.Context context, AppWidgetManager appWidgetManager, int appWidgetId, android.os.Bundle newOptions)
Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_OPTIONS_CHANGED} broadcast when this widget has been layed out at a new size. {@more}

param
context The {@link android.content.Context Context} in which this receiver is running.
param
appWidgetManager A {@link AppWidgetManager} object you can call {@link AppWidgetManager#updateAppWidget} on.
param
appWidgetId The appWidgetId of the widget whose size changed.
param
newOptions The appWidgetId of the widget whose size changed.
see
AppWidgetManager#ACTION_APPWIDGET_OPTIONS_CHANGED

    
public voidonDeleted(android.content.Context context, int[] appWidgetIds)
Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_DELETED} broadcast when one or more AppWidget instances have been deleted. Override this method to implement your own AppWidget functionality. {@more}

param
context The {@link android.content.Context Context} in which this receiver is running.
param
appWidgetIds The appWidgetIds that have been deleted from their host.
see
AppWidgetManager#ACTION_APPWIDGET_DELETED

    
public voidonDisabled(android.content.Context context)
Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_DISABLED} broadcast, which is sent when the last AppWidget instance for this provider is deleted. Override this method to implement your own AppWidget functionality. {@more}

param
context The {@link android.content.Context Context} in which this receiver is running.
see
AppWidgetManager#ACTION_APPWIDGET_DISABLED

    
public voidonEnabled(android.content.Context context)
Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_ENABLED} broadcast when the a AppWidget for this provider is instantiated. Override this method to implement your own AppWidget functionality. {@more} When the last AppWidget for this provider is deleted, {@link AppWidgetManager#ACTION_APPWIDGET_DISABLED} is sent by the AppWidget manager, and {@link #onDisabled} is called. If after that, an AppWidget for this provider is created again, onEnabled() will be called again.

param
context The {@link android.content.Context Context} in which this receiver is running.
see
AppWidgetManager#ACTION_APPWIDGET_ENABLED

    
public voidonReceive(android.content.Context context, android.content.Intent intent)
Implements {@link BroadcastReceiver#onReceive} to dispatch calls to the various other methods on AppWidgetProvider.

param
context The Context in which the receiver is running.
param
intent The Intent being received.

        // Protect against rogue update broadcasts (not really a security issue,
        // just filter bad broacasts out so subclasses are less likely to crash).
        String action = intent.getAction();
        if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
            Bundle extras = intent.getExtras();
            if (extras != null) {
                int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
                if (appWidgetIds != null && appWidgetIds.length > 0) {
                    this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);
                }
            }
        } else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
            Bundle extras = intent.getExtras();
            if (extras != null && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
                final int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
                this.onDeleted(context, new int[] { appWidgetId });
            }
        } else if (AppWidgetManager.ACTION_APPWIDGET_OPTIONS_CHANGED.equals(action)) {
            Bundle extras = intent.getExtras();
            if (extras != null && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)
                    && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS)) {
                int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
                Bundle widgetExtras = extras.getBundle(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS);
                this.onAppWidgetOptionsChanged(context, AppWidgetManager.getInstance(context),
                        appWidgetId, widgetExtras);
            }
        } else if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) {
            this.onEnabled(context);
        } else if (AppWidgetManager.ACTION_APPWIDGET_DISABLED.equals(action)) {
            this.onDisabled(context);
        } else if (AppWidgetManager.ACTION_APPWIDGET_RESTORED.equals(action)) {
            Bundle extras = intent.getExtras();
            if (extras != null) {
                int[] oldIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_OLD_IDS);
                int[] newIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
                if (oldIds != null && oldIds.length > 0) {
                    this.onRestored(context, oldIds, newIds);
                    this.onUpdate(context, AppWidgetManager.getInstance(context), newIds);
                }
            }
        }
    
public voidonRestored(android.content.Context context, int[] oldWidgetIds, int[] newWidgetIds)
Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_RESTORED} broadcast when instances of this AppWidget provider have been restored from backup. If your provider maintains any persistent data about its widget instances, override this method to remap the old AppWidgetIds to the new values and update any other app state that may be relevant.

This callback will be followed immediately by a call to {@link #onUpdate} so your provider can immediately generate new RemoteViews suitable for its newly-restored set of instances. {@more}

param
context
param
oldWidgetIds
param
newWidgetIds

    
public voidonUpdate(android.content.Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_UPDATE} and {@link AppWidgetManager#ACTION_APPWIDGET_RESTORED} broadcasts when this AppWidget provider is being asked to provide {@link android.widget.RemoteViews RemoteViews} for a set of AppWidgets. Override this method to implement your own AppWidget functionality. {@more}

param
context The {@link android.content.Context Context} in which this receiver is running.
param
appWidgetManager A {@link AppWidgetManager} object you can call {@link AppWidgetManager#updateAppWidget} on.
param
appWidgetIds The appWidgetIds for which an update is needed. Note that this may be all of the AppWidget instances for this provider, or just a subset of them.
see
AppWidgetManager#ACTION_APPWIDGET_UPDATE