简体   繁体   中英

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 homescreen 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.

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>

onUpdate is called by the onReceive only if you set the AppWidgetManager.EXTRA_APPWIDGET_IDS extra to an array of appWidgetIds ie

    // 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM