安卓appwidget不会从活动更新 [英] android appwidget won't update from activity
问题描述
我有一个简单appwidget,我要当一个动作发生在一个活动(在相同的应用程序)来更新它。中的OnUpdate(),我立刻更新小部件,它工作正常。在我的活动,我呼吁我的appwidget相同的静态更新方法被调用的OnUpdate()来更新视图。窗口小部件不更新
I have a simple appwidget and I want to update it when an action occurs in an activity (in the same app). in onUpdate(), I immediately update the widget, which works fine. In my activity, I call the same static update method in my appwidget that is called in onUpdate() to update the views. the widget is not updated.
我可以跟踪code右进AppWidgetManager.updateAppWidget()方法,而这一切的好,但小部件不更新。
I can trace the code right into the AppWidgetManager.updateAppWidget() method, and all this good, but the widget does not update.
唯一可能的区别,我可以看到的是,传递到我的静态更新方法的上下文对象是不同的,当它从一个活动与一个appwidget的的OnUpdate()方法的上下文的情况下调用。不过,所以我希望它应该有很多的例子这在网络上。
The only possible difference I can see is that the context object passed into my static update method is different, when it's called from the context of an activity vs. the context of a appwidget's onUpdate() method. however, there are lots of examples on the web of this so I expect it should work.
推荐答案
没有看到你的code我不是100%肯定你是如何试图做到这一点,但这里是我使用的方法。在我的活动
我有以下方法:
Without seeing your code I'm not 100% sure how you are trying to do it, however here is the method I use. Within my Activity
I have the following method:
private void updateAllWidgets(){
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(this, MyWidget.class));
if (appWidgetIds.length > 0) {
new MyWidget().onUpdate(this, appWidgetManager, appWidgetIds);
}
}
其中,进myWidget
是类appwidget的。我可以从我的活动
的任何地方调用此方法来更新我的所有appwidgets。
Where MyWidget
is the class of the appwidget. I can call this method from anywhere within my Activity
to update all my appwidgets.
这篇关于安卓appwidget不会从活动更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!