onUpdate not being called in widget, even though I see the android.appwidget.action.APPWIDGET_UPDATE intent in onreceive
I'm working with the emulator using the debugger and I've noticed that onUpdate()
is not getting called. When I add the widget to the emulator homes开发者_开发问答creen I see my breakpoint being hit in the onReceive method. The onReceive()
method does get the android.appwidget.action.APPWIDGET_UPDATE
intent. However, the onUpdate()
method never gets called. I believe it's defined correctly.
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
// code that sets up remove view and updates appWidgetManager
}
Call the super class method in onReceive()
@Override
public void onReceive(Context context, Intent intent)
{
// Chain up to the super class so the onEnabled, etc callbacks get dispatched
super.onReceive(context, intent);
// Handle a different Intent
Log.d(TAG, "onReceive()" + intent.getAction());
}
If you override onReceive() in your subclassed AppWidgetProvider, then the provider's onEnabled, onUpdate, etc callbacks are not triggered unless you call the superclass.
onUpdate is called by the onReceive only if you set the AppWidgetManager.EXTRA_APPWIDGET_IDS extra to an array of appWidgetIds i.e
// Set the update intent for when the sync button is clicked.
Intent syncIntent = new Intent(context,Widget.class);
syncIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
syncIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); // onUpdate is only called when AppWidgetManager.EXTRA_APPWIDGET_IDS is set to a non empty array.
PendingIntent syncPendingIntent = PendingIntent.getBroadcast(context,1, syncIntent,0);
rv.setOnClickPendingIntent(R.id.updateWidgetImageButton, syncPendingIntent);
Do you have a configuration activity setup for your widget? Because if you do, then onUpdate is not called and it is the job of the configuration activity to manually update the widget for the first time. After that onUpdate should be called as defined in your updatePeriod configuration.
Do you have this in the Android Manifest?
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
精彩评论