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

时间:2010-08-11 03:43:03

标签: android android-activity android-widget

我知道这是可能的,但我无法找到一种方法来触发主要活动中我的小部件的更新。我可以播放一些普遍意图吗?

11 个答案:

答案 0 :(得分:170)

如果 使用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);

答案 1 :(得分:67)

这有助于我搜索如何从服务/或操作更新小部件(但可能来自每个上下文):

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

答案 2 :(得分:28)

因此,要从活动更新窗口小部件,您可以这样做:

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

适合我:)

答案 3 :(得分:24)

试试这个:

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

答案 4 :(得分:12)

如果您正在使用使用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);

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

答案 5 :(得分:11)

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

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

答案 6 :(得分:5)

只需获取AppWidgetManager个实例并直接更新即可。或者,如果您使用IntentService进行更新,请致电startService()上的IntentServiceAppWidgetProvider一种更新应用小部件的方式,但这不是唯一的方法。

答案 7 :(得分:3)

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

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

您将可以隐式使用它:

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)

答案 8 :(得分:1)

我的待办事项列表中的示例:

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

答案 9 :(得分:0)

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)

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

答案 10 :(得分:0)

作为参考,我必须在一个片段中这样做:

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