Added in API level 3

AppWidgetProvider


open class AppWidgetProvider : BroadcastReceiver

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, onDeleted, onEnabled or onDisabled methods to implement your own AppWidget functionality.

Summary

Public constructors

Constructor to initialize AppWidgetProvider.

Public methods
open Unit
onAppWidgetOptionsChanged(context: Context!, appWidgetManager: AppWidgetManager!, appWidgetId: Int, newOptions: Bundle!)

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.

open Unit
onDeleted(context: Context!, appWidgetIds: IntArray!)

Called in response to the AppWidgetManager.ACTION_APPWIDGET_DELETED broadcast when one or more AppWidget instances have been deleted.

open Unit
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.

open Unit
onEnabled(context: Context!)

Called in response to the AppWidgetManager.ACTION_APPWIDGET_ENABLED broadcast when the a AppWidget for this provider is instantiated.

open Unit
onReceive(context: Context!, intent: Intent!)

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

open Unit
onRestored(context: Context!, oldWidgetIds: IntArray!, newWidgetIds: IntArray!)

Called in response to the AppWidgetManager.ACTION_APPWIDGET_RESTORED broadcast when instances of this AppWidget provider have been restored from backup.

open Unit
onUpdate(context: Context!, appWidgetManager: AppWidgetManager!, appWidgetIds: IntArray!)

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.

Inherited functions
Unit abortBroadcast()

Sets the flag indicating that this receiver should abort the current broadcast; only works with broadcasts sent through Context.sendOrderedBroadcast. This will prevent any other broadcast receivers from receiving the broadcast. It will still call onReceive of the BroadcastReceiver that the caller of Context.sendOrderedBroadcast passed in.

This method does not work with non-ordered broadcasts such as those sent with Context.sendBroadcast

Unit clearAbortBroadcast()

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

Boolean getAbortBroadcast()

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

Boolean getDebugUnregister()

Return the last value given to setDebugUnregister.

Int getResultCode()

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

String! getResultData()

Retrieve the current result data, as set by the previous receiver. Often this is null.

Bundle! getResultExtras(makeMap: Boolean)

Retrieve the current result extra data, as set by the previous receiver. Any changes you make to the returned Map will be propagated to the next 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.

BroadcastReceiver.PendingResult! goAsync()

This can be called by an application in onReceive to allow it to keep the broadcast active after returning from that function. This does not change the expectation of being relatively responsive to the broadcast, but does allow the implementation to move work related to it over to another thread to avoid glitching the main UI thread due to disk IO.

As a general rule, broadcast receivers are allowed to run for up to 10 seconds before the system will consider them non-responsive and ANR the app. Since these usually execute on the app's main thread, they are already bound by the ~5 second time limit of various operations that can happen there (not to mention just avoiding UI jank), so the receive limit is generally not of concern. However, once you use goAsync, though able to be off the main thread, the broadcast execution limit still applies, and that includes the time spent between calling this method and ultimately PendingResult.finish().

If you are taking advantage of this method to have more time to execute, it is useful to know that the available time can be longer in certain situations. In particular, if the broadcast you are receiving is not a foreground broadcast (that is, the sender has not used Intent.FLAG_RECEIVER_FOREGROUND), then more time is allowed for the receivers to run, allowing them to execute for 30 seconds or even a bit more. This is something that receivers should rarely take advantage of (long work should be punted to another system facility such as android.app.job.JobScheduler, android.app.Service, or see especially androidx.core.app.JobIntentService), but can be useful in certain rare cases where it is necessary to do some work as soon as the broadcast is delivered. Keep in mind that the work you do here will block further broadcasts until it completes, so taking advantage of this at all excessively can be counter-productive and cause later events to be received more slowly.

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.

Boolean isOrderedBroadcast()

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

IBinder! peekService(myContext: Context!, service: Intent!)

Provide a binder to an already-bound service. This method is synchronous and will not start the target service if it is not present, so it is safe to call from onReceive. For peekService() to return a non null android.os.IBinder interface the service must have published it before. In other words some component must have called android.content.Context#bindService(Intent, ServiceConnection, int) on it.

