窗口小部件换成其他内容时,在Android的编程方式刷新 [英] Widget replaced by other content when programmatically refreshing in Android
问题描述
在这里的某个地方对堆栈溢出,我发现下面的code有一天,我调整我的应用程序了一下:
Somewhere here on Stack Overflow, I had found the following code some day which I adjusted to my application a bit:
private void updateWidget() {
AppWidgetManager widgetManager = AppWidgetManager.getInstance(ctx);
ComponentName widgetComponent = new ComponentName(ctx, MyAppWidgetProvider.class);
int[] widgetIds = widgetManager.getAppWidgetIds(widgetComponent);
Intent update = new Intent();
update.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds);
update.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
ctx.sendBroadcast(update);
}
这应该编程刷新应用程序的窗口小部件的所有实例。 CTX
是活动的
上下文(这个
)的我在的onCreate设置一次()
。上面的方法是所谓的活动的
的onStop()
方法。
This should programmatically refresh all instances of the application's widget. ctx
is the Activity
's context (this
) that I set once in onCreate()
. The method above is called in the Activity
's onStop()
method.
不幸的是,当它被调用,它取代了其他应用的插件(例如AP新闻)应用程序的窗口小部件 - 至少一段时间
Unfortunately, when it is called, it replaces the app's widget by other apps' widgets (e.g. AP News) - at least for a while.
怎么能这样呢?有什么不对的code?
How can this happen? Is there something wrong in the code?
感谢您!
编辑#1:为了更清楚地指出这一点:我已经定义了自动刷新的时间间隔。但除此之外,我想从时间更新从活动
小部件时间。 这个问题的表明,这是可能的。
Edit #1: To point this out more clearly: I've already defined an interval for automatic refreshing. But in addition to that, I would like to update the widget from the Activity
from time to time. This question suggests that it is possible as well.
编辑#2:我刚刚看到了错误的部件只显示几秒钟。在那之后,我自己的应用程序的窗口小部件再次出现。
Edit #2: I've just seen that the wrong widget is only shown for some seconds. After that, my own app's widget is shown again.
推荐答案
我遇到了一些问题,在过去的窗口小部件,并解决了他们。我真的不知道为什么我的解决方案,为我工作的原因。而作为它的工作对马尔科·W·的,所以我在这里把它:我所做的感动更新code键的服务。当我需要更新,启动该服务。同样,我不知道这一点,但问题就解决了。
I had some problems with widgets in the past, and have solved them. I really do not know the reason why my solution worked for me. And as it worked for Marco W., so I place it here: all I did was moved the update code to a service. When I need to update, start that service. Again, I'm not sure about that but the problems were solved.
感谢马可,我得到了一些更多的经验与Android。这很有趣: - )
Thanks Marco, I got some more experiences with Android. It's funny :-)
这篇关于窗口小部件换成其他内容时,在Android的编程方式刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!