首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >什么是新生成的代码“这是自动生成以实现应用程序索引API。”?

什么是新生成的代码“这是自动生成以实现应用程序索引API。”?
EN

Stack Overflow用户
提问于 2015-12-27 22:27:32
回答 3查看 12.3K关注 0票数 18

背景

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

代码语言:javascript
复制
    // 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();

以及更多内容:

代码语言:javascript
复制
@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();
    }

这是添加到清单中的:

代码语言:javascript
复制
<!-- 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"/>

问题所在

看着写好的网站,我还是不明白它是什么。

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

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

问题

  1. 这是什么?它做了什么?
  2. 我应该用它做什么?
  3. 对它有什么自定义吗?有什么建议吗?
  4. 在哪些情况下会生成这行代码?我没有注意到它是如何以及何时被创建的--

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-01-06 06:41:00

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

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

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

或者您也可以使用弹出代码生成列表 by Alt + Insert,选择“应用索引接口代码”:

以下是Google开发人员的相关文档:

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

这里只有四个部分你需要调整:

代码语言:javascript
复制
// 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>标签中一样。

一旦实现,您的最终用户查看的任何活动都会将此深度链接报告给Android操作系统。然后,当用户在Google Quicksearch Box中键入查询时,它将在建议自动完成结果中可用。如果用户查询通过关键字匹配您的标题,您的应用程序图标和您提供的标题将显示在建议结果中。

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

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

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

最后,您可能会对此代码实验室感兴趣,将其作为额外的资源:

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

票数 16
EN

Stack Overflow用户

发布于 2016-11-12 06:09:04

如果这有帮助,您可以通过转到以下位置禁用该选项:

代码语言:javascript
复制
Settings > Intentions > Android > Insert App Indexing API code

并取消选中它。

票数 3
EN

Stack Overflow用户

发布于 2017-07-01 11:05:02

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

代码语言:javascript
复制
Settings > Editor > Intentions > Android > Insert App Indexing API code

要找到它,请在File>Settings窗口的搜索框中键入"api code“。它在我安装的Android Studio 2.2.3中

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34481370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档