什么是新生成的代码"这是自动生成的,用于实现App Indexing API."?

and*_*per 18 android google-api-client android-app-indexing

背景

我今天刚刚创建了一个新的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();
Run Code Online (Sandbox Code Playgroud)

和更多:

@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();
    }
Run Code Online (Sandbox Code Playgroud)

这被添加到清单中:

<!-- 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"/>
Run Code Online (Sandbox Code Playgroud)

问题

看看写的网站,我仍然不知道它是什么.

我想是如何使用它,但我不知道它是如何工作的.

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

问题

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

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

pfe*_*erg 16

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

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

要为您生成此代码,可以通过Alt + Enter 使用弹出意图列表,选择" 插入应用程序索引API代码 ":

在此输入图像描述

或者您可以通过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")
Run Code Online (Sandbox Code Playgroud)

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

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

这是一个例子,从最终用户的角度来看,在Live Nation应用程序中,假设他之前访问过左边的Suggest结果中显示的两个页面,它会是什么样子:

在此输入图像描述

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

这样可以为您的应用用户启用查询自动填充功能,以及更丰富的搜索结果,改进的搜索质量和增强的排名信号.

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

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

  • 你确定它没有自动插入吗?我从来没有要求AS插入此代码,但它在不同的项目中无论如何都做了三次. (3认同)
  • 那么它必须是通过键盘shorcut无意中激活的.有时其中一只猫跳到我的键盘上,也许他们按下正确的键.就像他们喜欢按ctrl-minus来使浏览器字符变得微不足道. (3认同)