【问题标题】:Workaround for Chrome on Android not firing intent when links are clicked单击链接时,Android 上 Chrome 的解决方法不会触发意图
【发布时间】:2013-07-17 17:44:55
【问题描述】:

我正在开发一个 Android 应用程序来处理某些 http(s) 链接的意图。由于this bug/missing feature,它在大多数情况下都有效,但不适用于点击 Chrome 中的链接。简而言之,Chrome 不会广播这些点击的意图。 :/

这可能最终会得到解决,但与此同时,有人知道解决方法吗?

如果我自己控制链接,我可以使用自己的 HTTP 方案,例如myapp://...,或者我可以use JavaScript to fake a button click,两者都发送意图...但我不控制链接。

具体来说,我想处理像http://github.com/snarfed 这样的链接。我在AndroidManifest.xml 中的活动定义如下。它正确地接收来自其他应用的此类链接点击的意图广播,而不是来自 Chrome。

<activity android:name="MyApp" android:exported="true">
  <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="http" />
    <data android:scheme="https" />
    <data android:host="github.com" />
  </intent-filter>
</activity>

【问题讨论】:

    标签: android google-chrome


    【解决方案1】:

    您需要在每个数据点中包含方案、主机和路径前缀。

    相应清单示例:

    <activity
        android:name=".PhotostreamActivity"
        android:label="@string/application_name">
    
        <!-- ... -->            
    
        <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="http"
                  android:host="flickr.com"
                  android:pathPrefix="/photos/" />
            <data android:scheme="http"
                  android:host="www.flickr.com"
                  android:pathPrefix="/photos/" />
        </intent-filter>
    </activity>
    

    此示例取自 Romain Guy 放在一起的应用程序,并在此处的重复问题中引用:

    Intercepting links from the browser to open my Android app

    需要注意的几个警告,似乎你需要在意图映射生效之前在后台杀死 Chrome,如果你直接输入 url,应用程序将不允许使用它,只有当它是一个链接时Chrome 是否为应用提供操作。

    【讨论】:

    • 太棒了!我很高兴能试试这个。收到后我会回帖。
    • 它有效!很酷。我已经用测试后发现的一些细节对其进行了更新。我也不需要先杀死 chrome。您可能需要在旧版本上使用吗?
    • 看起来 chrome 有时会在不杀死并重新安装它的情况下获取新支持的 Intent,但并非总是如此。也许它会在全新的应用安装时捕获它们,但不会在重新安装时捕获它们。
    • 哇,终于!指定 pathPrefix 解决了我的问题。我必须指定android:pathPrefix="/"。很奇怪,因为这只是使用“http”方案时的要求,但如果使用自定义方案则不是。
    【解决方案2】:

    似乎只有在 Intent 过滤器具有路径、pathPrefix 或 pathPattern 时,Chrome 才会触发 Intent。 所以,为了过滤整个域,我通过添加android:pathPattern=".*" 解决了这个问题,如下所示:

    <intent-filter
        android:priority="999">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:host="mydomain.com"
            android:scheme="http"
            android:pathPattern=".*" />
        <data
            android:host="www.mydomain.com"
            android:scheme="http"
            android:pathPattern=".*" />
    </intent-filter>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-29
      相关资源
      最近更新 更多