Android 新生成的代码是什么;这是为实现应用程序索引API而自动生成的;? 背景
我今天刚刚创建了一个新的POC(关于活动转换,但这不是主题),我注意到在主activity的“onCreate”方法中写了一行新行:Android 新生成的代码是什么;这是为实现应用程序索引API而自动生成的;? 背景,android,google-api-client,android-app-indexing,Android,Google Api Client,Android App Indexing,我今天刚刚创建了一个新的POC(关于活动转换,但这不是主题),我注意到在主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.
// 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"/>
问题
看着写的网站,我还是不明白它是什么
我想这就是如何使用它,但我不明白它是如何工作的
另外,奇怪的是,我创建的任何其他新项目都没有显示这一行新代码
问题
根据我在网站上读到和看到的内容,我的猜测是,这只用于可以执行某种搜索的应用程序,这样谷歌就可以向用户显示以前的查询和更快的结果 你是对的:Android Studio会自动为你创建代码,以帮助实现应用程序索引API 但是,它不是通过简单地向应用程序添加新活动创建的。您需要明确要求Android Studio创建此代码。然后,您需要使用活动的详细信息更新它:操作类型、标题、深度链接、相应的网页(如果存在) 要为您生成此代码,您可以使用弹出的意图列表通过Alt+Enter,选择“插入应用程序索引API代码””: 或者您可以使用弹出代码生成列表,通过Alt+Insert,选择“应用程序索引API代码””: 以下是相关的Google开发者文档: 实际上,您只需要调整以下四个部分:
// 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网页标题中的
标记中一样
一旦实施,终端用户查看的任何活动都将向Android操作系统报告此深度链接。然后,当用户在Google Quicksearch框中键入查询时,它将出现在“建议自动完成结果”中。如果用户查询按关键字匹配您的标题,则您提供的应用图标和标题将显示在建议结果中
下面是一个例子,从最终用户的角度来看,如果他之前访问过左侧“建议结果”中显示的两个页面,那么在Live Nation应用程序中,它会是什么样子:
此外,通过实施应用程序索引API,您将在搜索结果中获得排名提升,如您在原始问题中提供的链接所述:
这将为您的应用程序用户以及
更丰富的搜索结果、更高的搜索质量和增强的排名
信号。
最后,您可能会对这个代码实验室感兴趣,因为它是一个额外的资源:
如果这有帮助,您可以通过以下步骤禁用该选项:
Settings > Intentions > Android > Insert App Indexing API code
然后取消选中该选项。您可以通过在以下位置取消选中该选项来禁用该选项:
Settings > Editor > Intentions > Android > Insert App Indexing API code
要找到它,请在文件>设置窗口的搜索框中键入“api代码”。
它就在我安装的Android Studio 2.2.3中,很有趣,但我还是不明白:它的用途是什么?它对用户有什么作用?它是如何工作的?您确定它不是自动插入的吗?我从来没有要求过插入这段代码,但它在不同的项目中插入了三次。@Christine,是的,如果没有用户的任何触发,这段代码是不会自动生成的。您是否可能正在使用键盘快捷键?那么,这一定是通过键盘快捷键无意中激活的。有时,其中一只猫会跳到我的键盘上,也许他们按对了键。就像他们喜欢按ctrl键减号使浏览器字符变得很小一样。@pferg我可以不用担心就删除它吗?好像是我在不知情的情况下触发了它。这只是我的一个意外,因为它是默认的自动完成选项。。。现在我的构建失败了。如果我将其从Gradle文件中删除,并且只触摸AndroidStudio外部磁盘上的文件,然后告诉它同步Gradle文件,那么额外的代码就会神奇地返回。找不到最后一部分-“插入应用程序索引API代码”-在下面是否有任何特定的搜索部分我看不到。