How many scheme & host tags can come under intent-filter in android manifest

China☆狼群 提交于 2019-12-03 12:40:44

问题


Need more info regarding intent-filter tag specified in manifest. I am aware that we can specify data in two forms:

<intent-filter>
     <data android:host="com.myHost" android:scheme="content"/>
</intent-filter>

AND :

<intent-filter>
     <data android:scheme="content"/>
     <data android:host="com.myHost"/>
</intent-filter>

But I wish to know can several combinations exist, like

<intent-filter>
         <data android:host="com.myHost" android:scheme="content"/>
         <data android:scheme="content"/>
         <data android:host="com.myHost1"/>
</intent-filter>

OR:

<intent-filter>
         <data android:host="com.myHost" android:scheme="content"/>
         <data android:scheme="content"/>
         <data android:host="com.myHost1"/>
         <data android:scheme="content"/>
         <data android:host="com.myHost2"/>
</intent-filter>

In the last case, I wish to know firstly if this can exist & how is it decided that which host to be used for which scheme, as the data tags containing scheme & host can occur in any order.

Please help.


回答1:


I am aware that we can specify data in two forms

Do not use content for a scheme, unless you truly mean that you are creating an activity in support of a ContentProvider.

But I wish to know can several combinations exist

If your filter has just one attribute for <data>, you definitely can have different values, such as this from the Contacts app:

    <activity
        android:name=".activities.ShowOrCreateActivity"
        android:theme="@android:style/Theme.Translucent.NoTitleBar">

        <intent-filter>
            <action android:name="com.android.contacts.action.SHOW_OR_CREATE_CONTACT" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="mailto" />
            <data android:scheme="tel" />
        </intent-filter>
    </activity>

Also, one component can have several <intent-filter> elements, each of which is logically OR'd with the others (any Intent matching any filter is a match for the component). So for more complex scenarios, where you have 2+ attributes per <data> element, I would be inclined to put those in separate <intent-filter> elements.

how is it decided that which host to be used for which scheme

Any match is considered good. You would examine the Intent yourself to learn more about what it contains.




回答2:


Complementing @CommonsWare answer, it appears that you cannot use two <data> tags if you are not so specific.

A) In one of my apps I can have:

<data android:scheme="myAppScheme1"/>

B) And

<data android:scheme="myAppScheme2" android:host="host2"/>

C) But the following will ignore the first tag (using myAppScheme1://whatever URI won't work):

<data android:scheme="myAppScheme1"/>
<data android:scheme="myAppScheme2" android:host="host2"/>

D) However if I complement the first scheme it will work for both URIS:

<data android:scheme="myAppScheme1" android:host="host1"/>
<data android:scheme="myAppScheme2" android:host="host2"/>

Probably if you really need the case C), you'll better create two intent-filters



来源:https://stackoverflow.com/questions/16583377/how-many-scheme-host-tags-can-come-under-intent-filter-in-android-manifest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!