从Android浏览器/ Chrome启动自定义Android应用程序

时间:2014-02-09 18:20:41

标签: android android-intent intentfilter

首先,我的问题与thisthisthis非常相似。我正在尝试实现的Android文档是here。我无法使用这些资源使用它,所以请不要将其标记为重复,因为它不是。

我有一个网站和一个Android应用程序。用户将能够扫描包含http://mywebsite.com/map/等链接的QR码。当用户尝试打开此链接时,我希望Android向他显示一个选择器对话框,他可以选择使用我的应用程序打开该链接。如果我的应用程序没有安装,它应该进入指定的网站。

我知道当用户导航到该地址时,Chrome会通过打开选择器对话框来实现此目的。例如,尝试下载Stack Exchange app并在Chrome中转到此问题。它会显示:

Screenshot of chooser dialog.

按照上述答案中的建议后,我在AndroidManifest.xml中添加了以下代码:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <action android:name="android.intent.action.SEND" />

    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />

    <data
        android:host="mywebsite.com"
        android:path="/map"
        android:scheme="http" />
    <data
        android:host="mywebsite.com"
        android:path="/animals"
        android:scheme="http" />
    <data
        android:host="mywebsite.com"
        android:path="/articles"
        android:scheme="http" />
</intent-filter>

另外,我尝试将android:mimeType =“text / plain”添加到数据中,但它没有帮助。

问题在于,当我转到http://mywebsite.com/maphttp://mywebsite.com/map/ Chrome时,只需打开网页而不显示选择器对话框。

我想提一下:

  • 在Android文档之后,我已将此代码添加到AndroidManifest.xml中的一个活动结构中。由于我不确定这是添加它的最佳位置,我还尝试将其添加到应用程序结构之外并直接在应用程序结构中添加但是它不起作用
  • 这是我实现的唯一代码。如果需要其他东西请告诉我。据我所知,只有在使用自定义模式时才需要在网页上添加href
  • 我不想使用自定义架构来实现此目的
  • 我正在使用运行Android 4.4.2(最新版)
  • 的Nexus 4进行开发

3 个答案:

答案 0 :(得分:61)

你需要像这样设置:

<intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="example.com"
            android:pathPrefix="/someresource/"
            android:scheme="http" />
        <data
            android:host="www.example.com"
            android:pathPrefix="/someresource/"
            android:scheme="http" />
    </intent-filter>

请注意,在您的情况下,您需要使用android:pathPrefix而不是android:path。

答案 1 :(得分:6)

为了确保您可以重置应用的偏好设置,以防您不小心将其设置为始终在Chrome中打开链接而不是显示选择器对话框。一旦“Always”用于打开匹配的uri,它将永远不会显示选择器。

其次,您可以根据需要在intent过滤器中包含尽可能多的数据元素,但不必重复信息。你可以这样做:

<data android:host="mywebsite.com"/>
<data android:scheme="http"/>
<data android:path="/map"/>
<data android:path="/animals"/>
<data android:path="/articles"/>

但请注意,对于路径,您可以使用通配符

<data android:path="/.*"/>

考虑添加额外的

<data android:host="www.mywebsite.com"/>

最后,您可能不想显示选择器对话框,而是直接打开特定的应用程序意图/活动。在您的网站上,如果您检测到Android用户代理,您可以通过以下方式创建链接URL:

<a href="intent://mywebsite.com/articles#Intent;package=com.myapp;scheme=http;end;"/>

有关详细信息,请参阅此处How do I open any app from my web browser (Chrome) in Android? What do I have to do with the A Href link?

请注意,使用此方法,如果未安装该应用,则系统会将用户带到指定应用包的Google Play商店。

如果您仍然遇到问题,请检查您的意图过滤器优先级。 http://developer.android.com/guide/topics/manifest/intent-filter-element.html

答案 2 :(得分:3)

在我的案例中,网站网址为: http://www.example.org/mobile/

将这些代码放入活动中的 AndroidManifest.xml

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="www.example.org"
                    android:pathPrefix="/mobile/"
                    android:scheme="http" />
            </intent-filter>

下面,

计划 - &gt;特定网站协议

<强>宿主 - &GT;带有 WWW

的精确网站网址

pathprefix - &gt;您网站的子路径(如果有)

现在,

您可以使用chrome / etc android browser'search box搜索 example 然后打开所选对话框.. !!