通过 RemoteViews 从服务更新小部件 - 在手机上可以,但在模拟器上不起作用
我创建了一个 WidgetProvider 和一个在系统 TIME_TICK 上触发的服务 - 该部分工作正常,日志消息显示该服务工作正常,并且在实际手机上看起来很棒。
为了更新我的小部件,我使用这样的代码(为清楚起见进行了编辑,因此可能有拼写错误)
Log.d("xxx","Updating");
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
views.setTextViewText(R.id.appwidget_text, "Some Text" + System.currentTimeMillis());
ComponentName thisWidget = new ComponentName(context, SWClockWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, views);
在我的手机(运行 2.2 的 HTC Desire)上,它是完美的,小部件每分钟都会更新。
在模拟器上(运行 2.2)会出现日志消息(例如服务正在运行),但小部件不会更新。
现在我在两个地方调用该代码 - 从 Provider 的 onUpdate 方法(以便在首次显示时将某些内容放入 Widget 中)和从 Service 的侦听器(以更新它们)
现在这是奇怪的一点 - 在我的我最初像这样声明我的
服务 android:name=".SWClockWidgetService"
当我更改它以明确提及该包(当然与提供者的包相同)
服务时android:name="com.somewhatdog.swclockwidget.SWClockWidgetService"
初始更新(从提供者 onUpdate 调用)有效,但来自服务侦听器的后续调用仍然无效!?
注意:最初我将服务作为提供者的内部类,但这在手机或模拟器上不起作用 - 不知道这是否相关......
无论如何 - 我很困惑 - 手机和模拟器上的相同 Android - 一个可以工作,一个没有(这意味着至少可以说,在其他设备上工作的可能性是喜怒无常的?)
我在这里有点迷失了 - 任何建议apprec。
ps 我已经在运行 1.6 的模拟器上对此进行了测试,没有乐趣 - 它可以在运行 2.1r1 和 2.3.3 的模拟器上运行,所以谁知道......
I've created a WidgetProvider and a Service which triggers on the system TIME_TICK - that part is working fine, Log messages show the Service is working AOK and it looks great on an actual phone.
To update my Widgets I use code like this (editted for clarity so it might have a typo)
Log.d("xxx","Updating");
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
views.setTextViewText(R.id.appwidget_text, "Some Text" + System.currentTimeMillis());
ComponentName thisWidget = new ComponentName(context, SWClockWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, views);
On my phone (an HTC Desire running 2.2) it's perfect, the Widgets update bang-on the minute, every minute.
On the emulator (running 2.2) Log messages appear (e.g. the service is working) but widgets aren't updated.
Now I call that code in 2 places - from the onUpdate method of the Provider (so that it puts something into the Widgets when they're first displayed) and from the Service's listener (to update them)
Now here's the weird bit - in my manifest I originally declared my service like this
service android:name=".SWClockWidgetService"
When I changed that to explicitly mention the package (which is identical to the Provider's package of course)
service android:name="com.somewhatdog.swclockwidget.SWClockWidgetService"
the initial update (called from the Provider onUpdate) works BUT subsequent calls from the Service's listener still don't!?
Note: Originally I made the Service an inner-class of the provider but that didn't work on a phone OR emulator - no idea if that's related...
Anyway - I'm mystified - same Android on phone and emulator - one works, one doesn't (which means the odds of this working on other devices are moody to say the least??)
I'm more than a bit lost here - any advice apprec.
p.s. I've tested this on an emulator running 1.6 no joy - it works on an emulator running 2.1r1 and 2.3.3 tho, so who knows...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模拟器小部件更新似乎有点棘手。它有两个已知问题:
updateAppWidget(...)
调用 可能仅适用于至少重新启动一次的模拟器。The emulator widget update seems to be a bit dodgy. There are two known issues with it:
updateAppWidget(...)
calls may only work on emulators that have been restarted at least once.进一步的实验表明,模拟器内的 Widget 更新刚刚被破坏。
即使您部署新代码,现有的小部件也保持不变!
使用 Androidx86 进行测试表明问题仅限于模拟器 - 所以我想我会用它来进行 Widget 测试......
Much further experimentation suggests that Widget updating within the Emulator is just broken.
Even if you deploy new code, existing Widgets remain unchanged!
Testing using Androidx86 suggests the problem is confined to the emulator tho - so I guess I'll use that for Widget testing instead...