前言
Intent 在 Android 开发的应用非常常见
今天我就带给大家简单讲一下 Intent 的相关知识 & 其用法
目录
1. 定义
意图, 描述的是应用的动作 & 其对应的数据
2. 作用
指定当前组件要完成的动作
在 Android 不同组件间 传递数据
Activity,Service,BroadcastReceiver 之间的通信载体 = Intent
下面, 将根据 Intent 的作用, 详细讲解其使用方法
3. 使用 1: 指定当前组件要完成的动作
该使用 分为显式 & 隐式意图:
3.1 显式意图
特点
明确指定需启动的组件名
即 显式 Intent 不需 解析 Intent 则可直接启动目标组件
具体使用
明确指定组件名的方式: 调用 Intent 的构造方法, Intent.setComponent(),Intent.setClass()
实例说明
- // 使 FirstActivity 启动 SecondActivity(通过按钮)
- mybutton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 1. 实例化显式 Intent & 通过构造函数接收 2 个参数
- // 参数 1 = Context: 启动活动的上下文, 一般为当前 Activity
- // 参数 2 = Class: 是指定要启动的目标活动
- Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
- // 2. 通过 Activity 类的 startActivity()执行该意图操作(接收一个 Intent 对象)
- // 将构建好的 Intent 对象传入该方法就可启动目标 Activity
- startActivity (intent);
- }
- });
3.2 隐式意图
特点
无明确指定需启动的组件名, 但 指定了需启动组件需满足的条件
即 隐式 Intent 需 解析 Intent , 才可启动目标组件
具体使用
通过
AndroidManifest.xml
文件下的 < Activity > 标签下的 < intent -filter> 声明 需 匹配的条件
一个 < Activity > 标签下可以有多组 < intent -filter>, 只需匹配其中 1 组即可
详细说明
声明条件含: 动作(Action), 类型(Category), 数据(Data)
实例说明
- // 使 FirstActivity 启动 SecondActivity(通过按钮)
- mybutton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 1. 实例化 1 个隐式 Intent 对象, 并指定 action 参数
- Intent intent = new Intent("Android.intent.action.ALL_APPS");
- // 2. 调用 Intent 中的 addCategory()来添加一个 category
- // 注: 每个 Intent 中只能指定 1 个 action, 但却能指定多个 category
- intent.addCategory("com.example.intent_test.MY_CATEGORY");
- startActivity (intent);
- }
- });
- // 为使 SecondActivity 能继续响应该 Intent
- // 我们需在 AndroidManifest.xml 文件下的 < Activity > 标签下配置 < intent -filter > 的内容
- <intent-filter>
- <action Android:name="Android.intent.action.ALL_APPS"/>
- <category Android:name="Android.intent.category.DEFAULT">
- </category>
- <category Android:name="com.example.intent_test.MY_CATEGORY"/>
- </intent-filter>
4. 使用 2: 不同组件间 传递数据
4.1 使用方法
putExtra(),Bundle 方式
4.2 可传递的数据类型
a. 8 种基本数据类型(boolean byte char short int long float double),String
b. Intent,Bundle
c. Serializable 对象, Parcelable 及其对应数组, CharSequence 类型
d. ArrayList, 泛型参数类型为:<Integer>,<? Extends Parcelable>,<Charsequence>,<String>
4.3 具体使用
在当前 Activity 把要传递的数据暂存在 Intent 中, 在新启动的 Activity 中取出 Intent 中的数据
方法 1:putExtra()
- // 目的: 将 FristActivity 中的一个字符串传递到 SecondActivity 中, 并在 SecondActivity 中将 Intent 对象中的数据 (FristActivity 传递过来的数据) 取出
- // 1. 数据传递
- // a. 创建 Intent 对象(显示 Intent)
- Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
- // b. 通过 putExtra()方法传递一个字符串到 SecondActivity;
- // putExtra()方法接收两个参数: 第一个是键, 第二个是值(代表真正要传递的数据)
- intent.putExtra("data","I come from FirstActivity");
- // c. 启动 Activity
- startActivity(intent);
- // 2. 数据取出(在被启动的 Activity 中)
- // a. 获取用于启动 SecondActivit 的 Intent
- Intent intent = getIntent();
- // b. 调用 getStringExtra(), 传入相应的键名, 就可得到传来的数据
- // 注意数据类型 与 传入时保持一致
- String data = intent.getStringExtra("data");
方法 2:Bundle
- // 1. 数据传递
- // a. 创建 Intent 对象(显示 Intent)
- Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
- // b. 创建 bundle 对象
- Bundle bundle = new Bundle();
- // c. 放入数据到 Bundle
- bundle.putString("name", "carson");
- bundle.putInt("age", 28);
- // d. 将 Bundle 放入到 Intent 中
- intent.putExtras(bundle);
- // e. 启动 Activity
- startActivity(intent);
- // 2. 数据取出(在被启动的 Activity 中)
- // a. 获取用于启动 SecondActivit 的 Intent
- Intent intent = getIntent();
- // b. 通过 Intent 获取 bundle
- Bundle bundle = intent.getExtras();
- // c. 通过 bundle 获取数据传入相应的键名, 就可得到传来的数据
- // 注意数据类型 与 传入时保持一致
- String nameString = bundle.getString("name");
- int age = bundle.getInt("age");
4.4 两种方式的区别
Bundle 意为 捆绑 的意思, 更多适用于:
连续传递数据
若需实现连续传递: Activity A -> B -> C; 若使用 putExtra(), 则需写两次 intent = A->B 先写一遍 + 在 B 中取出来 & 再把值重新写到 Intent 中再跳到 C; 若使用 Bundle, 则只需取出 & 传入 Bundle 对象即可
可传递的值: 对象
putExtra()无法传递对象, 而 Bundle 则可通过 putSerializable 传递对象
但传递的对象要实现 Serializable 接口
- // 如传递 User 类的对象
- public class User implements Serializable {
- ...
- }
- // 传递时
- User user = new User();
- Intent intent = new Intent(MyActivity.this,OthereActivity.class);
- Bundle bundle = new Bundle();
- bundle.putSerializable("user", user);
- intent.putExtras(bundle);
而 putExtra()更多使用于单次传递, 传递简单数据类型的应用场景
5. 总结
本文对 Android 中的 Intent 组件进行了全面的介绍
接下来我将继续介绍 Android 开发中的相关知识, 有兴趣可以继续关注 Carson_Ho 的安卓开发笔记 https://blog.csdn.NET/carson_ho
来源: https://juejin.im/entry/5ba20cede51d450e664b392f