Home进程重启后解耦AppWidget Intent
我的 Android 应用程序 Transdroid 提供了几个主屏幕小部件。每个 AppWidget 都有 2 个“按钮”(ImageButton),一个启动应用程序,一个启动一些刷新 AppWidget 内容的活动。很简单。 这里是屏幕截图。小部件代码位于我的 Google 代码网站,但最重要的是:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_15);
views.setOnClickPendingIntent(R.id.widget_action, PendingIntent.getActivity(context, 0, new Intent(context, Transdroid.class), 0));
appWidgetManager.updateAppWidget(id, views);
问题是:重新启动 Home 进程后不会调用小部件的 onUpdate,因此用于将功能附加到按钮的 PendingIntents 丢失。
复制起来相当容易。
- 启动模拟器
- 添加一个小部件(使用 PendingIntent 来启动一个活动)
- 单击按钮查看其实际效果
- 强制终止主进程('adb -e shell Kill 96',其中 96 是 android.process 的 PID .acore)
- 小部件的按钮不再起作用。
更准确地说:当 android.process.acore Home 进程重新启动时,不会调用 onReceive,因此不会调用 onUpdate。反过来,没有附加意图。
有人遇到过同样的问题并且知道如何规避这个问题吗?
My Android app Transdroid offers several home screen widgets. Every AppWidget has 2 'buttons' (ImageButton), one starts the app and one starts some activity that refreshes the AppWidget content. Pretty simple. Here is a screenshot. The widget code is at my Google Code website, but most importantly:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_15);
views.setOnClickPendingIntent(R.id.widget_action, PendingIntent.getActivity(context, 0, new Intent(context, Transdroid.class), 0));
appWidgetManager.updateAppWidget(id, views);
The problem is: the widget's onUpdate is not called after the Home process is restarted, and hence the PendingIntents used to attach functionality to the buttons is lost.
It's fairly easy to reproduce.
- Start an emulator
- Add a widget (that uses a PendingIntent to, say, start an activity)
- Click the button to see it actually works
- Force kill the home proces ('adb -e shell kill 96' where 96 is the PID of android.process.acore)
- The widget's button doesn't work any more.
More precise: no onReceive and thus no onUpdate is called when the android.process.acore Home process is restarted. In turn, no Intent is attached.
Anyone experienced the same problem and knows how to circumvent this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚测试了几个我自己的应用程序小部件,它们的点击事件在杀死 acore 后工作正常。这是我的相关代码:
我可以看到的主要区别是与意图相关的标志;我想说它们绝对值得尝试。 [我的代码中的
layoutId
参数是在其他地方设置的;相同的代码用于具有不同布局的多个小部件]。I've just tested a couple of my own appwidgets, and their click events work fine after killing acore. Here's my relevant code:
The major difference I can see is the intent-related flags; I'd say they are definitely worth trying. [The
layoutId
parameter in my code is set elsewere; this same code is used for several widgets with different layouts].