Android Studio官方文档之添加URL和App索引支持

本文由nyk翻译,jkYishon审校。

Android Studio可以帮你在App中添加对URLs,app索引,搜索功能的支持。这些功能可以帮你推动更多的流量到你的App、发现App中最被常用的内容,使用户更容易发现已安装App中内容,并以此来吸引新用户。

典型的工作流程

用Android Studio添加对URL支持,App索引,搜索功能到你的App中:

  1. 添加Intent过滤器和处理传入Intent的代码
  2. 关联网站和App
  3. 添加App Indexing API代码

Intent过滤器和App Indexing API是实现URL支持和索引的方式,但也有其他的可行方法。更过信息请查看替代Android索引方法。

支持URLs的Intent过滤器

Android Studio可以在清单中创建基本的Intent过滤器,你可以在其中定义URLs。在Activity中写Java代码来操作Intent。这个实现可以让用户通过点击一个URL来直接打开指定的App。用户可以在浏览器的google.com、Google Search App、Google Now On Tap中看到这个URLs。

和URLs关联的网站

在为你的应用设置了URL支持后,你可以使用Google Search Console和Google Play Developer Console把你的网站和App关联。在这之后,Google会通过你Intent过滤器中的URLs索引到你的App,并且开始将它们包含到搜索结果中。另外,你可以选择性的将App的内容屏蔽在Google搜索之外。在你将网站和App关联后,像Now On Tap的功能和提升搜索结果显示(相包含你的App图标)就会可用。

在Android 6.0(API 23)及更高版本中,你可以为URL添加默认的处理器和验证,以替代把你的网站和App相关联。

Chrome显示的google.com服务的搜索结果的URL对所有登录和未登录的用户都是可以访问的。对于Google Search App,用户必须登录才能看到搜索结果的URL。

添加索引API到Activity中

接下来,如果你想你的App支持搜索功能,比如自动填充,你需要添加App Indexing API代码到你Activity中。Android Studio可以在Activity添加构架代码,你可以优化它以支持App索引。App Indexing API可以能使你App索引,即使在GoogleBot无法得到你App内容的情况下。

测试URL支持和App Indexing API

Android Studio可以帮你测试以下功能:

  • URL支持测试帮你验证指定的URL是否可以启动一个App
  • logcat显示器帮你测试Activity中的App Indexing API调用情况
  • Android Lint工具可以对URL支持和App Indexing API的一些问题发出警告。这些警告和错误显示在代码编辑器和Lint inspection结果中
  • Google App Indexing测试来检查Google能否通过爬虫你的App页面或App Indexing API索引到你的URL

添加对URL支持和Google搜索的Intent过滤器

使用Android Studio添加一个定义URL的Intent过滤器。

  1. 在工程窗口的Android视图中,双击打开AndroidManifest.xml文件
  2. 按以下方式中的一种插入Intent过滤器:

    • 点击activity元素的左侧,会出现灯泡,点击灯泡选择Create URL
    • 右击元素activity,选择Generate>URL
    • 把你的光标放在一个Activity中,选择Code > Generate>URL

    代码编辑器使用Intention Action和生成代码机制来添加主要代码。

    代码编辑器会生成与以下代码相似的Intent过滤器

    <!-- ATTENTION: This intent was auto-generated. Follow instructions at
    https://g.co/AppIndexing/AndroidStudio to publish your URLs. -->
    <intent-filter><action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT" /><category android:name="android.intent.category.BROWSABLE" /><!-- ATTENTION: This data URL was auto-generated.We recommend that you use the HTTP scheme.TODO: Change the host or pathPrefix as necessary. --><dataandroid:host="www.example.com"android:pathPrefix="/gizmos"android:scheme="http" />
    </intent-filter>
    
  3. 修改

添加App Indexing API主代码到Activity

在添加了URL支持到你的App之后,你需要添加App Indexing API代码到Activity中以支持附加的搜索功能

