Android-display Intent and implicit Intent and IntentFilter matching rules

One: Display Intent I believe that everyone has been very skilled in displaying Intent, so
I won't elaborate on it here.

 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,FirstActivity.class);
                startActivity(intent);
            }
        });

We first talk about the IntentFilter matching rules and then talk about the implicit Intent, it will be easier to understand.
Two: IntentFilter matching rules
The filtering information in the IntentFilter includes action, category, and data.
If you want to match the filtering list, you must match the action, category, and data information in the filtering list at the same time, otherwise the matching fails. An Activity can have multiple groups of IntentFilters, as long as the Intent matches one of them, and a group of IntentFilters can also have multiple actions, category, data
1. The
action matching rule action is a string, which is predefined by the system, and we can also arrogant.
Matching rules: A group of IntentFilters can have multiple actions. As long as the action in the Intent matches any one of the actions in the IntentFilter, it will succeed. A successful match means that the string is the same. The string is case sensitive. If the Intent does not specify an action, the match will fail.

2. The
category matching rule category is a string, which is predefined by the system, and we can also define it ourselves.
The Intent can match successfully without specifying the category, because the system will add the Intent by default when calling startActivity android.intent.category.DEFAULT, but if the Intent specifies the category, it must match the category in the IntentFilter, otherwise the match will fail.

3. The
data matching rule data is composed of mimeType and URI.
mimeType refers to media types, such as pictures, videos, audios, etc. The
URI structure is as follows:

<scheme>://<host>:<port>/[<path> | <pathPrefix> | <pathPattern>]

scheme: URI mode, such as http, file, etc. If no scheme is specified, the URI parameter is invalid, that is, the URI is invalid.
host: The host name of the URI. If host is not specified, the URI parameter is invalid, that is, the URI is invalid.
port: port number, only if scheme and host exist and are reasonable, port is meaningful
path: complete path
pathPrefix: path prefix information
pathPattern: complete path, wildcards can be included
Matching rules:
Intent must specify data, and must match any of IntentFilter Only if a data match is successful, it makes sense.
If we specify mimeType separately, we can match successfully without knowing the URI, because the default value of URI is content and file

Three: Implicit Intent
Implicit Intent needs to clearly point to component information. The implicit Intent needs to match the filtering information set in the IntentFilter.
We first customize the action and categroy in the manifest file (remember that you can customize the meaningless action and categroy, as long as it matches the Intent)

<activity android:name=".FirstActivity">
            <intent-filter>
                <action android:name="com.example.implicit.Activity"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
                <category android:name="com.example.implicit.FirstActivity"/>
        </intent-filter>
        </activity>

Then we implicitly Intent in ManActivity

 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.implicit.Activity");
                intent.addCategory("com.example.implicit.FirstActivity");
                startActivity(intent);
            }
        });

Predefined Action and Category attribute values ​​The values ​​of
Action and category can be customized. The Android system also provides many predefined constant values ​​for starting the system's predefined Activity and Service.
Below we introduce some predefined indicators

Action常量            对应的字符串                    说明

ACTION_MIAN    android.intent.action.MAIN        应用程序入口  
ACTION_VIEW    android.intent.action.VIEW        显示指定数据
ACTION_EDIT    android.intent.action.EDIT        编辑指定数据
ACTION_DIAL    android.intent.action.DIAL        显示拨号面板
ACTION_CALL    android.intent.action.CALL        向指定用户打电话
ACTION_SEND    android.intent.action.SEND        向其他人发送数据
ACTION_SENDTO  android.intent.action.MESSAGE     向其他人发送消息
ACTION_ANSWER  android.intent.action.ANSWER        应答电话
ACTION_INSERT  android.intent.action.INSERT        插入数据
ACTION_DELETE  android.intent.action.DELETE        删除数据
ACTION_RUN     android.intent.action.RUN           运行数据
ACTION_SYNC    android.intent.action.SYNC          用户数据同步
ACTION_PICK_ACTIVITY android.intent.action.PICK_ACTIVITY  选择Activity
ACTION_SEARCH  android.intent.action.SEARCH        执行搜索
ACTION_WEB_SEARCH android.intent.action.WEB_SEARCH 执行Web搜索

Intent类中与Category相关的常量值列表

Category            常量对应的字符串                           说明
CATEGORY_DEFAULT    android.intent.category.DEFAULT           默认的Category
CATEGORY_TAB        android.intent.category.TAB指定Activity   作为TabActivity的Tab页
CATEGORY_LAUNCHER   android.intent.category.LAUNCHERActivity  显示在顶级程序列表中
CATEGORY_INFO       android.intent.category.INFO              用于提供包信息CATEGORY_HOMEandroid.intent.category.HOME设置该Activity随系统启动而运行
CATEGORY_PREFERENCE android.intent.category.PREFERENCE       设置Activity是参数面板

Guess you like

Origin blog.csdn.net/News53231323/article/details/113981962