使用Intent进行页面之间的跳转【Intent_1】

 

目录

Intent 类型

一、显式Intent方法

二、隐式Intent方法


开发文档链接:Intent 和 Intent 过滤器  |  Android 开发者  |  Android Developers (google.cn)https://developer.android.google.cn/guide/components/intents-filters

Intent是一个消息传递对象,解决Android应用的各项组件之间的通讯,您可以用来从其他应用组件请求操作。尽管 Intent 可以通过多种方式促进组件之间的通信,但其基本用例主要包括以下三个:

  • 启动 Activity,即实现在几个Activity之间的跳转
  • 启动服务
  • 传递广播

Intent 类型

Intent 分为两种类型:

  • 显式 Intent:通过提供目标应用的软件包名称或完全限定的组件类名来指定可处理 Intent 的应用。通常,您会在自己的应用中使用显式 Intent 来启动组件,这是因为您知道要启动的 Activity 或服务的类名。例如,您可能会启动您应用内的新 Activity 以响应用户操作,或者启动服务以在后台下载文件。
  • 隐式 Intent :不会指定特定的组件,而是声明要执行的常规操作,从而允许其他应用中的组件来处理。例如,如需在地图上向用户显示位置,则可以使用隐式 Intent,请求另一具有此功能的应用在地图上显示指定的位置。

本文主要介绍第一个用法,通过显式和隐式分别启动 Activity,即实现在几个Activity之间的跳转。

作好准备工作,新建一个项目,命名为MyAppTest_1。

先来构建两个Activity。分别命名为MainActivity(主界面)和SecondActivity(跳转之后的界面)。

activity_main.xml中添加两个按钮。

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_1"
        android:text="显式Intent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <Button
        android:id="@+id/btn_2"
        android:text="隐式Intent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_gravity="center"/>

</LinearLayout>

activity_second.xml中添加一个字段以示区分。

<?xml version="1.0" encoding="utf-8"?>
<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=".SecondActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="This is SecondActivity" />

</LinearLayout>

一、显式Intent方法

1. 1  MainActivity.java  完整代码

package com.example.myapptest_1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        //跳转页面的按钮,显式Intent。
        //通过findViewById获取按钮,并定义变量名为bt_change_1
        Button bt_change_1 =(Button)this.findViewById(R.id.btn_1);
        bt_change_1.setOnClickListener((View view) -> {
            //显式Intent
            Intent intent = new Intent(this, SecondActivity.class);
            this.startActivity(intent);
        });

    }
}

这是其中的第一种写法。


        //跳转页面的按钮,显式Intent。
        //通过findViewById获取按钮,并定义变量名为bt_change_1
        Button bt_change_1 =(Button)this.findViewById(R.id.btn_1);
        bt_change_1.setOnClickListener((View view) -> {
            //显式Intent
            Intent intent = new Intent(this, SecondActivity.class);
            this.startActivity(intent);
        });

还有第二种写法,将上方代码块替换为如下。


        //跳转页面的按钮,显式Intent。
        //通过findViewById获取按钮,并定义变量名为bt_change_2
        Button bt_change_2 =(Button)this.findViewById(R.id.btn_1);
        bt_change_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });

最后运行界面及结果:

二、隐式Intent方法

隐式 Intent 指定能够在可以执行相应操作的设备上调用任何应用的操作。如果您的应用无法执行该操作而其他应用可以,且您希望用户选取要使用的应用,则使用隐式 Intent 非常有用。

Q:  那么是怎样正确找到并打开目标活动呢?

Intent的发送者在构造Intent对象时,并不需要指定“接收者”,而是通过一定的设置,由系统进行筛选,这有助于解耦。隐式Intent需要借助Intent Filter来实现“筛选”这一过程,并且仅当隐式 Intent 可以通过 Intent 过滤器之一传递时,系统才会将该 Intent 传递给应用组件。
 

意思就是说,当且仅当该 “Intent对象” 与 “在清单文件中为目标活动创建的action和category” 相一致时,即调用该目标活动。

<action>为目标活动地址,而<category>标签则包含了一些附加信息,更精确地指明了当前的活动能够响应的Intent中还可能带有的category。只有<action>和<category>中的内容同时能够匹配上
Intent中指定的action和 category时,这个活动才能响应该Intent。
 


下面是具体的做法。

由此引出,我们要注意,在使用隐式调用时,需要先在AndroidManifest.xml(清单文件)中对需要打开的Activity进行定义。

2.1 AndroidManifest.xml 完整代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapptest_1">

    <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/Theme.MyAppTest_1">

        <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.example.myapptest_1.ACTION_START"></action>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.example.myapptest_1.MY_CATEGORY"/>
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

我们做了什么呢,其他的都没有变,仅添加了以下代码:

        <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.example.myapptest_1.ACTION_START"></action>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.example.myapptest_1.MY_CATEGORY"/>
            </intent-filter>
        </activity>

几个问题:

  1. 为什么在<action android:name="com.example.myapptest_1.ACTION_START"></action>这里,最后这里是有一个</action>的方式结尾,我也尝试了以下,改为以 />结尾程序也是可以跑通的。不知道是不是有什么特别的区别。
  2. 这里的com.example.myapptest_1.是活动所在的地址。需要换成你对应的路径。

  3. 为什么定义了两个 category,其中android.intent.category.DEFAULT是一种默认的category, 在调用startActivity()方法的时候会自动将这个category添加到 Intent中。第二个是自己定义的。
  4. 注意:如果当前组件是Activity,并且没有指定的category,必须加上category并使用默认的DEFAULT!!!
    <category android:name="android.intent.category.DEFAULT" />
    
  5. 在<action>和<category>后面这些全大写的指令是什么意思,这些,可以搜一下对应的源码。只需要跳转页面的话,就按照这个样子写是可以的。
  6. 注意:
  • 一个Activity可以指定多个action属性
  • 一个Activity也可以指定多个category属性
  • 一个Intent只能指定一个action
  • 一个intent可以指定多个category

2. 2  MainActivity.java  完整代码

package com.example.myapptest_1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        //跳转页面的按钮,隐性Intent
        Button bt_second =(Button)this.findViewById(R.id.btn_2);
        bt_second.setOnClickListener((View view) -> {
            //隐性Intent
            Intent intent = new Intent("com.example.myapptest_1.ACTION_START");
            intent.addCategory("com.example.myapptest_1.MY_CATEGORY");
            this.startActivity(intent);
        });

    }
}

几个问题:

若没有指定的category,则不需要添加

intent.addCategory("com.example.myapptest_1.MY_CATEGORY");

只需要如下指定action即可,就是说这样写也是可行的,不会报错,但不建议。

        //跳转页面的按钮,隐性Intent
        Button bt_second =(Button)this.findViewById(R.id.btn_2);
        bt_second.setOnClickListener((View view) -> {
            //隐性Intent
            Intent intent = new Intent("com.example.myapptest_1.ACTION_START");
            this.startActivity(intent);
        });

那么我们跳转到了新的界面怎么返回呢?

下面是返回代码示例,我们在 SecondActivity.java 中编写。

package com.example.myapptest_1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends AppCompatActivity {

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

        Button gobackBtn = findViewById(R.id.button_back);
        gobackBtn.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 返回上一个页面
                finish();
            }
        });

    }
}

相对应的activity_second.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<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=".SecondActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="This is SecondActivity" />

    <Button
        android:id="@+id/button_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="back"/>

</LinearLayout>

界面如图。

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值