android - 如何使用 https 方案在 customtabs 上使用 Intent 过滤器?

标签 android intentfilter chrome-custom-tabs

我的应用程序打开了一个 customtabs 浏览器,当浏览器遇到某个 url 时,我想弹回打开我的应用程序。

我发现这个帖子有效 - Android - create a schema for my app that will open from a webpage link使用名为“myapp”的协议(protocol)

我更喜欢使用 http/https 协议(protocol)来触发我的应用程序,但由于某种原因它似乎不起作用。

具体来说,这是有效的:

       <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:scheme="myapp" />
       </intent-filter>

当浏览器重定向到“myapp://10.2.2.0:3001/cats”时,我的应用程序会重新打开,耶!

问题是当我尝试这个(或我尝试过的 100 种其他方法中的任何一种)时
            <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:scheme="https" android:host="10.0.2.2" />
                <data android:scheme="http" android:host="10.0.2.2" />
                <data android:pathPrefix="/cats" />
            </intent-filter>

当我的应用程序重定向到“http://10.2.2.0:3001/cats”时它不起作用,作为一个负面影响,它还会询问我在打开 customTabs 时我想做什么(使用谷歌浏览器,使用不同的应用程序),无论我选择什么, Intent 过滤器不会被触发。

使用 http/https Intent 过滤器是否有限制?我究竟做错了什么?

最佳答案

来自:Launch custom android application from android browser (阅读所有答案,可能有用)

截至 2014 年 1 月 28 日,以上所有答案都不适用于我的 CHROME

我的应用程序从 http://example.com/someresource/ 正确启动来自环聊、gmail 等应用程序的链接,但不是来自 chrome 浏览器中的链接。

要解决这个问题,以便它从 CHROME 正确启动,您必须像这样设置 Intent 过滤器

    <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>

注意 pathPrefix 元素

现在,只要用户请求 http://example.com/someresource/,您的应用就会出现在 Activity 选择器中通过单击谷歌搜索结果或任何其他网站中的链接从 chrome 浏览器中获取模式

更新

您应该对我从您的代码中看到的内容没有问题。只需尝试在不使用自定义方案的情况下解决您的问题。

阅读此答案:explaining why not to use a custom scheme

此外,如果您有多个操作,请检查是否将每个操作放在单独的 Intent 过滤器中,并且 Intent 过滤器的顺序对您的问题有任何影响。最后,您是否尝试过使用 Intent 方案?那也值得一试。

关于android - 如何使用 https 方案在 customtabs 上使用 Intent 过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58985170/

相关文章:

Android 将 GridView 与 OnScrollListener 结合使用

android - 如何让移动网页在所有设备上显示相同

android - 我们如何找到启动其他应用程序的信息?

android - GCM 如何处理带有 BroadCastReceiver 的应用程序

android - 使用 Chrome 自定义标签发布数据

android - 如何在android中解析复杂的JSON文件

Android ListView 的麻烦

android - 设置 Activity 的 Intent 过滤器

android - WebView 与 Chrome 自定义选项卡

android - Chrome 自定义标签页重定向到 Android 应用程序将关闭该应用程序