将应用与Google搜索结果和Chrome集成

时间:2014-07-29 08:02:39

标签: android android-intent google-chrome-app intentfilter google-search

官方维基百科应用程序以某种方式这样做,在我的应用程序中,我使用标准的intent-filter来监听导航到维基百科的网址。

有没有办法与Google搜索集成?是否有可用的API,或者仅适用于所选的API?

enter image description here

5 个答案:

答案 0 :(得分:7)

根据the documentation,您必须向要为其搜索的网页添加元标记,以显示指向应用的链接。例如,在您的屏幕截图的搜索结果中链接的Wikipedia页面在其源代码中包含以下链接:

<link rel="alternate" href="android-app://org.wikipedia/http/en.m.wikipedia.org/wiki/Apollo_17" />

我认为您无法通过其他方式添加指向您的应用的链接以搜索结果:搜索结果还建议用户安装他们尚未拥有的应用。赋予权力来决定向页面所有者以外的其他人推荐哪个应用程序显然是危险的。另一个强烈暗示是文档没有提到任何替代方案。

答案 1 :(得分:2)

正如我记得的,谷歌在今年谷歌I / O中宣布将实现这一目标的API将来自Android L

该公告可以在keynote video 33:45找到,虽然它没有说明如何运作

答案 2 :(得分:1)

您需要在网页的HTML标记中注释深层链接。您可以在每个网页的部分中执行此操作,方法是添加标记并将深层链接指定为备用URI。

查看维基百科的示例(我查找了您搜索过的内容的页面来源)

<meta name="generator" content="MediaWiki 1.24wmf15" />
<link rel="alternate"
  href="android-app://org.wikipedia/http/en.m.wikipedia.org/wiki/Apollo_17" />
<link rel="alternate" 
  type="application/x-wiki" title="Edit this page" href="/w/index.php?  title=Apollo_17&amp;action=edit" />

您需要在网页上添加以下代码:

<html>
<head>
    <link rel="alternate"
      href="android-app://com.example.android/example/gizmos" />
...
</head>
<body> ... </body>

现在要在应用程序中处理这个意图,当你搜索某些东西时,链接为你提供了在维基百科应用程序中打开的选项,如果你想支持两者,那么你需要修改你的android应用程序。

首先,您需要在Manifest中添加该方案。

以下是WikiPedia应用程序的工作原理。

WikiPedia应用程序添加了该方案,如下面的页面视图活动。

<activity android:name=".page.PageActivity" >
<intent-filter>
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
 <data
    android:scheme="http"
    android:host="*.wikipedia.org"
    android:pathPrefix="/wiki/" />
<data
    android:scheme="https"
    android:host="*.wikipedia.org"
    android:pathPrefix="/wiki/" />
</intent-filter>
</activity>

您需要为您的域做同样的事情,它会起作用。如果您需要确保您的应用程序也显示在深层链接中,请参阅link1link2

答案 3 :(得分:0)

顺便提一下,这称为应用程序索引,Google可以通过Android设置此方法

以下是如何为您的应用内容指定deep link

  1. 在Android清单文件中,为应从Google搜索结果中启动的活动添加一个或多个 <intent-filter> 元素。
  2. 添加 <action> 标记,用于指定 ACTION_VIEW 意图操作。
  3. 为活动接受的每种数据URI格式添加 <data> 标记。这是声明深层链接格式的主要机制。
  4. <category> BROWSABLE 意图类别添加 DEFAULT

    • BROWSABLE 是必需的,以便可以从Web浏览器执行。没有它,单击浏览器中的链接无法解析为您的应用程序,只有当前的Web浏览器才会响应该URL。
    • 如果您唯一的兴趣是从Google搜索结果中提供与您的应用的深层链接,则不需要
    • DEFAULT 。但是,如果您希望Android应用在用户点击指向您网站的任何其他网页的链接时进行响应,则需要 DEFAULT 类别。区别在于,Google搜索结果中使用的意图包含您应用的身份,因此意图明确指向您的应用作为收件人 - 您网站的其他链接不知道您的应用标识,因此 {{1 }} 类别声明您的应用可以接受隐含的意图。
  5. 实施例

    DEFAULT

    在此示例中,您的应用会响应深层链接,例如...

    意图过滤器的<activity android:name="com.example.android.GizmosActivity" android:label="@string/title_gizmos"> <intent-filter android:label="@string/filter_title_viewgizmos"> <action android:name="android.intent.action.VIEW" /> <!-- Accepts URIs that begin with "http://example.com/gizmos” --> <data android:scheme="http" android:host="example.com" android:pathPrefix="/gizmos" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> </intent-filter> </activity> 元素必须至少提供<data>属性。然后,您可以添加其他属性以进一步优化活动接受的URI类型

    可以找到更多信息here...

答案 4 :(得分:0)

Android引入应用链接api在Google搜索结果中整合原生应用内容。文档说:

Google's web crawling bot (Googlebot), which crawls and indexes web sites for the Google search engine, can also index content in your Android app. By opting in, you can allow Googlebot to crawl the content in the APK through the Google Play Store to index the app content. To indicate which app content you’d like Google to index, simply add link elements either to your existing Sitemap file or in the <head> element of each web page in your site, in the same way as you would for web pages.

The deep links that you share with Google Search must take this URI format:

android-app://<package_name>/<scheme>/<host_path>
The components that make up the URI format are:

package_name. Represents the package name for your APK as listed in the Google Play Developer Console.
scheme. The URI scheme that matches your intent filter.
host_path. Identifies the specific content within your application.
The following sections describe how to add a deep link URI to your Sitemap or web pages.

同样重要的是,如果用户手机上没有该应用,并且您仍希望在Google搜索结果中正确格式化内容,则需要使用元标记。 表示:

  

元标记是网站管理员提供搜索引擎的好方法   有关其网站的信息。元标记可用于提供   信息给各种客户,每个系统只处理   他们理解的元标记,忽略了其余部分。元标签是   添加到HTML页面的部分,通常看起来像   这样:

参阅 - https://support.google.com/webmasters/answer/79812?hl=en