Added in API level 3

AppWidgetProvider


public class AppWidgetProvider
extends BroadcastReceiver

java.lang.Object
   ↳ android.content.BroadcastReceiver
     ↳ android.appwidget.AppWidgetProvider


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

Extend this class and override one or more of the onUpdate(Context, AppWidgetManager, int), onDeleted(Context, int), onEnabled(Context) or onDisabled(Context) 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.

Summary

Public constructors

AppWidgetProvider()

Constructor to initialize AppWidgetProvider.

Public methods

void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions)

Called in response to the AppWidgetManager.ACTION_APPWIDGET_OPTIONS_CHANGED broadcast when this widget has been layed out at a new size or its options changed via AppWidgetManager.updateAppWidgetOptions.

void onDeleted(Context context, int[] appWidgetIds)

Called in response to the AppWidgetManager.ACTION_APPWIDGET_DELETED broadcast when one or more AppWidget instances have been deleted. Override this method to implement your own AppWidget functionality.

void onDisabled(Context context)

Called in response to the 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.

void onEnabled(Context context)

Called in response to the AppWidgetManager.ACTION_APPWIDGET_ENABLED broadcast when the a AppWidget for this provider is instantiated. Override this method to implement your own AppWidget functionality.

void onReceive(Context context, Intent intent)

Implements BroadcastReceiver.onReceive to dispatch calls to the various other methods on AppWidgetProvider.

void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds)

Called in response to the 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 onUpdate(Context, AppWidgetManager, int) so your provider can immediately generate new RemoteViews suitable for its newly-restored set of instances.

In addition, you should set AppWidgetManager.OPTION_APPWIDGET_RESTORE_COMPLETED to true indicate if a widget has been restored successfully from the provider's side.

void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)

Called in response to the AppWidgetManager.ACTION_APPWIDGET_UPDATE and AppWidgetManager.ACTION_APPWIDGET_RESTORED broadcasts when this AppWidget provider is being asked to provide RemoteViews for a set of AppWidgets. Override this method to implement your own AppWidget functionality.

Inherited methods

final void abortBroadcast()

Sets the flag indicating that this receiver should abort the current broadcast; only works with broadcasts sent through Context.sendOrderedBroadcast.

final void clearAbortBroadcast()

Clears the flag indicating that this receiver should abort the current broadcast.

final boolean getAbortBroadcast()

Returns the flag indicating whether or not this receiver should abort the current broadcast.

final boolean getDebugUnregister()

Return the last value given to setDebugUnregister(boolean).

final int getResultCode()

Retrieve the current result code, as set by the previous receiver.

final String getResultData()

Retrieve the current result data, as set by the previous receiver.

final Bundle getResultExtras(boolean makeMap)

Retrieve the current result extra data, as set by the previous receiver.

String getSentFromPackage()

Returns the package name of the app that initially sent this broadcast.

int getSentFromUid()

Returns the uid of the app that initially sent this broadcast.

final BroadcastReceiver.PendingResult goAsync()

This can be called by an application in onReceive(Context, Intent) to allow it to keep the broadcast active after returning from that function.

final boolean isInitialStickyBroadcast()

Returns true if the receiver is currently processing the initial value of a sticky broadcast -- that is, the value that was last broadcast and is currently held in the sticky cache, so this is not directly the result of a broadcast right now.

final boolean isOrderedBroadcast()

Returns true if the receiver is currently processing an ordered broadcast.

abstract void onReceive(Context context, Intent intent)

This method is called when the BroadcastReceiver is receiving an Intent broadcast.

IBinder peekService(Context myContext, Intent service)

Provide a binder to an already-bound service.

final void setDebugUnregister(boolean debug)

Control inclusion of debugging help for mismatched calls to Context.registerReceiver().

final void setOrderedHint(boolean isOrdered)

For internal use, sets the hint about whether this BroadcastReceiver is running in ordered mode.

final void setResult(int code, String data, Bundle extras)

