AppWidgetProvider
class, which in turn extends from BroadcastReceiver
. AppWidgetProvider
objects receive only broadcasts relevant to the app widget, including intents for update, enabled, disabled, and deleted. As with all broadcast receivers, app widget providers are declared in the Android manifest. AppWidgetManager
) manages widget updates and sends the broadcast intents that app widget providers receive to do the actual work of updating. AppWidgetHost
) is an app component that can hold and display other app widgets. The Android home screen is by far the most frequently used app widget host, although it is possible to create your own app widget host. ACTION_APPWIDGET_UPDATE
. The app widget-provider class receives that intent and calls the onUpdate()
method. onUpdate()
method in your widget-provider class to update your widget. For simple widgets, you might just update the data in the widget's views. More complex widgets may need to interact with additional services to retrieve data from content providers or other data storage locations. AlarmManager
. Set the alarm type to either ELAPSED_REALTIME
or RTC
, which will only deliver the alarm when the device is awake. NewAppWidget
: NewAppWidget.java
is added to your project.res/layouts/new_app_widget.xml
.res/xml/new_app_widget_info.xml
.NewAppWidgetConfigurationActivity.java
is added to your project. res/xml/
folder, and it contains a single <appwidget-provider>
element. Android Studio creates a template provider-info file when you add a new widget, based on the values you provided in the New Android Component dialog. AppWidgetProviderInfo
class for more information on the attributes for the <appwidget-provider>
element. android:minHeight
and android:minWidth
attributes define the minimum initial size of the widget, in dp. Android Studio provides these values in the provider-info file based on the number of grid spaces you specify when you create the widget. minWidth
and minHeight
values.minWidth
and minHeight
should be:# of columns or rows | minWidth or minHeight |
1 | 40 dp |
2 | 110 dp |
3 | 180 dp |
4 | 250 dp |
android:initialLayout
attribute defines the XML layout file for your widget, for example @layout/my_app_widget
. You will learn more about widget layout in the next section. android:updatePeriodMillis
attribute defines update interval in milliseconds for the app widget, that is, how often the app widget manager should send a broadcast intent to update the widget. The default update interval is 86,400,000 milliseconds, or 24 hours. The minimum possible update interval is 1,800,000 milliseconds or 30 minutes. Because widget updates use device resources and may even awaken a sleeping device, design your widget to update as infrequently as possible (use a larger value for android:updatePeriodMillis
). android:updatePeriodMillis
to 0. android:updatePeriodMillis
value determines how often the app widget manager requests an update for an app widget. A running app can update its associated widget at any time. android:previewImage
attribute specifies an image or drawable that previews what the app widget will look like when it has been configured and placed on the user's home screen. The user sees the preview image in the widget picker when they choose an app widget. res/drawable
folder. When you create a new widget, Android Studio provides an 'example' widget preview image for you (@drawable/new_appwidget_preview
) android:previewImage
line in the provider-info file, the launcher icon for your app is used instead. android:resizeMode
attribute specifies whether your widget can be resized horizontally, vertically, both, or neither. The possible values are 'horizontal'
, 'vertical'
, 'horizontal|vertical'
, and 'none'
.android:widgetCategory
attribute declares whether your app widget can be displayed on the home screen ('home_screen'
), the lock screen ('keyguard'
), or both. Android Studio provides this attribute based on the value you choose for the Placement drop-down menu when you create the widget. For Android 5.0 and higher, only home_screen
is valid. Keyguard (lock screen) widgets were only available in Android 4.2 (API 17) to Android 4.4.4 (API 19.0). android:configure
attribute defines the app widget's configuration activity, if you selected the Configuration Screen checkbox in the Add Android Component dialog. https://software-killer.mystrikingly.com/blog/slots-vacation-free-coins. The configuration activity is specified in this attribute with its fully qualified class name. You will learn more about configuration activities later in this chapter. RemoteViews
object hierarchies, not View
object hierarchies. Remote views do not support every kind of layout or view that standard views support, and remote views do not support custom views. RemoteViews
object from the layout, update the views in the layout with data, and then pass that remote view to the app widget manager to display. RemoteViews
object (and, consequently, an app widget) can support the following layout classes:RemoteViews
class also supports ViewStub
, which is an invisible, zero-sized View.
You can use ViewStub
to lazily inflate layout resources at runtime.android:padding
attribute:widget_margin
value is defined in the default dimens.xml
file (res/values/dimens.xml
):dimens.xml (v14)
(res/values-v14/dimens.xml
) file, which removes the extra space for API 14 and higher versions:AppWidgetProvider
class. The AppWidgetProvider
class extends BroadcastReceiver
as a convenience class to receive and handle broadcasts specific to app widgets, such as when the app widget is updated, deleted, enabled, and disabled.AppWidgetProvider
for your own class.onUpdate()
method, at minimum, to construct the widget layout and manage widget updates. AndroidManifest.xml
).onUpdate()
, the AppWidgetProvider
class includes other handler methods for different events including onDeleted()
, onEnabled()
, and onDisabled()
. See AppWidgetProvider
for more details on these methods. <receiver>
element to your manifest when you add a new widget:name
attribute in <receiver>
contains the class name for the widget provider. <intent-filter>
element declares an action of APPWIDGET_UPDATE
. This element indicates that the receiver can receive broadcast intents from the app widget manager to update the widget. <meta-data>
element specifies the location of the widget provider-info file. onUpdate()
method in your provider class. The onUpdate()
method is called when the user adds the app widget, and every time the widget receives an update broadcast intent from the app widget manager or from your associated app. Perform all essential widget setup in the onUpdate()
method, such as creating the widget layout, defining event handlers for views, or starting services. onUpdate()
method is not called when the user adds the app widget, but it is called for all subsequent updates. Your configuration activity must request the first update when configuration is complete. See Using a configuration activity for details. onUpdate()
method:onUpdate()
method is a loop that iterates over an array of widget IDs. All instances of your widget that are currently active are identified by an internal ID. When onUpdate()
is called, it is passed and array of IDs for the widgets that need updating. Your widget may have multiple instances active for several reasons, including most typically one widget with different configurations. For example, multiple weather widgets may be active to display the current weather in different locations. Make sure your onUpdate()
method loops through all the widgets by ID and updates each widget individually. RemoteViews
object and updating any data that the remote view contains. The last line in the loop tells the app widget manager to update the widget with the current RemoteViews
object. setOnClickPendingIntent
()
method (defined in the RemoteViews
class) enables you to connect a PendingIntent
object to one or more views in your widget. The Android system delivers those intents to your app (or to any other app). onUpdate()
method. For example, this code launches the widget's associated app.ACTION_APPWIDGET_CONFIGURE
action. For example:android:configure
attribute. (See Updating the provider info, above.) For example:onCreate()
method, get the widget ID from the intent that launched the activity. You'll need it to update the widget when configuration is complete. The app widget ID is in the incoming intent extras
, with the key AppWidgetManager.EXTRA_APPWIDGET_ID
.onCreate()
, set the default activity result to RESULT_CANCELED
. This covers the case where the user backs out of widget configuration before it is complete. A canceled result alerts the widget host (the Android home screen) that the widget should not be added.extras
with the key AppWidgetManager.EXTRA_APPWIDGET_ID
. Set the result to RESULT_OK
and call the finish()
method: