应用窗口小部件被Google声音搜索劫持了吗? [英] App Widget getting hijacked by Google Sound Search?

查看:115
本文介绍了应用窗口小部件被Google声音搜索劫持了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在写的一个Appwidget中看到了一些奇怪的行为.

So I'm seeing some bizarre behavior in an appwidget that I wrote.

小部件本身非常简单-它从持久性存储中读取一些值,然后将它们显示为文本.布局没什么花哨的-只是一个FrameLayout根元素,其中包含一些Linear LayoutTextView子级.

The widget itself is quite simple - it reads a few values from persistent storage and then displays them as text. Nothing fancy in the layout - just a FrameLayout root element with some Linear Layout and TextView children.

该小部件具有与之关联的简单配置活动.

The widget has a simple configuration activity associated with it.

奇怪的行为是,在用户关闭配置活动后,该小部件最初将显示问题正在加载小部件",然后在几秒钟后显示"Google声音搜索"按钮(然后单击该按钮实际上会启动Google声音搜索).然后,再过几秒钟,它终于显示了预期的显示.

The bizarre behavior is that the widget will initially show "Problem Loading Widget" after the user closes the configuration activity, and then after a few seconds it shows a "Google Sound Search" button (and clicking on the button actually does launch Google Sound Search). Then, after a few more seconds, it finally shows the expected display.

我现在正远离我的代码,因此我必须等到今晚才能发布代码段.但是,与此同时,任何人都可以对这种事情的发生情况提供一些见解吗?有没有其他人经历过?另一个小部件如何劫持"我?

I am away from my code right now, so I'll have to wait until tonight to post code snippets. However, in the meantime, can anyone provide some insight into how such a thing could happen? Has anyone else ever experienced this? How could another widget "hijack" mine?

谢谢, 罗恩(Ron)

Thanks, -Ron

以下是一些屏幕截图:

Here are some screenshots:

推荐答案

好的,所以我知道了.如果有人遇到此问题,请在此处发布.我认为Android Developer文档在这里有些误导.

ok, so I figured it out. Posting here in case anyone else runs into this. I think that the Android Developer docs are a little misleading here.

问题在于,在我的配置活动中,我在结尾处有以下代码:

The problem was that in my configuration Activity, I had this code at the end:

    Intent intent = new Intent();
    intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {mAppWidgetId});
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
    setResult(RESULT_OK, intent);
    sendBroadcast(intent);
    finish();

建议提供的文档中提供额外的EXTRA_APPWIDGET_ID的意图google .

但是,同一文档指出,您必须通过创建RemoteView并调用AppWidgetManager.updateAppWidget()来更新小部件的视图,如下所示:

However, that same document says that you have to update the widget's view by creating a RemoteView and calling AppWidgetManager.updateAppWidget() like so:

RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.example_appwidget);
appWidgetManager.updateAppWidget(mAppWidgetId, views);

我不喜欢将表示逻辑放在配置活动和小部件类中的想法,所以我决定在配置活动结束时广播一个意图,告诉小部件重新绘制自己.这就是为什么在活动结束时出现setResult()sendBroadcast()的原因.该文档进一步指出,在使用配置活动时,不会调用onUpdate()回调.因此,这似乎是必要的.我将ACTION_APPWIDGET_UPDATEEXTRA_APPWIDGET_IDS添加到了意图中,以便它将触发onUpdate()方法. SO答案(尽管未包含在活动结果意图中,但我还是建议将这种做法与它没有任何作用.)

I didn't like the idea of placing the presentation logic in both the configuration activity and the widget class, so I instead decided to broadcast an intent at the end of the configuration activity to tell the widget to redraw itself. That's why I have setResult() AND sendBroadcast() at the end of the activity. The documentation further states that the onUpdate() callback will not be called when using a configuration activity. So this seemed neccessary. I added the ACTION_APPWIDGET_UPDATE and the EXTRA_APPWIDGET_IDS to the intent so that it would trigger the onUpdate() method. This practice was recommended by this SO answer (albeit without being included in the activity result intent - but I tried separating the two and it had no effect).

现在,我不确定"Google声音搜索"小部件是如何准确进入其中的,也不完全了解意图如何相互作用以产生观察结果的机制.但是,一旦我用文档中所述的代码替换了上面的代码,该小部件就会正确更新.

Now I'm not certain exactly how the "Google Sound Search" widget got in there, nor do I fully understand the mechanics of how the intents interacted to produce the observed results. However, as soon as I replaced my code above with the code stated in the docs, the widget was updated properly.

    Intent resultIntent = new Intent();
    resultIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
    setResult(RESULT_OK, resultIntent);
    finish();

这似乎与文档中有关配置活动必须更新窗口小部件的视图的声明相矛盾.只需提供如下所示的配置活动结果即可触发小部件中的onUpdate()方法,从而允许小部件重绘自身.我在运行API 23的仿真器上以及在运行Samsung android风格的Samsung设备上确认了该行为.

This seems to contradict the documentation's statement that the configuration activity must update the widget's view. Simply providing the configuration activity result as below triggers the onUpdate() method in the widget, thus allowing the widget to redraw itself. I confirmed the behavior on an emulator running API 23 and also on a Samsung device running Samsung's android flavor.

这篇关于应用窗口小部件被Google声音搜索劫持了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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