Archive for May, 2011

Android AppWidget 101

May 23, 2011

0. To create an appWidget, you would need 4 things: in your application manifest xml, declare the appWidget Provider (in a <receiver> tag, see code below);  a layout xml for your remote views (in “res/layout”); an app widget provider xml (in “res/xml” with an <appwidget-provider> tag); and optionally implementing AppWidgetProvider for the logic in your app.

<receiver android:name=".TestAppWidgetProvider" android:enabled="true">
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<meta-data android:resource="@xml/widget" android:name="android.appwidget.provider"></meta-data>

1. You can explicitly update the appWidgets using AppWidgetManager and not rely on the timer mechanism. This means your app can register with all kinds of listeners and update your appWidgets based on the corresponding events when they occur:

final AppWidgetManager gm = AppWidgetManager.getInstance(context);
gm.updateAppWidget( new ComponentName("",
""), views);

2. Each time a new appWidget is added to the home screen,

AppWidgetProvider.onReceive > onUpdate

also get called; each time an appWidget is deleted from the home screen,

onReceive > onDelete

get called.

When the first appWidget is added to home,

onReceive > onEnabled > onReceive > onUpdate

anddroid.appWidget.action.APPWIDGET_ENABLED is the onReceived intent action;

When the last appWidget is removed from home,

 onReceive - onDeleted > onReceive - onDisabled

anddroid.appWidget.action.APPWIDGET_DELETED for onDeleted and anddroid.appWidget.action.APPWIDGET_DISABLED for onDeleted get called.

You don’t seem to explicitly declare in your manifest xml for your appWidgetProvider to receive APPWIDGET_DELETED/APPWIDGET_DISABLED actions but you need to declare an intent filter for ACTION_APPWIDGET_UPDATE action.