简体   繁体   English

即使我在 onreceive 中看到了 android.appwidget.action.APPWIDGET_UPDATE 意图,也没有在小部件中调用 onUpdate

[英]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.我正在使用调试器与模拟器一起工作,我注意到onUpdate()没有被调用。 When I add the widget to the emulator homescreen I see my breakpoint being hit in the onReceive method.当我将小部件添加到模拟器主屏幕时,我看到我的断点在 onReceive 方法中被击中。 The onReceive() method does get the android.appwidget.action.APPWIDGET_UPDATE intent. onReceive()方法确实获取了android.appwidget.action.APPWIDGET_UPDATE意图。 However, the onUpdate() method never gets called.但是,永远不会调用onUpdate()方法。 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()在 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.如果您在子类 AppWidgetProvider 中覆盖 onReceive(),则除非您调用超类,否则不会触发提供程序的 onEnabled、onUpdate 等回调。

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.因为如果这样做,则不会调用 onUpdate 并且首次手动更新小部件是配置活动的工作。 After that onUpdate should be called as defined in your updatePeriod configuration.之后,应按照您的 updatePeriod 配置中的定义调​​用 onUpdate。

Do you have this in the Android Manifest?你在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仅当您将 AppWidgetManager.EXTRA_APPWIDGET_IDS 额外设置为 appWidgetIds 数组时, onReceive 才会调用 onUpdate

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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