App Widget Broadcast Intents

AppWidgetProvider is just a convenience class. If you would like to receive the widget broadcasts directly, you can. The four intents you need to care about are:

ACTION APPWIDGET UPDATE

ACTION

APPWIDGET

DELETED

ACTION

APPWIDGET

ENABLED

ACTION

APPWIDGET

DISABLED

By way of example, the implementation of onReceive(Context, Intent) is quite simple:

public void onReceive(Context context, Intent intent) {

// Protect against rogue update broadcasts (not really a security issue, // just filter bad broacasts out so subclasses are less likely to crash). String action = intent.getAction();

if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) { Bundle extras = intent.getExtras();

int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);

if (appWidgetIds != null && appWidgetIds.length > 0) {

this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);

else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) { Bundle extras = intent.getExtras(); if (extras != null) {

int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);

if (appWidgetIds != null && appWidgetIds.length > 0) { this.onDeleted(context, appWidgetIds);

else if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) { this.onEnabled(context);

else if (AppWidgetManager.ACTION_APPWIDGET_DISABLED.equals(action)) { this.onDisabled(context);

0 0

Post a comment