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?
10 Answers
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())
.getAppWidgetIds(new ComponentName(getApplication(), MyAppWidgetProvider.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);

- 195,646
- 29
- 319
- 436

- 2,664
- 1
- 18
- 7
-
30Where do you set `widgetId`? – Kris B Jul 12 '12 at 17:59
-
1Does the AppWidgetProvider then execute the onUpdate() method? – IgorGanapolsky Sep 26 '12 at 00:54
-
38@KrisB from the answer below, get the array by doing this: int ids[] = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), WidgetProvider.class)); – MobileMon Nov 15 '12 at 12:42
-
@phaethon i am also facing the same problem, and i used ur code to update app widget on activity finish, But in my case it called the onUpdate() method of widget provider class, but after that failed to call the RemoteViewsService class. My code snapshot is this http://stackoverflow.com/questions/15523736/app-widget-issue-while-showing-list-of-images?noredirect=1#comment22016477_15523736 – AndroidDev Mar 21 '13 at 12:00
-
It call the onRecieve override method. But question is about how to call onUpdate method. – Shakeeb Ayaz Jul 05 '13 at 08:35
-
11You can use a constant, no need to hardcode: `intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);` – Mārtiņš Briedis Jul 16 '13 at 18:46
-
Great answer. Since ids is required only for the sake of firing onUpdate at least in my case, I just fake an id as following:int[] ids = {0}; I obtain all the real widget IDs in my service class. – Hong Aug 29 '15 at 21:56
-
i have added the above code. it calls onUpdateMethod(), but its not refreshing the list-view with newly updated database values. – Happy Feb 09 '17 at 05:10
-
3DON'T follow @gelupa comment to use R.xml.mywidget for widgetID! It is COMPLETELY wrong and just cost me 4hours of debugging!!! USE MobileMon solution to get correct widgetID – Ilja S. Jun 11 '17 at 13:24
-
5for ids: ` int widgetIDs[] = AppWidgetManager.getInstance(activity).getAppWidgetIds(new ComponentName(activity, widget.class));` – abbasalim Jun 22 '17 at 13:58
-
Easily converted to Xamarin (C#). Only issue was on creating the ComponentName had to use "Java.Lang.Class.FromType(typeof(MyAppWidgetProvider)).Name". – StuffOfInterest May 05 '18 at 11:12
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);

- 11,225
- 11
- 59
- 104
-
I know I'm jumping in here like 6 months late. But I agree with Andrew. I tried the original method in this thread. And something about how the IDs are grabbed caused it to start failing. This method (of directly updating the fields manually) works better. For my purposes, at least. – durbnpoisn Apr 26 '14 at 11:35
-
Awesome stuff. Dont know how you ended up with this but works fine and smooth for all the instances of my Widget. :) – Atul O Holic May 15 '14 at 10:11
-
this worked for me as well. I thought we would need to do broadcast and onReceive, but my requirements fit perfectly with this – stingray_ Aug 06 '18 at 17:40
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 :)

- 1,229
- 1
- 18
- 22
-
It call the onRecieve override method. But i want how to call onUpdate method. – Amit Thaper Feb 18 '13 at 06:52
Try this:
int[] ids = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), MyWidget.class));
MyWidget myWidget = new MyWidget();
myWidget.onUpdate(this, AppWidgetManager.getInstance(this),ids);
-
2Amazingly, only this little answer solved it. I still don't know the implications of instantiating the Widget class and directly calling the onUpdate method, but hey, it finally works :) – Henrique de Sousa Sep 25 '14 at 21:06
-
1Thanks. I am testing it out now and will post feedback. Tested and confirmed working. I am wondering through, if there are different types of widgets, will it work the same? (+1 by the way for working) – Si8 Nov 21 '16 at 11:05
-
-
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.

- 391
- 3
- 10
int widgetIDs[] = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), WidgetProvider.class));
for (int id : widgetIDs)
AppWidgetManager.getInstance(getApplication()).notifyAppWidgetViewDataChanged(id, R.id.widget_view);
}

- 18,597
- 1
- 25
- 34
-
1
-
Awesome. You don't even have to loop, there's a version of `notifyAppWidgetViewDataChanged()` that takes an array. – Lawrence Kesteloot Mar 05 '16 at 01:18
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() {

- 3,136
- 7
- 32
- 38
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)

- 716
- 6
- 17
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);

- 732
- 7
- 8
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)
})

- 117
- 1
- 9