Android 从活动/服务/接收器以编程方式更新小部件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3455123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:16:04  来源:igfitidea点击:

Programmatically update widget from activity/service/receiver

androidandroid-activityandroid-widget

提问by Nuvious

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?

我知道这是可能的,但我想不出一种方法来从主要活动触发我的小部件更新。不是有一些我可以广播的一般意图吗?

回答by phaethon

If you areusing an AppWidgetProvider, you can update it this way:

如果您正在使用AppWidgetProvider,您可以更新这样说:

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

回答by Stuck

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

回答by Voidcode

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

对我有用:)

回答by Oleg Tarashkevich

Try this:

尝试这个:

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

回答by thanhtd

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:

如果您正在使用使用 ListView、GridView 或 StackView 等集合的小部件来更新小部件的项目,请执行以下操作:

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.

使用该方法 notifyAppWidgetViewDataChanged(),您可以强制小部件项实时获取任何新数据。

回答by Jakob Eriksson

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

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

回答by Elletlar

Phaethon's accepted solution in Kotlin:

Phaethon 在 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() {

其中 MyAppWidgetProvider 派生自 AppWidgetProvider:
class MyAppWidgetProvider : AppWidgetProvider() {

回答by gareoke

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:

如果您尝试以编程方式更新您无法显式访问 YourWidgetProvider.class 的小部件,例如从另一个模块或库,您可以捕获 YourWidgetProvider.class.getName() 输出的内容并创建一个常量:

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)