android - 如何禁用 Google Assistant API(Now on Tap)

标签 android android-6.0-marshmallow google-now google-assist-api

Google 最近发布了带有 Now on Tap 的 Android Marshmallow,它可以扫描应用程序内容并向用户提供额外信息。

不幸的是,对于我们的应用来说,这些信息看起来不太相关,Google 忽略了我们在 onProvideContentAssist()onProvideAssistData() 中设置的数据。

These specs看起来相当高级,还包含诸如“可以建议”和“附加信息”之类的词,因此 Google 似乎正式允许自己忽略应用程序开发人员提供的数据。

所以我们决定禁用 Now on Tap,但这似乎不是很简单。 根据上面提供的文档,在这种情况下我们应该使用 FLAG_SECURE。但随后用户无法捕获屏幕截图,Google Now on Tap 开始将以下面向用户的消息归咎于我们的应用:

Results not available
This app has blocked Now on Tap

enter image description here

但 Opera 似乎以某种方式温和地阻止了他们私有(private)标签的 Now on Tap。它为他们显示了更多应用友好的消息:

Nothing on tap

enter image description here

Opera 如何阻止 Now on Tap?

有谁知道如何阻止 Google Assist API(Now on Tap)而不会受到他们一方对我们应用程序的指责?

最佳答案

View 类有一个方法 setAssistBlocked :

Controls whether assist data collection from this view and its children is enabled (that is, whether {@link #onProvideStructure} and {@link #onProvideVirtualStructure} will be called). The default value is false, allowing normal assist collection. Setting this to false will disable assist collection.

@param enabled Set to true to disable assist data collection, or false (the default) to allow it.

不幸的是这个方法被@hide注解了,所以我们看不到也不能直接使用。

但是我们可以通过一个小的反射调用来使用它:

private void setAssistBlocked(View view, boolean blocked) {
        try {
            Method setAssistBlockedMethod = View.class.getMethod("setAssistBlocked", boolean.class);
            setAssistBlockedMethod.invoke(view, blocked);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

要阻止 Activity 中的所有内容,您可以使用:

final View rootContent = findViewById(android.R.id.content);
setAssistBlocked(rootContent, true);

关于android - 如何禁用 Google Assistant API(Now on Tap),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33043826/

相关文章:

java - Marshmallow 上 native WebView 中的 Instagram 链接

ios - iOS Google Now 如何显示不同的卡片模板

java - 指示器 OnPageChangeListener 不起作用

java - 无法实现 View OnClickListener 或 ToolTipView.OnToolTipViewClickedListener

android - 无法打开 avd_name.avd/cache.img

android - 在运行时拒绝和授予权限后在 Android 应用程序上获取 FileNotFoundException

android - 如何为 api 级别 11 构建 android AttributedString

android - 来自遥远站点的图片库 - Android

gmail - 架构未显示在 Google Now 或 Google 搜索中

android - 如何以编程方式强制退出(或暂时禁用)Google App