添加App Indexing API到Activity中:

  1. Project窗口的Android视图中,双击Activity文件进入编辑界面
  2. 通过以下方式之一插入框架代码:

    • 在Activity定义中,单击Java代码,出现灯泡图标,点击灯泡选择Insert App Indexing API Code
    • 在Activity编辑界面,右击选择Generate > App Indexing API Code
    • 光标停留在Activity编辑界面,选择Code > Generate > App Indexing API Code

    代码编辑器使用Intention Action和生成代码机制来添加架构代码。

    如果你看不到App Indexing API Code的菜单选项,确保你的光标在Activity内,且按App Indexing API的方法检查你的代码。代码编辑器会依据以下情形之一来插入架构代码到Activity中:

    • 如果Activity中没有onStart()方法,或者onStart()没有包含AppIndexApi.start()AppIndexApi.view()调用
    • 如果Activity中没有onStop()方法,或者onStop()没有包含AppIndexApi.end()AppIndexApi.end()调用

    代码编辑器插入的Java代码与以下代码相似:

     /*** ATTENTION: This was auto-generated to implement the App Indexing API.* See https://g.co/AppIndexing/AndroidStudio for more information.*/private GoogleApiClient client;// ATTENTION: This was auto-generated to implement the App Indexing API.// See https://g.co/AppIndexing/AndroidStudio for more information.client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();@Overridepublic void onStart() {super.onStart();// ATTENTION: This was auto-generated to implement the App Indexing API.// See https://g.co/AppIndexing/AndroidStudio for more information.client.connect();Action viewAction = Action.newAction(Action.TYPE_VIEW, // TODO: choose an action type."Main 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://www.example.com/gizmos"),// TODO: Make sure this auto-generated app URL is correct.Uri.parse("android-app://com.example/http/www.example.com/gizmos"));AppIndex.AppIndexApi.start(client, viewAction);}@Overridepublic 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."Main 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://www.example.com/gizmos"),// TODO: Make sure this auto-generated app URL is correct.Uri.parse("android-app://com.example/http/www.example.com/gizmos"));AppIndex.AppIndexApi.end(client, viewAction);client.disconnect();}}
    

    关于App Indexing API方法的更多信息,请查看Android API for App Indexing,关于Action类型的更多信息,请查看Action类常量总结。

    如果你的App没有准备配置Google Play服务的App Indexing API,代码编辑器也会修改并包含在build.gradle和AndroidManifest.xml文件。如果你的App依赖Google Play服务,且版本低于8.1,那你的应该升级到8.1版本。更多信息请查看Google Play服务和设置Google Play服务。

  3. 按需优化架构代码

    请仔细查看注释会帮你找到需要修改的地方,例如设置标题和URL,更多信息请查看添加App Indexing API。

  4. 用logcat显示器验证你的App Indexing代码是否运行。

为了运行和调试App Indexing API代码,你可以使用Android Studio以下功能:

  • 检查logcat的监控信息。
  • 启用Android Lint以下功能:Missing support for Google App Indexing API
  • 测试Google App Indexing

另外,你可以在Google Search Console中预览你的APK

测试URL

当你用Android Studio运行App时,你可以指定一个URL来启动且测试。

要启动于Android Studio中的URL:

  1. 在Android Studio中打开Android视图
  2. 选中一个工程,选run > Edit Configurations
  3. Run/Debug Configurations对话框中,在Android Application下面,选择你要运行的应用
  4. 选择General标签
  5. Launch中,选择URL
  6. URL中,点击…从列表中选择一个URL
    或者你输入一个URL,例如,http://example.com/gizmos
  7. 点击OK
  8. 选择Run > Run appDebug app
  9. 如果Select Deployment Target对话框出现,选择一个连接设备或模拟设备

    如果链接是成功的,App会在设备或模拟设备启动,并且在指定的activity中显示App,否则的话,错误信息会显示在Run窗口

关于设置App的运行信息,请查看编译运行App

当App运行时,你可以查看App Indexing API的日志

在logcat中查看App Indexing API的信息

logcat Monitor可以显示应用的索引的日志信息以确定你应用的App Indexing API代码是否推送正确的数据到云端。例如,你可以查验App的标题和URL。logcat Monitor是Android Monitor的一部分。

在logcat Monitor中查看App Indexing API日志信息:

  1. 在Android Studio中运行你的App,它会启动URL
  2. 显示Android Monitor点击logcat标签
  3. 设置log的级别为Verbose
  4. 在过滤器菜单中,选择No Filters
  5. 用字符串“appindex”在log中搜索

    App的索引日志信息就会显现,如果没有,请检查以下项目:

    • 设备或模拟器上是否安装了 Google Play服务?可以通过检查设备的设置得知
    • 设备或模拟器上的Google Play服务的版本是否低于build.gradle指定的版本?如果是的话,那可能版本已经太,必须升级到更高的版本。

    更多信息请查看下载Google Play服务和设置Google Play服务。

  6. 访问App界面启动App Indexing API调用

