显示意图,一般情况下是用于,APP应用自身:组件(Activity,Service,...) 与 组件(Activity,Service,...) 的激活调用:

 

显示意图,是可以看得到,明确激活哪一个Activity

package liudeli.activity.intent;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import liudeli.activity.R;

public class OneActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
    }

    public void startTwoActivity(View view) {
        /**
         * 显示意图激活的三种方式
         */
        // 1.快捷方式激活
        // Intent intent = new Intent(this, TwoActivity.class);

        // 2.面向组件激活,老外很喜欢用这种方式激活
        /*Intent intent = new Intent();
        ComponentName componentName = new ComponentName(this, TwoActivity.class);
        intent.setComponent(componentName);*/

        // 3.设置Class方式激活
        /*Intent intent = new Intent();
        intent.setClass(this, TwoActivity.class);*/

        // 4.设置ClassName方式激活,传入完整包名+类名
        Intent intent = new Intent();
        intent.setClassName(this, "liudeli.activity.intent.TwoActivity");

        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

 

activity_one.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="激活"
        android:onClick="startTwoActivity"
        />

</RelativeLayout>

 


 

 

隐式意图,隐式意图一般情况下用于,两个应用程序的:组件(Activity,Service,...) 与 组件(Activity,Service,...) 的激活调用:

 

OuterProject应用的MainActivity需要对外暴露(在AndroidManifest.xml 文件中 对Activity 配置 Intent-filter):

必须要有动作标识:wo.shi.outer.project.main.activity.action
必须要有一个缺省:<category android:name="android.intent.category.LAUNCHER" />
    <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="wo.shi.outer.project.main.activity.action" />

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

            </intent-filter>

        </activity>

 

我的应用的OneActivity去隐式意图激活,OuterProject应用的MainActivity

 

在这里为什么不需要设置:category android:name="android.intent.category.DEFAULT",因为在startActivity会自动添加
    /**
         * 隐士意图去激活 OuterProject应用的Activity
         *
        /*
            <intent-filter>

                <action android:name="wo.shi.outer.project.main.activity.action" />

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

            </intent-filter>
         */
        Intent intent = new Intent();
        intent.setAction("wo.shi.outer.project.main.activity.action");
        intent.setPackage("liudeli.outer");
        // 在这里为什么不需要设置:category android:name="android.intent.category.DEFAULT",因为在startActivity会自动添加
        startActivity(intent);

 

 


 

OuterProject应用的MainActivity

Intent-filter 参数增加:

     <!--
            android:mimeType="aa/bb" 必须是 xxx/xxx 这种格式,否则安装应用的时候解析失败
            android:scheme="mydata" 不需要 mydata:, 但是在激活的那边需要加:
         -->
        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="wo.shi.outer.project.main.activity.action" />

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

                <data android:scheme="mydata" android:mimeType="aa/bb" />

            </intent-filter>

        </activity>

 

 

我的应用的OneActivity去隐式意图激活

    /**
         * 隐士意图去激活 OuterProject应用的Activity
         */
        /*
            <intent-filter>

                <action android:name="wo.shi.outer.project.main.activity.action" />

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

                <data android:scheme="mydata" android:mimeType="aa/bb" />

            </intent-filter>
         */
        Intent intent = new Intent();
        intent.setAction("wo.shi.outer.project.main.activity.action");
        intent.setPackage("liudeli.outer");

        // 注意⚠️:当有Data和Type的时候,这种方式不可用🚫
        /*intent.setData(Uri.parse("mydata:"));
        intent.setType("aa/bb");*/

        intent.setDataAndType(Uri.parse("mydata:"), "aa/bb");

        // 在这里为什么不需要设置:category android:name="android.intent.category.DEFAULT",因为在startActivity会自动添加
        startActivity(intent);

 


 

intent-filter 多匹配规则:当有多个 action,多个category,多个data,只需要匹配一组

     <!--
            android:mimeType="aa/bb" 必须是 xxx/xxx 这种格式,否则安装应用的时候解析失败
            android:scheme="mydata" 不需要 mydata:, 但是在激活的那边需要加:
         -->
        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="wo.shi.outer.project.main.activity.action" />
                <action android:name="wo.shi.outer.project.main.activity.action2" />
                <action android:name="wo.shi.outer.project.main.activity.action3" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.AAA" />
                <category android:name="android.intent.category.BBB" />
                
                <data android:scheme="mydata" android:mimeType="aa/bb" />
                <data android:scheme="mydata2" android:mimeType="aa/bb22" />
                <data android:scheme="mydata3" android:mimeType="aa/bb33" />

            </intent-filter>

        </activity>

 

intent-filter 多匹配规则:当有多个intent-filter,只需匹配任意intent-filter即可

 

当看到有多个intent-filter,只需匹配某个intent-filter即可

隐式意图,更多的时候,是去访问激活系统的intent-filter