How can I have my app appear in the intent chooser

2020-06-12 04:43发布

问题:

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?

回答1:

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>


回答2:

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.



回答3:

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>