配置Lint

你可以使用Android Studio内建的Lint工具来检查定义在清单文件中的URLs和已在Activity中实现的App Indexing API代码是否有效。

你可以通过两种方式看到URL和App Indexing的警告和错误信息。

  • 代码编辑器中弹出的文本。当Lint发现问题时,他会用黄色高亮有问题代码,或者在更严重代码下标红
  • 选择Analyze > Inspect Code ,弹出的Lint的Inspection Results窗口中

用默认的Lint来检查URLs和the App Indexing API:

  1. 在Android Studio中打开你的Android视图
  2. 选择File > Other Settings > Default Settings
  3. Default Preferences对话框中,选择Editor > Inspections
  4. Profile中,选择DefaultProject Default来决定该设置的适用范围是整个Android Studio或本工程
  5. 展开Android Lint的目录,并根据需要更改Lint的设置:

    • Missing support for Google App Indexing - 如果应用没有执行被Google Search使用的URLs会报告警告,默认是选中的
    • Missing support for Google App Indexing API:当应用没有实现App Indexing API时会报告。默认不选中
    • URL not supported by app for Google App Indexing:报告清单文件中的URL错误,默认选中

    例如,以下是第一种设置中的Lint警告:

  6. 点击OK

执行Inspection Results窗口中一系列的Lint检查:

  1. 在Android Studio中打开工程的Android视图,选择你要测试的工程的一部分
  2. 选择Analyze > Inspect Code
  3. Specify Inspection Scope对话框中,设置inspection scope和profile

    inspection scope指定了你想分析的文件,profile指定你想执行的Lint检查

  4. 如果你想改Lint的设置,点击Inspections对话框中的…。在Inspections对话框中,选择Manage定义一个新的配置文件,指定Lint设置,点击 OK

    Inspections对话框中,你可以搜索“app indexing”来找到URL和App Indexing API的Lint检查。注意在Inspections 对话框中改变配置的Lint设置不会改变默认设置,就像之前描述的那样。但是,它会改变配置文件的设置。

  5. 点击OK

    结果显示在Inspection Results窗口中

执行Google App Indexing测试

你可以用Google App Indexing Test来检查Google能否通过爬虫你的应用页面或使用App Indexing API来索引到URL。Google可以索引到你的URL如果你的应用至少支持以下情形的一种:

  • URL指定的页面可以由Googlebot打开或爬虫
  • 你的应用通过 App Indexing API发送了正确的数据

指定Google App Indexing Test:

  1. 在应用中添加URL或App Indexing API
  2. 在Android Studio中打开你的应用,选择 Tools > Android > Google App Indexing Test
  3. Google App Indexing Test 对话框中,设置Module, URLLanguage
  4. 如果你看到需要登录Google Cloud Platform帐号的信息的话,你需要登录
  5. Google App Indexing Test对话框中,点击OK

    Android Studio会编译APK并且开始测试,需要几分钟才能完成,结果会显示在代码编辑器的新标签页中。

    如果应用预览在屏幕右边,显示的和你测试的URL相关的界面,那么Googlebot就能找到该URL。

  6. 修改测试发现的问题,并且按需要重复测试

以下是一些常见的错误和警告提示:

警告或错误 描述
Error: Google cannot index this page. 你的应用不能被Googlebot爬虫或无法使用App Indexing API,所以Google无法索引到的你的App
Warning: The App URL you sent by using the App Indexing API doesn’t match the URL opened. 当调用App Indexing API时,在App中指定的URL必须和打开的URL相匹配
Warning: Google cannot index this page using the App Indexing API because the title is empty. 当调用 App Indexing API时,标题不能为空
Warning: Google can index this page using Googlebot crawling but identified blocked resources. App参考了其他资源,其中一些被阻塞或暂时不可用。如果不是关键资源,可能无关紧要。检查右边预览到的内容是否正确显示。要解决此问题,要确保资源不被 robots.txt 拦截
Warning: Google cannot index this page using the App Indexing API. 你的应用无法使用App Indexing API,推荐添加 App Indexing API到你的App中

