11

I am developing an app that can extract information from certain web pages. The idea is that when the user is within a specific url path in the browser and press the share button, my app will show up in the list of receiver apps.

I can do that easily by adding this to the manifest:

<intent-filter android:label="@string/app_name" >
    <action android:name="android.intent.action.SEND" />
    <data android:mimeType="text/plain" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

However, this will make my app appear on the list on all urls, also all those where it will have nothing to do. Instead I would like the app appear in the chooser only from these urls:

www.example.com/foo/bla.html

www.example.com/foo/bar/blabla.html

But not from these:

www.example.com

www.foobar.com

etc. Ie. only from within a certain path on a certain host. Also note that I do not want my app to be launched when the user clicks on links matching the criteria. It should only be invoked from the share menu.

So my question is: How can I limit my app to show up in the intent choose only for certain urls?

Ziem
  • 6,579
  • 8
  • 53
  • 86
marlar
  • 3,858
  • 6
  • 37
  • 60
  • do you have the possibility to make subdomains on that URLs? like bla.example.com? – A.S. Mar 31 '14 at 09:33
  • No, that is not an option. – marlar Mar 31 '14 at 10:34
  • 2
    You're entitled to vote however you want, but I don't see the logic in downvoting every answer you get. It discourages anyone else from answering and even if you don't like the facts, CommonsWares answer is informative and correct and Dharanis answer is the closest reasonable solution – Nick Cardoso Mar 01 '16 at 09:10
  • Why do you say I am downvoting every answer I get? This is probably the first answer to one of my own questions I have ever downvoted. And I didn't downvote the current answer. The one I downvoted was rude, but CommonsWare have modified it in the meantime and made it much better. So I have removed the downvote. I want to add that I normally enjoy CommonsWare's answers but frankly that answer was rude. But not anymore. – marlar Mar 05 '16 at 13:59

3 Answers3

4

Add the following filter to the destination Activity .. 1. host your site name. 2. scheme scheme of your site http or https. 3. path for the file path your app should display.

<intent-filter>
             <data
                android:host="www.example.com"
                android:scheme="http"
                android:path="/foo/bla.html"
                />
            <action android:name="android.intent.action.VIEW" />
             <category android:name="android.intent.category.DEFAULT" >
            </category>
            <category android:name="android.intent.category.BROWSABLE" >
            </category>
</intent-filter>
Dharani Kumar
  • 1,981
  • 3
  • 13
  • 18
  • This makes my app respond to clicks matching urls which is not what I want. I don't want to "hijack" those urls. I only need my app to show up in the list that appears when the user presses the share button. But only when browsing those specific urls. – marlar Mar 31 '14 at 10:58
  • why don't you try along with above code...?...refer http://developer.android.com/guide/topics/manifest/grant-uri-permission-element.html.. – Dharani Kumar Mar 31 '14 at 11:50
3

How can I limit my app to show up in the intent choose only for certain urls?

You can't. ACTION_SEND has no notion of "only from certain URLs", or "only from certain WhatsApp users", or "only from left-handed Alaskan pipe-welders".

This is not significantly different than any other activity. An <intent-filter> only controls what the Intent is being delivered to. Nothing controls what the Intent is delivered from, with the exception of security-related items (permissions, whether or not the component is exported, etc.).

So, with ACTION_VIEW, the URL is something being delivered to another activity ("I wish to view this URL"). This works because the URL is turned into the Uri of the Intent, and that can be filtered upon via <data> elements in the <intent-filter>.

But ACTION_SEND doesn't have to have a URL, and even if there is one, it is merely payload in the form of an extra. It is not something that can be filtered upon.

Hence, what you want is not supported.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
-1

Use pathPrefix

    <intent-filter>
         <data
            android:host="www.example.com"
            android:scheme="http"
            android:pathPrefix="/foo/"
            />
        <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" >
        </category>
            <category android:name="android.intent.category.BROWSABLE" >
        </category>
    </intent-filter>
DDsix
  • 1,966
  • 1
  • 18
  • 23