如何更新 Android 应用程序小部件的 UI

发布于 2024-10-03 00:33:21 字数 1563 浏览 2 评论 0原文

我确信我错过了一些东西,但我只是想获得一个带有按钮和计数器的应用程序小部件。每次我单击按钮时,我希望计数器更新 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望笑 2024-10-10 00:33:21

从:
http://code.google.com/p/android/issues/detail ?id=8889

将小部件安装到
刚刚擦除或新创建的 AVD,
appWidgetManager.updateAppWidget 调用
没有更新相应的
小部件。意图正确
收到,呼叫被调用
通常,但没有小部件更新
发生。

如果您重新启动 AVD(使用或
没有安装小部件的包
在设备上)问题不再出现
在第一个非新鲜之后存在
初始化启动。

这个问题似乎存在于 2.0 和
2.1、1.5、1.6 和 2.2 上的行为符合预期。

From:
http://code.google.com/p/android/issues/detail?id=8889

When installing a widget onto a
freshly wiped or newly created AVD,
appWidgetManager.updateAppWidget calls
are not updating the respective
widgets. Intents are properly
received, the calls are called
normally, yet no widget updates
occur.

If you restart the AVD (with or
without the widget's package installed
on the device) the problem cease to
exist after the first non freshly
initialized boot.

The issue seems to exist on 2.0 and
2.1, on 1.5, 1.6 and 2.2 it is behaving as expected.

乖不如嘢 2024-10-10 00:33:21

您的一般方法似乎是合理的,尽管 this 可能与这里的 getApplicationContext() 一样有效。不过,你的最后一段完整的段落表明其他地方可能有问题。 这是一个有点复杂的示例项目,它演示了您正在使用的某种模式 - 在这种情况下,随机选择一家餐厅而不是去柜台。

Your general approach seems sound, though this probably works as well as getApplicationContext() 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文