android - 新生成的代码 "This was auto-generated to implement the App Indexing API."是什么?

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

背景

我今天刚刚创建了一个新的 POC(关于 Activity 转换,但这不是主题),我注意到主 Activity 的“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();
    }

这已添加到 list 中:

<!-- 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. 我应该如何处理它?<​​/li>
  3. 是否有任何自定义?有什么建议吗?
  4. 在哪些情况下会生成这行代码?我没有注意到它是如何以及何时创建的......

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

最佳答案

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

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

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

enter image description here

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

enter image description here

这是相关的 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")

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

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

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

enter image description here

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

This enables query autocompletions for your app users, as well as richer search results, improved Search quality, and enhanced ranking signals.

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

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

关于android - 新生成的代码 "This was auto-generated to implement the App Indexing API."是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34481370/

相关文章:

android - 拖动后如何将布局恢复到其在android中的原始位置

java - proguard 和 jdom 错误编译 android APK

android - 如何从谷歌搜索网站结果中推广/打开应用程序?

android - App Indexing "Open in App"不起作用

空对象引用上的 java.lang.String com.parse.ParseFile.getUrl()'

javascript - 使用 WebView 的 Rtmp 视频流

java - 如何模拟 Google API AndroidPublisher 请求

oauth-2.0 - 通过 JS 客户端库为 Drive 使用授权时,是否可以正确选择任何可用的 Google 帐户来使用?

gradle - Google API客户端链接了错误版本的jackson-core

android - 有没有人让 Firebase 应用索引工作?