Android的主页屏幕小部件:RemoteViews setRemoteAdapter(...)方法不工作的API 11+部件、屏幕、方法、工作

2023-09-04 13:32:03 作者:一介俗人

那么的onupdate方法调用

So onUpdate method calls

remoteViews.setRemoteAdapter(ID,R.id.listview,意图)

为了到适配器适用于该插件的列表视图。

in order to apply an adapter to the listview in the widget.

有一个在数据集的列表视图显示可以改变窗口小部件的头一个按钮(想想收件箱,发件箱,主演等为假设的电子邮件小部件)。当我点击这个按钮,它需要用户的活动,让他们选择要显示的数据集。当选择时,下面的code运行时:

There is a button in the header of the widget which can change which data set is displayed by the listview (think inbox, outbox, starred, etc for a hypothetical email widget). When I click that button, it takes the user to an Activity which allows them to choose which data set to display. When the selection is made, the following code is run:

Intent intent = new Intent(this, WidgetReceiver.class);
intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
intent.putExtra("notify", true);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {R.xml.widget_provider});
sendBroadcast(intent);

这成功地调用控件的AppWidgetProvider类的OnUpdate方法。但是,如果我是不同类型的数据集之间进行切换,经过2-3个像preference的变化, setRemoteAdapter 方法简单地停止运行。我已经广泛地记录了过程,方法,这是应该调用哪个依次加载了 RemoteViewsService.RemoteViewsFactory 类填充部件和适配器的服务,做不会做这些事情。你改变了preference第几次,它将按预期工作。但后来它退出。

This successfully calls the onUpdate method in the widget's AppWidgetProvider class. However, if I were to toggle between different types of data sets, after like 2-3 preference changes, the setRemoteAdapter method simply stops functioning. I have extensively logged the process, and the method, which is supposed to call a service which in turn loads up the RemoteViewsService.RemoteViewsFactory class to populate the widget and its adapter, does not do any of these things. The first couple times you change the preference, it works as expected. But afterwards it quits.

有没有人有任何线索是怎么回事呢?

Does anyone have any clue what is going on here?

推荐答案

怪异,但找到了解决办法。 Android的似乎是缓存在您的onupdate方法使用的意图。如果重新发送什么似乎是同样的意图,它将无法达到你所期望的。

Weird, but found a solution. Android seems to be caching the intents you use in your onUpdate method. If you resend what seems to be the same intent, it won't perform as you'd expect.

解决方案:有一个静态的迭代整数,你包括如意图演员一个参数。它解决了我的问题。

Solution: have a static iterating integer which you include as a param in the intent extras. It solved the problem for me.

Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, currentWidgetId);
intent.putExtra("random", randomNumber);
randomNumber++;
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

呵呵,安卓...

Oh, Android...