从活动/服务/接收器以编程方式更新小部件 [英] Programmatically update widget from activity/service/receiver

查看:33
本文介绍了从活动/服务/接收器以编程方式更新小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是可能的,但我无法找到一种方法来从主要活动触发我的小部件更新.不是有一些我可以传播的通用意图吗?

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?

推荐答案

如果你正在使用 AppWidgetProvider,你可以这样更新它:

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

这篇关于从活动/服务/接收器以编程方式更新小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