gpt4 book ai didi

android - 如何更新 Android 应用小部件的用户界面

转载 作者:太空狗 更新时间:2023-10-29 16:25:01 25 4
gpt4 key购买 nike

我确定我遗漏了什么,但我只是想获得一个带有按钮和计数器的应用程序小部件。每次单击按钮时,我都希望计数器更新 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() 函数中我增加计数(目前存储在 prefs 中)然后尝试更新显示当前计数值的文本字段:

// ... 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() 的调用似乎由于某种原因静默失败。

我完全不知道它是否相关,但是我第一次将小部件的实例添加到主屏幕时,甚至按钮都不起作用(即在 Provider 的 onUpdate() 中调用 updateAppWidget() 失败).小部件的后续实例似乎可以正常调用 Provider 中的 updateAppWidget(),但永远无法用于服务。

如有任何帮助,我们将不胜感激。

最佳答案

来自: 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.

关于android - 如何更新 Android 应用小部件的用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4198901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com