我如何更新一个Android appwidget的UIAndroid、appwidget、UI

2023-09-06 06:01:53 作者:雨落倾城夏未凉

我敢肯定,我失去了一些东西,但我只是想获得一个按钮和一个柜台一个应用程序部件。每次我按一下按钮我想计数器1进行更新。

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.

我已经设置了WidgetProvider的的onUpdate()函数,以便它启动的服务碰到柜台numbver注册未决事件到按钮:

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);

然后在服务CounterService ::在onStart()函数的凹凸我伯爵(存储在preFS现在),然后尝试和更新的文本字段,显示当前计数值:

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);

明智地使用LogCat中的指示THA这一切工作正常,且串似乎是好的,但由于某些原因调用appWidgetManager.updateAppWidget(),似乎因为某种原因被悄悄地失败了。

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.

我不知道它是否在所有的关系,但我第一次添加的小部件的一个实例到主屏幕,甚至没有按钮的作品(即在提供调用updateAppWidget()中的onUpdate()失败)。小部件的后续实例似乎使该呼叫updateAppWidget()在提供工作确定,但从来没有适​​用于该服务。

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.

任何帮助将大大AP preciated。

Any help would be greatly appreciated.

推荐答案

来源:http://$c$c.google.com/p/android/issues/detail?id=8889

在安装部件到  刚抹的或新建的AVD,  appWidgetManager.updateAppWidget电话  没有更新各自的结果  小部件。意图是正确的  接收时,呼叫被称为  通常情况下,还没有更新部件  发生。

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.

如果您重新启动AVD(有或  没有小部件的安装包  在设备上),这个问题要停止  第一个非新鲜存在后  初始化启动。

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.

这个问题似乎在2.0和存在  2.1,1.5,1.6和2.2是表现如预期。

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