Android Studio官方文档之添加URL和App索引支持相关推荐

  1. Android Studio官方文档之使用Image Asset Studio添加位图图像

    Android Studio官方文档之使用Image Asset Studio添加位图图像 本文由nyk翻译,jkYihson审校. 前言 Android Studio中有一个工具叫Image Ass ...

  2. 【开源项目推荐】Android Jetpack 官方文档 中文翻译

    Jetpack 是 Android 软件组件的集合,使您可以更轻松地开发出色的 Android 应用.这些组件可帮助您遵循最佳做法.让您摆脱编写样板代码的工作并简化复杂任务,以便您将精力集中放在所需的 ...

  3. Android HIDL 官方文档(七,END)—— 网络堆栈配置工具(Network Stack Configuration Tools)

    网络工具封装 网络工具封装过滤器 1 供应商接口与链 2 允许使用的命令 21 ip 22 iptables ip6tables 23 ndc 24 tc 对应的官方文档地址:HIDL(General ...

  4. android mvvm官方文档,MVVM: 这是一个android MVVM 框架,基于谷歌dataBinding技术实现

    MVVM 这是一个android MVVM 框架,基于谷歌dataBinding技术实现.dataBinding 实现的 V 和 VM的关联:使用IOC架构实现了 M 和 V的关联. 框架具有以下功能 ...

  5. Android 重读官方文档 1

    刚开始学习安卓的时候,看的最多的就是官网了. 这几天可能比较空,所以想再看看,温故知新,希望有所收获. 多入口 Android 不同于以往的单一入口程序,例如 C++ 或者是 Java,都只有一个 m ...

  6. Android Design 官方文档离线版(英文)!

    将官网的内容编辑成PDF,方便离线阅读,与大家分享! 下载地址:http://115.com/file/e7k1yr82#Download http://pan.baidu.com/netdisk/s ...

  7. android findbugs官方文档,Android代码质量 FindBugs 规则

    FindBugs 规则 FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如Nu ...

  8. ExoPlayer详解(官方文档-入门)

    目录 ExoPlayer详解系列文章-入门 一.前言 二.优缺点比较 三.概述 ExoPlayer详解--入门(官方文档) 添加ExoPlayer作为依赖项 1.添加依赖 2.添加ExoPlayer模 ...

  9. GoPro接入 - 根据GoPro官方文档/Demo,实现对GoPro的控制和预览

    1. 背景 一个需求 : 我们需要自己去接入GoPro,在自己的App上去控制GoPro. GoPro接入文档 : Open GoPro 文档 GoPro的开放API网上相关的资料较少,所以只能拿着G ...

最新文章

  1. Keras—ModelCheckpoint
  2. JavaWeb:AJAX
  3. python 删除第三方库_python 安装移动复制第三方库操作
  4. warning: implicit declaration of function ‘sleep’(添加头文件: #include <unistd.h>)
  5. Java实现字符串反转的四种方式代码示例
  6. Rows Over Window与Range Over Window的区别
  7. python psycopg2使用_python 操作数据库:psycopg2的使用
  8. 【数学笑话】数学家与灯神
  9. 获取当前节点之后的同级节点_04面试常问:分库分表之后,id 主键如何处理?...
  10. python中缀表达式_中缀表达式变后缀表达式、后缀表达式(逆波兰)求值(python版本)...
  11. oracle 百分位数,Oracle分析函数PERCENTILE_CONT,percentile函数
  12. IDEA 导入 SSM项目
  13. rsync 同步文件
  14. 不要迷信微服务,微服务就是个传说
  15. Android 集成facebook 登录和分享
  16. Android studio显示百度地图及闪退问题的解决
  17. 情人节有哪些礼物可以送给男朋友的,情人节送礼推荐
  18. 洛谷P1217 [USACO1.5]回文质数 Prime Palindromes
  19. 阿里面试:分析为什么B+树更适合作为索引的结构以及索引原理
  20. 理想照进现实:大量AI项目未见收益,产业方渐归冷静

热门文章

  1. 人类已经无法阻止苹果了——吐槽PC厂商
  2. (31)2021-01-20(JSON字符串和本地存储)
  3. 产品销售份额数据统计流程图模板分享
  4. 有房没房,日子过的都是心态
  5. 贵州贵安新区试点大数据精准扶贫运营管理
  6. 计算机网络研究进展,计算机网络信息空间(Cyberspace)的人文地理学的研究进展和展望.pdf...
  7. 一张照片就能攻破人脸识别系统,人脸识别安全性亟需提高
  8. 能净化空气的PC电源,配置也很不错,艾湃电竞AP-550Ti体验
  9. 机电团队怎么使用软件系统快速实施 部署
  10. 个人电脑网站的创建与发布