显示Intent和隐式Intent讲解与实例应用

什么是Intent:

Intent是Android中各个组件之间进行交互的一种重要方式
他不仅可以表示当前组件想要执行的步骤,而且还可以在不同组件之间传递数据

Intent的分类

大致分为两种:显性Intent、隐性Intent

如何使用显式Intent:Intent的意图非常明显

实现效果

具体代码实现:(为了提高我英语水平,我决定添加英语备注具体代码)

1.修改FirstActivity的布局文件:添加按钮,设置id
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.activitytest.FirstActivity">

   <Button
       android:id="@+id/first_btn"
       android:layout_width="match_parent"
       android:layout_height="50dp"
       android:text="button  1"/>

</LinearLayout>

2.#####1.修改SecondActivity的布局文件:添加按钮,设置id

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.activitytest.SecondActivity">

    <Button
        android:id="@+id/second_btn"
        android:layout_width="match_parent"
        android:layout_height="50sp"
        android:text="button 2" />

</LinearLayout>

3.修改FirstActivity活动:实现创建按钮对象,设置监听,实现跳转

public class FirstActivity extends AppCompatActivity {

    //创建按钮对象   create button object
    private Button startBtn;

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

        //绑定id    bind id
        startBtn = findViewById(R.id.first_btn);
        //设置监听   set monitor
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //实现跳转      
                // achieve goto
                //创建Intent对象 参数1:当前上下文环境,参数2:将要跳转的活动页面   
                // create Intent object (param1: current context    param2:will goto context
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                //启动跳转   start goto 
                startActivity(intent);
            }
        });
    }
}

over

如何使用隐式Intent:

隐式Intent的比较含蓄,他并不会明确指出将要启动的活动,而是指定一系列更为抽象的action和category等信息,然后由系统去分析intent的意图,最后找到合适的活动去启动。

实例演示:

效果展现:

具体代码展现:

1.修改Manifest中的SecondActivity的启动方式:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.activitytest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".FirstActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity">
            <!--修改SecondActivity的启动方式   alter SecondActivity start mode-->
            <intent-filter>
                <!--添加action标签,说明此活动只能被这个action启动-->
                <!--add action label ,explain activity only start by this action -->
                <action android:name="com.example.administrator.activitytest.ACTION_START"/>
                <!--添加category标签,两个标签同时满足,才能启动活动,但category.DEFAULT为默认category,因此启动时不需要指定-->
                <!--add category label,only two label satisfy,this activity can start,but category.DEFAULT is default,so not need point out-->
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>

</manifest>
修改FirstActivity中的启动方式

public class FirstActivity extends AppCompatActivity {

//创建按钮对象   create button object
private Button startBtn;

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

    //绑定id    bind id
    startBtn = findViewById(R.id.first_btn);
    //设置监听   set monitor
    startBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //实现跳转
            // achieve goto
            //创建Intent对象 参数1:启动什么样的action
            // create Intent object (param1: start what action)
            Intent intent = new Intent("com.example.administrator.activitytest.ACTION_START");
            startActivity(intent);
        }
    });
}

}

over

现在程序已经可以运行,不过如果在Menifest中添加一句自定义的category,应该如何启动活动呢

需要注意的是每个Intent只能指定一个action,但却可以指定多个category,我们来添加一个自定义的吧

1.1.修改Manifest中的SecondActivity的启动方式:
 <intent-filter>
                <!--添加action标签,说明此活动只能被这个action启动-->
                <!--add action label ,explain activity only start by this action -->
                <action android:name="com.example.administrator.activitytest.ACTION_START"/>
                <!--添加category标签,两个标签同时满足,才能启动活动,但category.DEFAULT为默认category,因此启动时不需要指定-->
                <!--add category label,only two label satisfy,this activity can start,but category.DEFAULT is default,so not need point out-->
                <category android:name="android.intent.category.DEFAULT"/>
                <!--添加自定义的category    add user defined category-->
                <category android:name="com.example.administrator.activitytest.MY_CATEGORY"/>
            </intent-filter>

2.修改FirstActivity中的启动方式:

Intent intent = new Intent("com.example.administrator.activitytest.ACTION_START");
                //添加自定义的category   add user defined category
                intent.addCategory("com.example.administrator.activitytest.MY_CATEGORY");
                startActivity(intent);

over

其实隐式的Intent不仅可以调用自己的activity,还可以启动其他程序的activity

效果展现:

代码实现:只需要在启动当时上修改:
 //创建Intent对象 参数:启动什么样的action,ACTION_VIEW为系统内部的动作
                // create Intent object (param1: start what action),ACTION_VIEW is system action
                Intent intent = new Intent(Intent.ACTION_VIEW);
                //给Intent设置数据,将网址通过parse解析然后来设置网址
                //set Intent  data ,put website parsed Uri object
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
为了精确的指定当前的活动相应什么样的数据,在Menifest添加如下数据
<intent-filter>
                <!--添加action标签,说明此活动只能被这个action启动-->
                <!--add action label ,explain activity only start by this action -->
                <action android:name="com.example.administrator.activitytest.ACTION_START"/>
                <!--添加category标签,两个标签同时满足,才能启动活动,但category.DEFAULT为默认category,因此启动时不需要指定-->
                <!--add category label,only two label satisfy,this activity can start,but category.DEFAULT is default,so not need point out-->
                <category android:name="android.intent.category.DEFAULT"/>
                <!--响应的数据类型为http   response http data -->
                <data android:scheme="http"/>
            </intent-filter>

over

猜你喜欢

转载自blog.csdn.net/shaochen2015821426/article/details/79685260
今日推荐