6

I'm working on an Android app that handles intents for certain http(s) links. It works in most cases, but not for clicks on links in Chrome, due to this bug/missing feature. In short, Chrome doesn't broadcast an intent for those clicks. :/

That may be fixed eventually, but in the meantime, does anyone know a workaround?

If I controlled the links themselves, I could use my own HTTP scheme, e.g. myapp://..., or I could use JavaScript to fake a button click, both of which send the intent...but I don't control the links.

Specifically, I'd like to handle links like http://github.com/snarfed. My activity's definition in AndroidManifest.xml is below. It correctly receives intent broadcasts for these kinds of link clicks from other apps, just not from 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>
Community
  • 1
  • 1
ryan
  • 2,687
  • 1
  • 29
  • 38

2 Answers2

11

You need to include the scheme, host and pathPrefix in each data point.

Example of the appropriate manifest:

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

This example is taken from an application Romain Guy put together and referenced in a duplicate question here:

Intercepting links from the browser to open my Android app

Couple of caveats to note, it seems that you need to kill Chrome in the background before the intent mapping takes effect AND if you type the url directly, the application won't be allowed to consume it, only when it's a link does Chrome offer the action up to apps.

Community
  • 1
  • 1
Matt Gaunt
  • 9,434
  • 3
  • 36
  • 57
  • awesome! i'm excited to try this. i'll post back once i have. – ryan Jul 25 '13 at 00:00
  • it works! very cool. i've updated it with some details i found after testing. i also didn't need to kill chrome first. do you maybe need to on older versions? – ryan Jul 25 '13 at 03:01
  • looks like chrome sometimes picks up newly supported intents like these without killing and reinstalling it, but not always. maybe it catches them on a fresh app install but not a reinstall. – ryan Jul 25 '13 at 04:30
  • Wow, finally! Specifying the **pathPrefix** solved my issue. I had to specify `android:pathPrefix="/"`. Very strange, as this was only a requirement when using an "http" scheme, but not if using a custom one. – cprcrack Sep 19 '14 at 17:35
4

It seems that Chrome only fires intents if the intent filter has a path, pathPrefix or pathPattern. So, to filter the whole domain, I solved this problem by adding android:pathPattern=".*", like this :

<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>
Neoseifer
  • 41
  • 3