系统重新启动我的程序后,AppWidget单击丢失 [英] AppWidget click lost after system restarts my process

查看:96
本文介绍了系统重新启动我的程序后,AppWidget单击丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个appwidget,但单击事件有问题,当系统终止该小部件的进程并随后重新启动它时,单击事件会丢失.屏幕旋转后也会发生这种情况.

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.

根据SDK版本7构建,并在仿真器(2.1)和带有2.3.3的真实设备上运行.

building against SDK version 7 and running on emulator (2.1) and a real device with 2.3.3.

我的onUpdate方法:

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

其中R.id.widget_layout是该appwidget的线性布局的ID.我试图将此点击事件也添加到textview中,但结果相同.

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.

第二个问题是,当我在主屏幕上添加另一个appwidget时,它对点击事件没有反应.在logcat中,我看到了来自onUpdate方法"onUpdate小部件#xy"的消息,但是appwidget对点击没有反应.只有放在主屏幕上的第一个appwidget会对点击做出反应,但仅持续一段时间.有什么想法吗?

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?

推荐答案

当您说第一个窗口小部件停止响应单击时,是否表示未调用onUpdate方法?也许将一些代码放在 onEnabled(Context context) 查看是否正在调用该函数,如果是,则在该函数中放入任何必要的逻辑.另外,您可以通过onReceive方法(在同一链接中找到)捕获意图,以查看您的窗口小部件实际上正在接收哪些意图.

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 in onEnabled(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 the onReceive method (found at the same link) to see which ones your widget is actually receiving.

此外,请确保正在运行接收器的Context(传递给此函数的接收器)是应用程序或服务,而不是活动,否则对它的引用可能不会持久.

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.

还必须确保每次使用RemoteViews对象更新窗口小部件时,都发送完全重建窗口小部件所需的所有数据.这是因为,例如在屏幕旋转时,系统没有任何东西可以用来重建与您传递的最新RemoteViews相邻的窗口小部件.

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 latest RemoteViews you passed it.

这篇关于系统重新启动我的程序后,AppWidget单击丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