Unit setDebugUnregister(debug: Boolean)

Control inclusion of debugging help for mismatched calls to Context.registerReceiver(). If called with true, before given to registerReceiver(), then the callstack of the following Context.unregisterReceiver() call is retained, to be printed if a later incorrect unregister call is made. Note that doing this requires retaining information about the BroadcastReceiver for the lifetime of the app, resulting in a leak -- this should only be used for debugging.

Unit setOrderedHint(isOrdered: Boolean)

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

Unit setResult(code: Int, data: String!, extras: Bundle!)

Change all of the result data returned from this broadcasts; only works with broadcasts sent through Context.sendOrderedBroadcast. All current result data is replaced by the value given to this method.

This method does not work with non-ordered broadcasts such as those sent with Context.sendBroadcast

Unit setResultCode(code: Int)

Change the current result code of this broadcast; only works with broadcasts sent through Context.sendOrderedBroadcast. Often uses the Activity android.app.Activity#RESULT_CANCELED and android.app.Activity#RESULT_OK constants, though the actual meaning of this value is ultimately up to the broadcaster.

This method does not work with non-ordered broadcasts such as those sent with Context.sendBroadcast

Unit setResultData(data: String!)

Change the current result data of this broadcast; only works with broadcasts sent through Context.sendOrderedBroadcast. This is an arbitrary string whose interpretation is up to the broadcaster.

This method does not work with non-ordered broadcasts such as those sent with Context.sendBroadcast

Unit setResultExtras(extras: Bundle!)

Change the current result extras of this broadcast; only works with broadcasts sent through Context.sendOrderedBroadcast. This is a Bundle holding arbitrary data, whose interpretation is up to the broadcaster. Can be set to null. Calling this method completely replaces the current map (if any).

This method does not work with non-ordered broadcasts such as those sent with Context.sendBroadcast

Public constructors

AppWidgetProvider

Added in API level 3
AppWidgetProvider()

Constructor to initialize AppWidgetProvider.

Public methods

onAppWidgetOptionsChanged

Added in API level 16
open fun onAppWidgetOptionsChanged(
    context: Context!,
    appWidgetManager: AppWidgetManager!,
    appWidgetId: Int,
    newOptions: Bundle!
): Unit

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. {@more}

Parameters
context Context!: The Context in which this receiver is running.
appWidgetManager AppWidgetManager!: A AppWidgetManager object you can call android.appwidget.AppWidgetManager#updateAppWidget 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
open fun onDeleted(
    context: Context!,
    appWidgetIds: IntArray!
): Unit

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. {@more}

Parameters
context Context!: The Context in which this receiver is running.
appWidgetIds IntArray!: The appWidgetIds that have been deleted from their host.

onDisabled

Added in API level 3
open fun onDisabled(context: Context!): Unit

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. {@more}

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

onEnabled

Added in API level 3
open fun onEnabled(context: Context!): Unit

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. {@more} When the last AppWidget for this provider is deleted, AppWidgetManager.ACTION_APPWIDGET_DISABLED is sent by the AppWidget manager, and onDisabled 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
open fun onReceive(
    context: Context!,
    intent: Intent!
): Unit

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
open fun onRestored(
    context: Context!,
    oldWidgetIds: IntArray!,
    newWidgetIds: IntArray!
): Unit

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 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. {@more}

Parameters
context Context!:
oldWidgetIds IntArray!:
newWidgetIds IntArray!:

onUpdate

Added in API level 3
open fun onUpdate(
    context: Context!,
    appWidgetManager: AppWidgetManager!,
    appWidgetIds: IntArray!
): Unit

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. {@more}

Parameters
context Context!: The Context in which this receiver is running.
appWidgetManager AppWidgetManager!: A AppWidgetManager object you can call android.appwidget.AppWidgetManager#updateAppWidget on.
appWidgetIds IntArray!: 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.