美文网首页
Android intent-filter匹配规则

Android intent-filter匹配规则

作者: 木木禾木 | 来源:发表于2020-05-11 12:02 被阅读0次

Activity的过滤器 intent-filter 中有3个属性 actioncategorydata ,那么启动设置intent-filter有什么需要注意的呢?

1. 每个activity可以配置多个intent-filter
<activity android:name=".TestCustomActivity">

            <intent-filter>
                <!-- 系统预定义的action -->
                <action android:name="android.intent.action.VIEW" />
                <!-- 自定义的action -->
                <action android:name="com.test.action.CUSTOM" />

                <!-- 系统预定义的category -->
                <category android:name="android.intent.category.DEFAULT" />
                <!-- 自定义的category -->
                <category android:name="com.test.category.PAGE" />

                <!-- data属性 -->
                <data
                    android:host="www.123.com"
                    android:mimeType="custom/view"
                    android:path="/home/index"
                    android:port="8000"
                    android:scheme="custom" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <action android:name="com.test.action.QUICK" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.test.category.TAB" />

                <data
                    android:host="www.abc.com"
                    android:mimeType="test/tab"
                    android:path="/tab/three"
                    android:port="8888"
                    android:scheme="custom2" />
            </intent-filter>

        </activity>

如上代码:
a. 启动TestCustomActivity

val intent = Intent()
intent.setDataAndType(Uri.parse("custom2://www.abc.com:8888/tab/three"), "test/tab")
startActivity(intent)

val intent = Intent()
intent.setDataAndType(Uri.parse("custom://www.123.com:8000/home/index"), "custom/view")
startActivity(intent)

b. 因为data中设置了Uri 和 mimeType,通过 setDataAndType来设置数据,如下是不行的

                val intent = Intent()
                intent.data = Uri.parse("custom://www.123.com:8000/home/index")
                intent.type = "test/tab"
                startActivity(intent)

c. 因为设置了data,所以intent中不设置data也是不行的,如下所示

               val intent = Intent()
               intent.action = "com.test.action.CUSTOM"
               intent.addCategory("com.test.category.PAGE")
               startActivity(intent)

报错:No Activity found to handle Intent { act=com.test.action.CUSTOM cat=[com.test.category.PAGE] }

2. intent-filter中只设置category无法匹配,至少要有一个action
3. intent-filter中只设置data无法匹配,至少要有一个action
4. 多个action只需匹配其中一个即可启动activity
        <activity android:name=".TestCustomActivity">

            <intent-filter>
                <!-- 系统预定义的action -->
                <action android:name="android.intent.action.VIEW" />
                <!-- 自定义的action -->
                <action android:name="com.test.action.CUSTOM" />

                <!-- 系统预定义的category -->
                <category android:name="android.intent.category.DEFAULT" />
                <!-- 自定义的category -->
                <category android:name="com.test.category.PAGE" />

            </intent-filter>

        </activity>

a. 启动TestCustomActivity

                val intent = Intent()
                intent.action = "com.test.action.CUSTOM"
                intent.addCategory("com.test.category.PAGE")
                startActivity(intent)
5. 单靠category无法匹配,category是action的一种补充
6. 任何一个需要隐式启动的Activity都必须要有这项:<category android:name="android.intent.category.DEFAULT"/>

如下设置:

        <activity android:name=".TestCustomActivity">
            <intent-filter>
                <!-- 自定义的action -->
                <action android:name="com.test.action.CUSTOM" />
            </intent-filter>
        </activity>

如下启动是不可以的

                val intent = Intent()
                intent.action = "com.test.action.CUSTOM"
                startActivity(intent)

报错:No Activity found to handle Intent { act=com.test.action.CUSTOM }

7. 如果activity指定了<data>,那么启动activity必须设置intent的data

见1

8. 如果匹配到多个activity,会弹出选择框

如:

                val intent = Intent()
                intent.action = Intent.ACTION_VIEW
                startActivity(intent)
匹配到多个activity


相关文章

网友评论

      本文标题:Android intent-filter匹配规则

      本文链接:https://www.haomeiwen.com/subject/iiiynhtx.html