如何更新 Android 应用程序小部件的 UI
我确信我错过了一些东西,但我只是想获得一个带有按钮和计数器的应用程序小部件。每次我单击按钮时,我希望计数器更新 1。
我设置了 WidgetProvider 的 onUpdate() 函数来向按钮注册一个待处理事件,以便它启动一个服务来增加计数器的数字:
Intent active = new Intent(context, CounterService.class);
active.setAction(CounterService.COUNT);
PendingIntent pending = PendingIntent.getService(context, 0, active, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.CountButton, pending);
ComponentName component = new ComponentName(context.getPackageName(), KickCounterWidgetProvider.class.getName());
appWidgetManager.updateAppWidget(component, views);
然后在服务 CounterService::onStart() 函数 我增加计数(目前存储在首选项中),然后尝试更新显示当前计数值的文本字段:
// ... bump the count here and store a string representation of it in currentCountString ...
RemoteViews remoteView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.widget);
remoteView.setTextViewText(R.id.CurrentKickCount, currentCountString);
// apply changes to widget
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
ComponentName component = new ComponentName(getApplicationContext().getPackageName(), KickCounterWidgetProvider.class.getName());
appWidgetManager.updateAppWidget(component, remoteView);
明智地使用 Logcat 表明这一切正常,并且字符串似乎是好的,但由于某种原因,对 appWidgetManager.updateAppWidget() 的调用似乎因某种原因默默失败。
我不知道它是否相关,但第一次将小部件的实例添加到主屏幕时,甚至按钮都不起作用(即,在提供程序的 onUpdate() 中对 updateAppWidget() 的调用失败)。小部件的后续实例对于调用 Provider 中的 updateAppWidget() 似乎可以正常工作,但对于服务却不起作用。
任何帮助将不胜感激。
I'm sure I'm missing something, but I'm just trying to get an app widget with a button and a counter. Every time I click the button I want the counter to update by 1.
I've set the onUpdate() function of the WidgetProvider to register a pending event to the button so that it starts a service to bump the counter numbver:
Intent active = new Intent(context, CounterService.class);
active.setAction(CounterService.COUNT);
PendingIntent pending = PendingIntent.getService(context, 0, active, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.CountButton, pending);
ComponentName component = new ComponentName(context.getPackageName(), KickCounterWidgetProvider.class.getName());
appWidgetManager.updateAppWidget(component, views);
Then in the services CounterService::onStart() function I bump the count (stored in prefs for now) and then try and update the text field that shows the current count value:
// ... bump the count here and store a string representation of it in currentCountString ...
RemoteViews remoteView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.widget);
remoteView.setTextViewText(R.id.CurrentKickCount, currentCountString);
// apply changes to widget
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
ComponentName component = new ComponentName(getApplicationContext().getPackageName(), KickCounterWidgetProvider.class.getName());
appWidgetManager.updateAppWidget(component, remoteView);
Judicious use of Logcat indicates tha this all works ok and the string seems to be OK, but for some reason the call to appWidgetManager.updateAppWidget() seems to be failing silently for some reason.
I don't know if it's related at all, but the first time I add an instance of the widget to the homescreen, not even the button works (ie the call to updateAppWidget() in onUpdate() in the Provider fails). Subsequent instances of the widget seem to work OK for the call to updateAppWidget() in the Provider but never works for the service.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从:
http://code.google.com/p/android/issues/detail ?id=8889
From:
http://code.google.com/p/android/issues/detail?id=8889
您的一般方法似乎是合理的,尽管
this
可能与这里的getApplicationContext()
一样有效。不过,你的最后一段完整的段落表明其他地方可能有问题。 这是一个有点复杂的示例项目,它演示了您正在使用的某种模式 - 在这种情况下,随机选择一家餐厅而不是去柜台。Your general approach seems sound, though
this
probably works as well asgetApplicationContext()
here. Your last full paragraph suggests that something else may be awry, though. Here is a somewhat complicated sample project that demonstrates the sort of pattern you're using -- in this case, randomly choosing a restaurant rather than bumping a counter.