开发者

onUpdate not being called in widget, even though I see the android.appwidget.action.APPWIDGET_UPDATE intent in onreceive

开发者 https://www.devze.com 2023-01-06 21:22 出处:网络
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

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消