Programmatically update widget from activity/servi

2019-01-03 01:25发布

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?

8条回答
Evening l夕情丶
2楼-- · 2019-01-03 01:43

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);
查看更多
淡お忘
3楼-- · 2019-01-03 01:43

Just get an AppWidgetManager instance and update it directly. Or, if you are using an IntentService to do the updates, call startService() on the IntentService. The AppWidgetProvider is a way to update the app widget, but it is not the only way.

查看更多
Luminary・发光体
4楼-- · 2019-01-03 01:46

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 :)

查看更多
欢心
5楼-- · 2019-01-03 01:48

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.

查看更多
祖国的老花朵
6楼-- · 2019-01-03 01:49

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);
查看更多
狗以群分
7楼-- · 2019-01-03 01:56

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:
class MyAppWidgetProvider : AppWidgetProvider() {

查看更多
登录 后发表回答