系统重新启动我的进程后,AppWidget 点击丢失
我正在制作一个应用程序小部件,但我遇到了单击事件的问题,当系统终止小部件的进程并稍后重新启动它时,该事件会丢失。屏幕旋转后也会发生这种情况。
针对 SDK 版本 7 进行构建,并在模拟器 (2.1) 和 2.3.3 的真实设备上运行。
我的 onUpdate 方法:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int wid : appWidgetIds) {
Log.i(TAG, "onUpdate widget #" + wid);
Intent intent = new Intent(context, MyClass.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, wid);
PendingIntent clickIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);
widget.setOnClickPendingIntent(R.id.widget_layout, clickIntent);
appWidgetManager.updateAppWidget(wid, widget);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
其中 R.id.widget_layout 是 appwidget 线性布局的 id。我尝试将此单击事件也添加到文本视图中,但结果相同。
我与这个问题斗争了好几天,我发现有些人也有同样的问题,但没有解决方案适合我:(我也尝试了不同的挂起意图标志,但没有成功。
第二个问题是,当我在主屏幕上添加另一个应用程序小部件时,它不会对点击事件做出反应。在 logcat 中,我看到来自 onUpdate 方法“onUpdate widget #xy”的消息,但应用程序小部件不会对点击做出反应,只有放置在主屏幕上的第一个应用程序小部件才会对点击做出反应,但只会持续一段时间。有什么想法吗?
i am making an appwidget and i have problems with click event, which is lost when system kills the widget's process and later restarts it. this also happens after screen rotate.
building against SDK version 7 and running on emulator (2.1) and a real device with 2.3.3.
my onUpdate method:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int wid : appWidgetIds) {
Log.i(TAG, "onUpdate widget #" + wid);
Intent intent = new Intent(context, MyClass.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, wid);
PendingIntent clickIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);
widget.setOnClickPendingIntent(R.id.widget_layout, clickIntent);
appWidgetManager.updateAppWidget(wid, widget);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
where R.id.widget_layout is id of linear layout of the appwidget. i tried to add this click event also to a textview, but with same result.
i am fighting this problem for several days and i found some people with this same problem, but no solution works for me :( i also tried different pending intent flags without any success.
second problem is, when i add another appwidget on home screen, it does not react to click events. in logcat i see the message from onUpdate method "onUpdate widget #xy", but the appwidget does not react to clicks. only the first appwidget placed on home screen reacts to clicks, but only for some time. any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您说第一个小部件停止响应点击时,您的意思是
onUpdate
方法没有被调用吗?也许将一些代码放入onEnabled(Context context)
查看是否正在调用该函数,如果是,则在该函数中放入必要的任何逻辑。此外,您还可以通过onReceive
方法(可在同一链接中找到)捕获意图,以查看您的小部件实际接收到哪些意图。另外,请确保运行接收器的上下文(传递给此函数的上下文)是应用程序或服务,而不是活动,否则对其的引用可能不会持久。
您还必须确保每次使用
RemoteViews
对象更新小部件时,都会发送完全重建小部件所需的所有数据。这是因为,例如,在屏幕旋转时,除了您传递给它的最新RemoteViews
之外,系统没有任何东西可以重建小部件。When you say the first widget stops responding to clicks, do you mean that the
onUpdate
method is not being called? Perhaps put some code inonEnabled(Context context)
to see if that's being called instead, and if so, put whatever logic is necessary in that function. Also, you can catch intents via theonReceive
method (found at the same link) to see which ones your widget is actually receiving.Also, ensure that the
Context
that you have the receiver running in (the one that gets passed to this function) is an Application or Service, and not an Activity, or the reference to it may not persist.You must also make sure that every time you update the widget with a
RemoteViews
object, you send all of the data needed to fully reconstruct the widget. This is because on, e.g., screen rotation, the system doesn't have anything to reconstruct the widget with beside the very latestRemoteViews
you passed it.