Change all of the result data returned from this broadcasts; only works with broadcasts sent through Context.sendOrderedBroadcast.

final void setResultCode(int code)

Change the current result code of this broadcast; only works with broadcasts sent through Context.sendOrderedBroadcast.

final void setResultData(String data)

Change the current result data of this broadcast; only works with broadcasts sent through Context.sendOrderedBroadcast.

final void setResultExtras(Bundle extras)

Change the current result extras of this broadcast; only works with broadcasts sent through Context.sendOrderedBroadcast.

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Public constructors

AppWidgetProvider

Added in API level 3
public AppWidgetProvider ()

Constructor to initialize AppWidgetProvider.

Public methods

onAppWidgetOptionsChanged

Added in API level 16
public void onAppWidgetOptionsChanged (Context context, 
                AppWidgetManager appWidgetManager, 
                int appWidgetId, 
                Bundle newOptions)

Called in response to the AppWidgetManager.ACTION_APPWIDGET_OPTIONS_CHANGED broadcast when this widget has been layed out at a new size or its options changed via AppWidgetManager.updateAppWidgetOptions.

Parameters
context Context: The Context in which this receiver is running.

appWidgetManager AppWidgetManager: A AppWidgetManager object you can call AppWidgetManager.updateAppWidget(ComponentName, RemoteViews) on.

appWidgetId int: The appWidgetId of the widget whose size changed.

newOptions Bundle: The new options of the changed widget.

onDeleted

Added in API level 3
public void onDeleted (Context context, 
                int[] appWidgetIds)

Called in response to the AppWidgetManager.ACTION_APPWIDGET_DELETED broadcast when one or more AppWidget instances have been deleted. Override this method to implement your own AppWidget functionality.

Parameters
context Context: The Context in which this receiver is running.

appWidgetIds int: The appWidgetIds that have been deleted from their host.

onDisabled

Added in API level 3
public void onDisabled (Context context)

Called in response to the 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.

Parameters
context Context: The Context in which this receiver is running.

onEnabled

Added in API level 3
public void onEnabled (Context context)

Called in response to the AppWidgetManager.ACTION_APPWIDGET_ENABLED broadcast when the a AppWidget for this provider is instantiated. Override this method to implement your own AppWidget functionality.

When the last AppWidget for this provider is deleted, AppWidgetManager.ACTION_APPWIDGET_DISABLED is sent by the AppWidget manager, and onDisabled(Context) is called. If after that, an AppWidget for this provider is created again, onEnabled() will be called again.

Parameters
context Context: The Context in which this receiver is running.

onReceive

Added in API level 3
public void onReceive (Context context, 
                Intent intent)

Implements BroadcastReceiver.onReceive to dispatch calls to the various other methods on AppWidgetProvider.

Parameters
context Context: The Context in which the receiver is running.

intent Intent: The Intent being received.

onRestored

Added in API level 21
public void onRestored (Context context, 
                int[] oldWidgetIds, 
                int[] newWidgetIds)

Called in response to the 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 onUpdate(Context, AppWidgetManager, int) so your provider can immediately generate new RemoteViews suitable for its newly-restored set of instances.

In addition, you should set AppWidgetManager.OPTION_APPWIDGET_RESTORE_COMPLETED to true indicate if a widget has been restored successfully from the provider's side.

onUpdate

Added in API level 3
public void onUpdate (Context context, 
                AppWidgetManager appWidgetManager, 
                int[] appWidgetIds)

Called in response to the AppWidgetManager.ACTION_APPWIDGET_UPDATE and AppWidgetManager.ACTION_APPWIDGET_RESTORED broadcasts when this AppWidget provider is being asked to provide RemoteViews for a set of AppWidgets. Override this method to implement your own AppWidget functionality.

Parameters
context Context: The Context in which this receiver is running.

appWidgetManager AppWidgetManager: A AppWidgetManager object you can call AppWidgetManager.updateAppWidget(ComponentName, RemoteViews) on.

appWidgetIds int: 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.