1page.title=App Widgets 2@jd:body 3 4<div id="qv-wrapper"> 5 <div id="qv"> 6 <h2>Quickview</h2> 7 <ul> 8 <li>App Widgets provide users access to some of your application features 9directly from the Home screen (without the need to launch an activity)</li> 10 <li>App Widgets are backed by a special kind of broadcast receiver that handles the App 11Widget lifecycle</li> 12 </ul> 13 14 <h2>In this document</h2> 15 <ol> 16 <li><a href="#Basics">The Basics</a></li> 17 <li><a href="#Manifest">Declaring an App Widget in the Manifest</a></li> 18 <li><a href="#MetaData">Adding the AppWidgetProviderInfo Metadata</a></li> 19 <li><a href="#CreatingLayout">Creating the App Widget Layout</a></li> 20 <li><a href="#AppWidgetProvider">Using the AppWidgetProvider Class</a> 21 <ol> 22 <li><a href="#ProviderBroadcasts">Receiving App Widget broadcast Intents</a></li> 23 </ol> 24 </li> 25 <li><a href="#Configuring">Creating an App Widget Configuration Activity</a> 26 <ol> 27 <li><a href="#UpdatingFromTheConfiguration">Updating the App Widget from 28 the configuration Activity</a></li> 29 </ol> 30 </li> 31 </ol> 32 33 <h2>Key classes</h2> 34 <ol> 35 <li>{@link android.appwidget.AppWidgetProvider}</li> 36 <li>{@link android.appwidget.AppWidgetProviderInfo}</li> 37 <li>{@link android.appwidget.AppWidgetManager}</li> 38 </ol> 39 40 <h2>See also</h2> 41 <ol> 42 <li><a href="{@docRoot}guide/practices/ui_guidelines/widget_design.html">App Widget Design 43 Guidelines</a></li> 44 <li><a href="http://android-developers.blogspot.com/2009/04/introducing-home-screen-widgets-and.html">Introducing 45 home screen widgets and the AppWidget framework »</a></li> 46 </ol> 47 </div> 48</div> 49 50 51<p>App Widgets are miniature application views that can be embedded in other applications 52(such as the Home screen) and receive periodic updates. These views are referred 53to as Widgets in the user interface, 54and you can publish one with an App Widget provider. An application component that is 55able to hold other App Widgets is called an App Widget host. The screenshot below shows 56the Music App Widget.</p> 57 58<img src="{@docRoot}images/appwidget.png" alt="" /> 59 60<p>This document describes how to publish an App Widget using an App Widget provider.</p> 61 62 63<h2 id="Basics">The Basics</h2> 64 65<p>To create an App Widget, you need the following:</p> 66 67<dl> 68 <dt>{@link android.appwidget.AppWidgetProviderInfo} object</dt> 69 <dd>Describes the metadata for an App Widget, such as the App Widget's layout, update frequency, 70 and the AppWidgetProvider class. This should be defined in XML.</dd> 71 <dt>{@link android.appwidget.AppWidgetProvider} class implementation</dt> 72 <dd>Defines the basic methods that allow you to programmatically interface with the App Widget, 73 based on broadcast events. Through it, you will receive broadcasts when the App Widget is updated, 74 enabled, disabled and deleted.</dd> 75 <dt>View layout</dt> 76 <dd>Defines the initial layout for the App Widget, defined in XML.</dd> 77</dl> 78 79<p>Additionally, you can implement an App Widget configuration Activity. This is an optional 80{@link android.app.Activity} that launches when the user adds your App Widget and allows him or her 81to modify App Widget settings at create-time.</p> 82 83<p>The following sections describe how to setup each of these components.</p> 84 85 86<h2 id="Manifest">Declaring an App Widget in the Manifest</h2> 87 88<p>First, declare the {@link android.appwidget.AppWidgetProvider} class in your application's 89<code>AndroidManifest.xml</code> file. For example:</p> 90 91<pre> 92<receiver android:name="ExampleAppWidgetProvider" > 93 <intent-filter> 94 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 95 </intent-filter> 96 <meta-data android:name="android.appwidget.provider" 97 android:resource="@xml/example_appwidget_info" /> 98</receiver> 99</pre> 100 101<p>The <code><receiver></code> element requires the <code>android:name</code> 102attribute, which specifies the {@link android.appwidget.AppWidgetProvider} used 103by the App Widget.</p> 104 105<p>The <code><intent-filter></code> element must include an <code><action></code> 106element with the <code>android:name</code> attribute. This attribute specifies 107that the {@link android.appwidget.AppWidgetProvider} accepts the {@link 108android.appwidget.AppWidgetManager#ACTION_APPWIDGET_UPDATE ACTION_APPWIDGET_UPDATE} broadcast. 109This is the only broadcast that you must explicitly declare. The {@link android.appwidget.AppWidgetManager} 110automatically sends all other App Widget broadcasts to the AppWidgetProvider as necessary.</p> 111 112<p>The <code><meta-data></code> element specifies the 113{@link android.appwidget.AppWidgetProviderInfo} resource and requires the 114following attributes:</p> 115<ul> 116 <li><code>android:name</code> - Specifies the metadata name. Use <code>android.appwidget.provider</code> 117 to identify the data as the {@link android.appwidget.AppWidgetProviderInfo} descriptor.</li> 118 <li><code>android:resource</code> - Specifies the {@link android.appwidget.AppWidgetProviderInfo} 119 resource location.</li> 120</ul> 121 122 123<h2 id="MetaData">Adding the AppWidgetProviderInfo Metadata</h2> 124 125<p>The {@link android.appwidget.AppWidgetProviderInfo} defines the essential 126qualities of an App Widget, such as its minimum layout dimensions, its initial layout resource, 127how often to update the App Widget, and (optionally) a configuration Activity to launch at create-time. 128Define the AppWidgetProviderInfo object in an XML resource using a single 129<code><appwidget-provider></code> element and save it in the project's <code>res/xml/</code> 130folder.</p> 131 132<p>For example:</p> 133 134<pre> 135<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 136 android:minWidth="294dp" 137 android:minHeight="72dp" 138 android:updatePeriodMillis="86400000" 139 android:initialLayout="@layout/example_appwidget" 140 android:configure="com.example.android.ExampleAppWidgetConfigure" > 141</appwidget-provider> 142</pre> 143 144<p>Here's a summary of the <code><appwidget-provider></code> attributes:</p> 145<ul> 146 <li>The values for the <code>minWidth</code> and <code>minHeight</code> attributes specify the minimum 147 area required by the App Widget's layout. 148 <p>The default Home screen positions App Widgets in its window based on a grid of 149 cells that have a defined height and width. If the values for an App Widget's minimum width 150 or height don't match the dimensions of the cells, 151 then the App Widget dimensions round <em>up</em> to the nearest cell size. 152 (See the <a href="{@docRoot}guide/practices/ui_guidelines/widget_design.html">App Widget Design 153 Guidelines</a> for more information on the Home screen cell sizes.)</p> 154 <p>Because the Home screen's layout orientation (and thus, the cell sizes) can change, 155 as a rule of thumb, you should assume the worst-case cell size of 74 pixels for the height 156 <em>and</em> width of a cell. However, you must subtract 2 from the final dimension to account 157 for any integer rounding errors that occur in the pixel count. To find your minimum width 158 and height in density-independent pixels (dp), use this formula:<br/> 159 <code>(number of cells * 74) - 2</code><br/> 160 Following this formula, you should use 72 dp for a height of one cell, 294 dp and for a width of four cells.</p> 161 </li> 162 <li>The <code>updatePeriodMillis</code> attribute defines how often the App Widget framework should 163 request an update from the {@link android.appwidget.AppWidgetProvider} by calling the 164 {@link android.appwidget.AppWidgetProvider#onUpdate(Context,AppWidgetManager,int[]) 165 onUpdate()} method. The actual update is not guaranteed to occur exactly on time with this value 166 and we suggest updating as infrequently as possible—perhaps no more than once an hour to 167 conserve the battery. You might also allow the user to adjust the frequency in a 168 configuration—some people might want a stock ticker to update every 15 minutes, or maybe 169 only four times a day. 170 <p class="note"><strong>Note:</strong> If the device is asleep when it is time for an update 171 (as defined by <code>updatePeriodMillis</code>), then the device will wake up in order 172 to perform the update. If you don't update more than once per hour, this probably won't 173 cause significant problems for the battery life. If, however, you need to update more 174 frequently and/or you do not need to update while the device is asleep, then you can instead 175 perform updates based on an alarm that will not wake the device. To do so, set an alarm with 176 an Intent that your AppWidgetProvider receives, using the {@link android.app.AlarmManager}. 177 Set the alarm type to either {@link android.app.AlarmManager#ELAPSED_REALTIME} or 178 {@link android.app.AlarmManager#RTC}, which will only 179 deliver the alarm when the device is awake. Then set <code>updatePeriodMillis</code> to 180 zero (<code>"0"</code>).</p> 181 </li> 182 <li>The <code>initialLayout</code> attribute points to the layout resource that defines the 183 App Widget layout.</li> 184 <li>The <code>configure</code> attribute defines the {@link android.app.Activity} to launch when 185 the user adds the App Widget, in order for him or her to configure App Widget properties. This is optional 186 (read <a href="#Configuring">Creating an App Widget Configuration Activity</a> below).</li> 187</ul> 188 189<p>See the {@link android.appwidget.AppWidgetProviderInfo} class for more information on the 190attributes accepted by the <code><appwidget-provider></code> element.</p> 191 192 193<h2 id="CreatingLayout">Creating the App Widget Layout</h2> 194 195<p>You must define an initial layout for your App Widget in XML and save it in the project's 196<code>res/layout/</code> directory. You can design your App Widget using the View objects listed 197below, but before you begin designing your App Widget, please read and understand the 198<a href="{@docRoot}guide/practices/ui_guidelines/widget_design.html">App Widget Design 199Guidelines</a>.</p> 200 201<p>Creating the App Widget layout is simple if you're 202familiar with <a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout in XML</a>. 203However, you must be aware that App Widget layouts are based on {@link android.widget.RemoteViews}, 204which do not support every kind of layout or view widget.</p> 205 206<p>A RemoteViews object (and, consequently, an App Widget) can support the 207following layout classes:</p> 208 209<ul class="nolist"> 210 <li>{@link android.widget.FrameLayout}</li> 211 <li>{@link android.widget.LinearLayout}</li> 212 <li>{@link android.widget.RelativeLayout}</li> 213</ul> 214 215<p>And the following widget classes:</p> 216<ul class="nolist"> 217 <li>{@link android.widget.AnalogClock}</li> 218 <li>{@link android.widget.Button}</li> 219 <li>{@link android.widget.Chronometer}</li> 220 <li>{@link android.widget.ImageButton}</li> 221 <li>{@link android.widget.ImageView}</li> 222 <li>{@link android.widget.ProgressBar}</li> 223 <li>{@link android.widget.TextView}</li> 224</ul> 225 226<p>Descendants of these classes are not supported.</p> 227 228 229<h2 id="AppWidgetProvider">Using the AppWidgetProvider Class</h2> 230 231<div class="sidebox-wrapper"> 232<div class="sidebox"> 233 <p>You must declare your AppWidgetProvider class implementation as a broadcast receiver 234 using the <code><receiver></code> element in the AndroidManifest (see 235 <a href="#Manifest">Declaring an App Widget in the Manifest</a> above).</p> 236 </div> 237</div> 238 239<p>The {@link android.appwidget.AppWidgetProvider} class extends BroadcastReceiver as a convenience 240class to handle the App Widget broadcasts. The AppWidgetProvider receives only the event broadcasts that 241are relevant to the App Widget, such as when the App Widget is updated, deleted, enabled, and disabled. 242When these broadcast events occur, the AppWidgetProvider receives the following method calls:</p> 243 244<dl> 245 <dt>{@link android.appwidget.AppWidgetProvider#onUpdate(Context,AppWidgetManager,int[])}</dt> 246 <dd>This is called to update the App Widget at intervals defined by the <code>updatePeriodMillis</code> 247 attribute in the AppWidgetProviderInfo (see <a href="#MetaData">Adding the 248 AppWidgetProviderInfo Metadata</a> above). This method is also called 249 when the user adds the App Widget, so it should perform the essential setup, 250 such as define event handlers for Views and start a temporary 251 {@link android.app.Service}, if necessary. However, if you have declared a configuration 252 Activity, <strong>this method is not called</strong> when the user adds the App Widget, 253 but is called for the subsequent updates. It is the responsibility of the 254 configuration Activity to perform the first update when configuration is done. 255 (See <a href="#Configuring">Creating an App Widget Configuration Activity</a> below.)</dd> 256 <dt>{@link android.appwidget.AppWidgetProvider#onDeleted(Context,int[])}</dt> 257 <dd>This is called every time an App Widget is deleted from the App Widget host.</dd> 258 <dt>{@link android.appwidget.AppWidgetProvider#onEnabled(Context)}</dt> 259 <dd>This is called when an instance the App Widget is created for the first time. For example, if the user 260 adds two instances of your App Widget, this is only called the first time. 261 If you need to open a new database or perform other setup that only needs to occur once 262 for all App Widget instances, then this is a good place to do it.</dd> 263 <dt>{@link android.appwidget.AppWidgetProvider#onDisabled(Context)}</dt> 264 <dd>This is called when the last instance of your App Widget is deleted from the App Widget host. 265 This is where you should clean up any work done in 266 {@link android.appwidget.AppWidgetProvider#onEnabled(Context)}, 267 such as delete a temporary database.</dd> 268 <dt>{@link android.appwidget.AppWidgetProvider#onReceive(Context,Intent)}</dt> 269 <dd>This is called for every broadcast and before each of the above callback methods. 270 You normally don't need to implement this method because the default AppWidgetProvider 271 implementation filters all App Widget broadcasts and calls the above 272 methods as appropriate.</dd> 273</dl> 274 275<p class="warning"><strong>Note:</strong> In Android 1.5, there is a known issue in which the 276<code>onDeleted()</code> method will not be called when it should be. To work around this issue, 277you can implement {@link android.appwidget.AppWidgetProvider#onReceive(Context,Intent) 278onReceive()} as described in this 279<a href="http://groups.google.com/group/android-developers/msg/e405ca19df2170e2">Group post</a> 280to receive the <code>onDeleted()</code> callback. 281</p> 282 283<p>The most important AppWidgetProvider callback is 284{@link android.appwidget.AppWidgetProvider#onUpdate(Context,AppWidgetManager,int[]) 285onUpdated()} because it is called when each App Widget is added to a host (unless you use 286a configuration Activity). If your App Widget accepts any 287user interaction events, then you need to register the event handlers in this callback. 288If your App Widget doesn't create temporary 289files or databases, or perform other work that requires clean-up, then 290{@link android.appwidget.AppWidgetProvider#onUpdate(Context,AppWidgetManager,int[]) 291onUpdated()} may be the only callback method you need to define. For example, if you want an App Widget 292with a button that launches an Activity when clicked, you could use the following 293implementation of AppWidgetProvider:</p> 294 295<pre> 296public class ExampleAppWidgetProvider extends AppWidgetProvider { 297 298 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 299 final int N = appWidgetIds.length; 300 301 // Perform this loop procedure for each App Widget that belongs to this provider 302 for (int i=0; i<N; i++) { 303 int appWidgetId = appWidgetIds[i]; 304 305 // Create an Intent to launch ExampleActivity 306 Intent intent = new Intent(context, ExampleActivity.class); 307 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 308 309 // Get the layout for the App Widget and attach an on-click listener to the button 310 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout); 311 views.setOnClickPendingIntent(R.id.button, pendingIntent); 312 313 // Tell the AppWidgetManager to perform an update on the current App Widget 314 appWidgetManager.updateAppWidget(appWidgetId, views); 315 } 316 } 317} 318</pre> 319 320<p>This AppWidgetProvider defines only the 321{@link android.appwidget.AppWidgetProvider#onUpdate(Context,AppWidgetManager,int[]) 322onUpdated()} method for the purpose 323of defining a {@link android.app.PendingIntent} that launches an {@link android.app.Activity} 324and attaching it to the App Widget's button 325with {@link android.widget.RemoteViews#setOnClickPendingIntent(int,PendingIntent)}. 326Notice that it includes a loop that iterates through each entry in <code>appWidgetIds</code>, which 327is an array of IDs that identify each App Widget created by this provider. 328In this way, if the user creates more than one instance of the App Widget, then they are 329all updated simultaneously. However, only one <code>updatePeriodMillis</code> schedule will be 330managed for all instances of the App Widget. For example, if the update schedule is defined 331to be every two hours, and a second instance 332of the App Widget is added one hour after the first one, then they will both be updated 333on the period defined by the first one and the second update period will be ignored 334(they'll both be updated every two hours, not every hour).</p> 335 336<p class="note"><strong>Note:</strong> Because the AppWidgetProvider is a BroadcastReceiver, 337your process is not guaranteed to keep running after the callback methods return (see 338<a href="{@docRoot}guide/topics/fundamentals.html#broadlife">Application Fundamentals > 339Broadcast Receiver Lifecycle</a> for more information). If your App Widget setup process can take several 340seconds (perhaps while performing web requests) and you require that your process continues, 341consider starting a {@link android.app.Service} 342in the {@link android.appwidget.AppWidgetProvider#onUpdate(Context,AppWidgetManager,int[]) 343onUpdated()} method. From within the Service, you can perform your own updates to the App Widget 344without worrying about the AppWidgetProvider closing down due to an 345<a href="{@docRoot}guide/practices/design/responsiveness.html">Application Not Responding</a> 346(ANR) error. See the 347<a href="http://code.google.com/p/wiktionary-android/source/browse/trunk/Wiktionary/src/com/example/android/wiktionary/WordWidget.java">Wiktionary 348sample's AppWidgetProvider</a> for an example of an App Widget running a {@link android.app.Service}.</p> 349 350<p>Also see the <a 351href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/appwidget/ExampleAppWidgetProvider.html"> 352ExampleAppWidgetProvider.java</a> sample class.</p> 353 354 355<h3 id="ProviderBroadcasts">Receiving App Widget broadcast Intents</h3> 356 357<p>{@link android.appwidget.AppWidgetProvider} is just a convenience class. If you would like 358to receive the App Widget broadcasts directly, you can implement your own 359{@link android.content.BroadcastReceiver} or override the 360{@link android.appwidget.AppWidgetProvider#onReceive(Context,Intent)} callback. 361The four Intents you need to care about are:</p> 362<ul> 363 <li>{@link android.appwidget.AppWidgetManager#ACTION_APPWIDGET_UPDATE}</li> 364 <li>{@link android.appwidget.AppWidgetManager#ACTION_APPWIDGET_DELETED}</li> 365 <li>{@link android.appwidget.AppWidgetManager#ACTION_APPWIDGET_ENABLED}</li> 366 <li>{@link android.appwidget.AppWidgetManager#ACTION_APPWIDGET_DISABLED}</li> 367</ul> 368 369 370 371<h2 id="Configuring">Creating an App Widget Configuration Activity</h2> 372 373<p>If you would like the user to configure settings when he or she adds a new App Widget, 374you can create an App Widget configuration Activity. This {@link android.app.Activity} 375will be automatically launched by the App Widget host and allows the user to configure 376available settings for the App Widget at create-time, such as the App Widget color, size, 377update period or other functionality settings.</p> 378 379<p>The configuration Activity should be declared as a normal Activity in the Android manifest file. 380However, it will be launched by the App Widget host with the {@link 381android.appwidget.AppWidgetManager#ACTION_APPWIDGET_CONFIGURE ACTION_APPWIDGET_CONFIGURE} action, 382so the Activity needs to accept this Intent. For example:</p> 383 384<pre> 385<activity android:name=".ExampleAppWidgetConfigure"> 386 <intent-filter> 387 <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" /> 388 </intent-filter> 389</activity> 390</pre> 391 392<p>Also, the Activity must be declared in the AppWidgetProviderInfo XML file, with the 393<code>android:configure</code> attribute (see <a href="#MetaData">Adding 394the AppWidgetProviderInfo Metadata</a> above). For example, the configuration Activity 395can be declared like this:</p> 396 397<pre> 398<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 399 ... 400 android:configure="com.example.android.ExampleAppWidgetConfigure" 401 ... > 402</appwidget-provider> 403</pre> 404 405<p>Notice that the Activity is declared with a fully-qualified namespace, because 406it will be referenced from outside your package scope.</p> 407 408<p>That's all you need to get started with a configuration Activity. Now all you need is the actual 409Activity. There are, however, two important things to remember when you implement the Activity:</p> 410<ul> 411 <li>The App Widget host calls the configuration Activity and the configuration Activity should always 412 return a result. The result should include the App Widget ID 413 passed by the Intent that launched the Activity (saved in the Intent extras as 414 {@link android.appwidget.AppWidgetManager#EXTRA_APPWIDGET_ID}).</li> 415 <li>The {@link android.appwidget.AppWidgetProvider#onUpdate(Context,AppWidgetManager,int[]) 416 onUpdate()} method <strong>will not be called</strong> when the App Widget is created 417 (the system will not send the ACTION_APPWIDGET_UPDATE broadcast when a configuration Activity 418 is launched). It is the responsibility of the configuration Activity to request an update from the 419 AppWidgetManager when the App Widget is first created. However, 420 {@link android.appwidget.AppWidgetProvider#onUpdate(Context,AppWidgetManager,int[]) 421 onUpdate()} will be called for subsequent updates—it is only skipped the first time.</li> 422</ul> 423 424<p>See the code snippets in the following section for an example of how to return a result 425from the configuration and update the App Widget.</p> 426 427 428<h3 id="UpdatingFromTheConfiguration">Updating the App Widget from the configuration Activity</h3> 429 430<p>When an App Widget uses a configuration Activity, it is the responsibility of the Activity 431to update the App Widget when configuration is complete. 432You can do so by requesting an update directly from the 433{@link android.appwidget.AppWidgetManager}.</p> 434 435<p>Here's a summary of the procedure to properly update the App Widget and close 436the configuration Activity:</p> 437 438<ol> 439 <li>First, get the App Widget ID from the Intent that launched the Activity: 440<pre> 441Intent intent = getIntent(); 442Bundle extras = intent.getExtras(); 443if (extras != null) { 444 mAppWidgetId = extras.getInt( 445 AppWidgetManager.EXTRA_APPWIDGET_ID, 446 AppWidgetManager.INVALID_APPWIDGET_ID); 447} 448</pre> 449 </li> 450 <li>Perform your App Widget configuration.</li> 451 <li>When the configuration is complete, get an instance of the AppWidgetManager by calling 452 {@link android.appwidget.AppWidgetManager#getInstance(Context)}: 453<pre> 454AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 455</pre> 456 </li> 457 <li>Update the App Widget with a {@link android.widget.RemoteViews} layout by calling 458 {@link android.appwidget.AppWidgetManager#updateAppWidget(int,RemoteViews)}: 459<pre> 460RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_appwidget); 461appWidgetManager.updateAppWidget(mAppWidgetId, views); 462</pre> 463 </li> 464 <li>Finally, create the return Intent, set it with the Activity result, and finish the Activity:</li> 465<pre> 466Intent resultValue = new Intent(); 467resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); 468setResult(RESULT_OK, resultValue); 469finish(); 470</pre> 471 </li> 472</ol> 473 474<p class="note"><strong>Tip:</strong> When your configuration Activity first opens, set 475the Activity result to RESULT_CANCELED. This way, if the user backs-out of the Activity before 476reaching the end, the App Widget host is notified that the configuration was cancelled and the 477App Widget will not be added.</p> 478 479<p>See the <a 480href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/appwidget/ExampleAppWidgetConfigure.html"> 481ExampleAppWidgetConfigure.java</a> sample class in ApiDemos for an example.</p> 482 483 484 485