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

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

问题描述

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

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.我尝试将此单击事件也添加到文本视图中,但结果相同.

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 widget #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天全站免登陆