Programmatically update widget from activity/service/receiver

AndroidAndroid ActivityAndroid Widget

Android Problem Overview


I know it's possible, but I can't figure out a way to trigger an update of my widget from the main activity. Isn't there some general intent I can broadcast?

Android Solutions


Solution 1 - Android

If you are using an AppWidgetProvider, you can update it this way:

Intent intent = new Intent(this, MyAppWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
// Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
// since it seems the onUpdate() is only fired on that:
 int[] ids = AppWidgetManager.getInstance(getApplication())
    .getAppWidgetI‌​ds(new ComponentName(getApplication(), MyAppWidgetProvider.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);

Solution 2 - Android

This helped when I searched on how to update a widget from a service/or action (but may be possible from every Context):

Context context = this;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_2x1);
ComponentName thisWidget = new ComponentName(context, MyWidget.class);
remoteViews.setTextViewText(R.id.my_text_view, "myText" + System.currentTimeMillis());
appWidgetManager.updateAppWidget(thisWidget, remoteViews);

Solution 3 - Android

So to update an widget from an activity, you can do it like this:

Intent intent = new Intent(this, DppWidget.class);
intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
int ids[] = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), DppWidget.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
sendBroadcast(intent);

Works for me :)

Solution 4 - Android

Try this:

int[] ids = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), MyWidget.class));
        MyWidget myWidget = new MyWidget();
        myWidget.onUpdate(this, AppWidgetManager.getInstance(this),ids);

Solution 5 - Android

In case you are working with a widget that uses a collection such as ListView, GridView, or StackView, to update the widget's items, do as follow:

Context context = getApplicationContext();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisWidget = new ComponentName(context, StackWidgetProvider.class);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.stack_view);

With this method notifyAppWidgetViewDataChanged(), you can force the widget items to fetch any new data in real time.

Solution 6 - Android

int widgetIDs[] = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), WidgetProvider.class));

for (int id : widgetIDs)
    AppWidgetManager.getInstance(getApplication()).notifyAppWidgetViewDataChanged(id, R.id.widget_view);
}

Solution 7 - Android

Phaethon's accepted solution in Kotlin:

val intent = Intent(this, MyAppWidgetProvider::class.java)
intent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
val ids = AppWidgetManager.getInstance(application).getAppWidgetIds(ComponentName(getApplicationContext(),MyAppWidgetProvider::class.java!!))
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
sendBroadcast(intent)

Where MyAppWidgetProvider is derived from AppWidgetProvider:

MyAppWidgetProvider : AppWidgetProvider() {

Solution 8 - Android

If you are trying to programmatically update the widget where you do not have access to YourWidgetProvider.class explicitly, e.g. from another module or library, you can capture what YourWidgetProvider.class.getName() outputs and create a constant:

val WIDGET_CLASS_NAME = "com.example.yourapplication.YourWidgetProvider"

You would then be able to use this implicitly:

val intent = Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
val widgetManager = AppWidgetManager.getInstance(context)
val ids = widgetManager.getAppWidgetIds(ComponentName(context, WIDGET_CLASS_NAME))
AppWidgetManager.getInstance(context).notifyAppWidgetViewDataChanged(ids, android.R.id.list)
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
context.sendBroadcast(intent)

Solution 9 - Android

For reference, I had to do like this in a fragment:

Application app = getActivity().getApplication();
Intent intent = new Intent(context, RQWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);

int[] ids = AppWidgetManager.getInstance(app).getAppWidgetIds(new ComponentName(app, RQWidget.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
context.sendBroadcast(intent);

Solution 10 - Android

Example from my to-do list:

Intent intent = new Intent(this, TodosAppWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] ids = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), TodosAppWidget.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);
sendBrodcast(Intent(this, TodosAppWidget::class.java).apply {
  action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
  val ids = AppWidgetManager.getInstance(application).getAppWidgetIds(ComponentName(application, TodosAppWidget::class.java))
  putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
})

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNuviousView Question on Stackoverflow
Solution 1 - AndroidphaethonView Answer on Stackoverflow
Solution 2 - AndroidStuckView Answer on Stackoverflow
Solution 3 - AndroidVoidcodeView Answer on Stackoverflow
Solution 4 - AndroidPrilagaView Answer on Stackoverflow
Solution 5 - AndroidthanhtdView Answer on Stackoverflow
Solution 6 - AndroidJakob ErikssonView Answer on Stackoverflow
Solution 7 - AndroidElletlarView Answer on Stackoverflow
Solution 8 - AndroidgareokeView Answer on Stackoverflow
Solution 9 - AndroidInam Ul HuqView Answer on Stackoverflow
Solution 10 - AndroidEric XueView Answer on Stackoverflow