如何更新应用程序启动的窗口小部件 [英] How to update a widget on app start
问题描述
我有一个小部件与我的应用程序工作。当应用程序第一次启动我把背景和价值观,所以我可以从应用程序调用我的UpdateWidget()当事情发生改变。但是,如果应用程序犯规知道是什么背景和的AppID是我不知道如何获得这些信息。我想,我试图做到这一点的方式是错误的,无论如何,但不知道什么尝试。
所以基本上小部件是已经在桌面上。我启动应用程序,并做出一些改变,我需要将更新发送到应用程序的新信息。
我还以为在启动时应用程序将获得此连接到窗口小部件,但情况并非如此。
@覆盖
公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){
Toast.makeText(上下文中,的onUpdate,Toast.LENGTH_SHORT).show(); CT =背景;
ApMgr = appWidgetManager;
为widgetid = appWidgetIds; WidgetOn = TRUE; UpdateWidget(); }
当应用程序第一次启动我把背景
块引用>除非它是应用程序上下文,您正在泄漏内存。请不要泄漏内存。
和值,这样我就可以从应用程序调用我的UpdateWidget()时,事情改变
块引用>在你的应用程序的任何地方,你可能需要更新部件,你已经有一个
上下文
。例如:
但是,如果应用程序犯规知道是什么背景和的AppID是我不知道如何获得这些信息。
块引用>您绝对知道上下文的说法。这是你的
活动
。或者,如果有问题的code是在自定义应用
对象上下文是应用
对于应用程序部件的ID,将其存储在一个文件中。或数据库。只是一定要清理干净,当应用程序widget被卸载。
或者,如果你不支持多个应用程序窗口小部件,只跳过应用小部件ID干脆,并使用不带程序的数组
updateAppWidget()
方法控件的ID。I have a widget working with my app. When the app is first started I keep the context, and values so I can call my UpdateWidget() from the app when things change. But if the app doesnt know what the context and AppID is I dont know how to get that information. I think the way I tried to do this is wrong anyway, but not sure what else to try.
So basically the widget is already on the desktop. I start up the app and make some changes and I need to send an update to the app with the new info.
I would have thought the app would get this connection to the widget when it starts up, but not the case.
@Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Toast.makeText(context, "onUpdate", Toast.LENGTH_SHORT).show(); Ct = context; ApMgr = appWidgetManager; WidgetID = appWidgetIds; WidgetOn = true; UpdateWidget(); }
解决方案When the app is first started I keep the context
Unless that is the application context, you are leaking memory. Please do not leak memory.
and values so I can call my UpdateWidget() from the app when things change
Any place in your app where you could possibly need to update the widget, you already have a
Context
. For example:But if the app doesnt know what the context and AppID is I dont know how to get that information.
You absolutely know "what the context" is. It is your
Activity
. Or, if the code in question is in your customApplication
object, "the context" is theApplication
.With respect to the app widget ID, store it in a file. Or a database. Just be sure to clean it up when the app widget is uninstalled.
Or, if you are not supporting multiple app widgets, just skip the app widget ID altogether, and use the
updateAppWidget()
method that does not take an array of app widget IDs.这篇关于如何更新应用程序启动的窗口小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!