目录
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中添加两个按钮。
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">
在activity_second.xml中添加一个字段以示区分。
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"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="This is SecondActivity" />
一、显式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” 相一致时,即调用该目标活动。
下面是具体的做法。
由此引出,我们要注意,在使用隐式调用时,需要先在AndroidManifest.xml(清单文件)中对需要打开的Activity进行定义。
2.1 AndroidManifest.xml 完整代码
package="com.example.myapptest_1"> 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可以指定多个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 文件
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"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="This is SecondActivity" /> android:id="@+id/button_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="back"/>
界面如图。
相关链接
发表评论