【问题标题】:What is the new generated code "This was auto-generated to implement the App Indexing API."?什么是新生成的代码“这是自动生成的以实现 App Indexing API。”?
【发布时间】:2016-04-01 14:03:15
【问题描述】:

背景

我今天刚刚创建了一个新的 POC(关于活动转换,但这不是主题),我注意到在主要活动的“onCreate”方法中编写了一行新代码:

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    mClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

还有更多:

@Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        mClient.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "SinglePhotoViewer Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.example.user.transitionstest/http/host/path")
        );
        AppIndex.AppIndexApi.start(mClient, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "SinglePhotoViewer Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.example.user.transitionstest/http/host/path")
        );
        AppIndex.AppIndexApi.end(mClient, viewAction);
        mClient.disconnect();
    }

这已添加到清单中:

<!-- ATTENTION: This was auto-generated to add Google Play services to your project for
        App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>

问题

看了写的网站,还是不明白是什么。

我猜this 是如何使用它的,但我不明白它是如何工作的。

另外,奇怪的是我创建的任何其他新项目都没有显示这行新代码

问题

  1. 这是什么?它有什么作用?
  2. 我该怎么处理它?
  3. 是否有任何自定义?有什么建议吗?
  4. 在哪些情况下会生成这行代码?我没有注意到它是如何以及何时创建的...

根据我在网站上阅读和看到的内容,我的猜测是,这仅用于可以执行某种搜索的应用程序,以便 Google 可以向用户显示以前的查询和更快的结果。

【问题讨论】:

  • 这只是偶然发生在我身上,因为它是默认的自动完成选项......现在我的构建失败了。如果我将附加代码从 Gradle 文件中删除,并且只在 AndroidStudio 之外触摸磁盘上的文件,然后告诉它同步 Gradle 文件,那么附加代码就会神奇地回来。

标签: android google-api-client android-app-indexing


【解决方案1】:

您是对的:Android Studio 会自动为您创建该代码,以帮助实现 App Indexing API。

但是,它不是通过简单地向您的应用添加新活动来创建的。您需要明确要求 Android Studio 创建此代码。然后,您需要使用活动的详细信息对其进行更新:操作类型、标题、深层链接、相应网页(如果存在)。

要为您生成此代码,您可以通过Alt + Enter使用弹出意图列表,选择“Insert App Indexing API Code”:

或者你可以使用Alt+Insert弹出代码生成列表,选择“App Indexing API Code”:

这是相关的 Google Developers 文档:

https://developers.google.com/app-indexing/android/test#link-creation

您实际上只需要调整四个部分:

// What type of action is this? (TYPE_VIEW, TYPE_LISTEN, TYPE_WATCH, etc...)    
Action.TYPE_VIEW

// Title of your page, just like the web
"SinglePhotoViewer Page"

// The web url of corresponding content, if exists, otherwise leave blank, ""
Uri.parse("http://host/path") 

// Your deep link starting with "android-app://"
Uri.parse("android-app://com.example.user.transitionstest/http/host/path")

作为最佳做法,您应该选择最准确地描述应用中该深层链接位置的内容的标题。就像您在 HTML 网页标题中的 &lt;TITLE&gt;&lt;/TITLE&gt; 标记中一样。

实施后,您的最终用户查看的任何活动都会将此深层链接报告给 Android 操作系统。然后,当用户在 Google 快速搜索框中键入查询时,它会出现在建议自动完成结果中。如果用户查询按关键字匹配您的标题,您的应用图标和您提供的标题将显示在建议结果中。

下面是一个示例,从最终用户的角度来看,在 Live Nation 应用程序中,假设他之前访问过左侧建议结果中显示的两个页面:

此外,通过实施 App Indexing API,您将在搜索结果中获得排名提升,如您在原始问题中提供的链接中所述:

这将为您的应用用户启用查询自动完成功能,以及 更丰富的搜索结果、改进的搜索质量和增强的排名 信号。

最后,您可能有兴趣将此代码实验室作为附加资源:

https://codelabs.developers.google.com/codelabs/app-indexing/#0

【讨论】:

  • 很有趣,但我还是不明白:它是做什么用的?它对用户有什么作用?它是如何工作的?
  • 你确定不是自动插入的吗?我从来没有要求 AS 插入这段代码,但它在不同的项目中做了 3 次。
  • @Christine,是的,如果没有用户的任何触发,应该不可能自动生成此代码。是否有可能您正在使用键盘快捷键?
  • 这一定是通过键盘快捷键无意激活的。有时其中一只猫会跳到我的键盘上,也许它们按下了正确的键。就像他们喜欢按 ctrl-minus 使浏览器字符变小一样。
  • @pferg 我可以毫无顾虑地删除它吗?好像是在不知不觉中触发的。
【解决方案2】:

如果这有帮助,您可以通过以下方式禁用该选项:

Settings > Intentions > Android > Insert App Indexing API code

并取消选中它。

【讨论】:

  • 无法找到最后一部分 - “插入 App Indexing API 代码” - 是否有任何特定部分可供搜索
【解决方案3】:

您可以通过在以下位置取消选中该选项来禁用该选项:

Settings > Editor > Intentions > Android > Insert App Indexing API code

要找到它,请在“文件”>“设置”窗口的搜索框中键入“api 代码”。 它在我安装的 Android Studio 2.2.3 中

【讨论】:

  • 我在那里看不到。
猜你喜欢
  • 1970-01-01
  • 2010-12-19
  • 1970-01-01
  • 2023-01-16
  • 2022-11-17
  • 1970-01-01
  • 2010-09-10
  • 2021-07-30
  • 2017-12-03
相关资源
最近更新 更多