AppWidgetManager.getAppWidgetIds in activity returns an empty list

前端 未结 3 539
感动是毒
感动是毒 2021-02-06 08:34

I have an appwidget that I\'m trying to update from an activity.

To do that, I need the appwidget id.

I\'ve used AppWidgetManager.getAppWidgetIds bu

3条回答
  •  情深已故
    2021-02-06 09:29

    You can access your widget without knowing id

    appWidgetManager.updateAppWidget(new ComponentName(this.getPackageName(),Widget.class.getName()), views);
    

    This let you access the widget without having to know the 'appWidgetID'. Then in activity:

    Intent intent = new Intent(this, Settings.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    
    views.setOnClickPendingIntent(R.id.btnActivate, pendingIntent);
    
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
    appWidgetManager.updateAppWidget(new ComponentName(this.getPackageName(), Widget.class.getName()), views);
    finish();
    

提交回复
热议问题