Carson带你学Android:这是一份全面 & 详细的Intent组件学习指南

桃扇骨 2022-05-11 02:04 332阅读 0赞

a26aedafc2ac45510787091b1d02f019.png

前言

  • IntentAndroid开发的应用非常常见
  • 今天我就带给大家简单讲一下Intent的相关知识 & 其用法

Carson带你学Android系列文章
Carson带你学Android:学习方法
Carson带你学Android:四大组件
Carson带你学Android:自定义View
Carson带你学Android:异步-多线程
Carson带你学Android:性能优化
Carson带你学Android:动画


目录

示意图


1. 定义

意图,描述的是应用的动作 & 其对应的数据


2. 作用

  1. 指定当前组件要完成的动作
  2. Android 不同组件间 传递数据

ActivityServiceBroadcastReceiver之间的通信载体 = Intent

下面,将根据Intent的作用,详细讲解其使用方法


3. 使用1:指定当前组件要完成的动作

该使用 分为显式 & 隐式意图:

3.1 显式意图

  • 特点
    明确指定需启动的组件名

即 显式Intent不需 解析Intent 则可直接启动目标组件

  • 具体使用
    明确指定组件名的方式:调用Intent的构造方法、Intent.setComponent()Intent.setClass()
  • 实例说明

    // 使FirstActivity启动SecondActivity(通过按钮)
    mybutton.setOnClickListener(new OnClickListener() {

    1. @Override
    2. public void onClick(View v) {
    3. // 1. 实例化显式Intent & 通过构造函数接收2个参数
    4. // 参数1 = Context:启动活动的上下文,一般为当前Activity
    5. // 参数2 = Class:是指定要启动的目标活动
    6. Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
    7. // 2. 通过Activity类的startActivity()执行该意图操作(接收一个Intent对象)
    8. // 将构建好的Intent对象传入该方法就可启动目标Activity
    9. startActivity (intent);
    10. }

    });

3.2 隐式意图

  • 特点
    无明确指定需启动的组件名,但 指定了需启动组件需满足的条件

即 隐式Intent需 解析Intent ,才可启动目标组件

  • 具体使用
    通过 AndroidManifest.xml文件下的<Activity>标签下的<intent -filter> 声明 需 匹配的条件

一个<Activity>标签下可以有多组,只需匹配其中1组即可

  • 详细说明
    声明条件含:动作(Action)、类型(Category)、数据(Data

示意图

  • 实例说明

    // 使FirstActivity启动SecondActivity(通过按钮)

    1. mybutton.setOnClickListener(new OnClickListener() {
    2. @Override
    3. public void onClick(View v) {
    4. // 1. 实例化1个隐式Intent对象,并指定action参数
    5. Intent intent = new Intent("android.intent.action.ALL_APPS");
    6. // 2. 调用Intent中的addCategory()来添加一个category
    7. // 注:每个Intent中只能指定1个action,但却能指定多个category

    intent.addCategory(“com.example.intent_test.MY_CATEGORY”);

    1. startActivity (intent);
    2. }

    });

    // 为使SecondActivity能继续响应该Intent
    // 我们需在AndroidManifest.xml文件下的标签下配置的内容

    1. <action android:name="android.intent.action.ALL_APPS"/>
    2. <category android:name="android.intent.category.DEFAULT">
    3. </category>
    4. <category android:name="com.example.intent_test.MY_CATEGORY"/>
    5. </intent-filter>

4. 使用2:不同组件间 传递数据

4.1 使用方法

putExtra()Bundle方式

4.2 可传递的数据类型

a. 8种基本数据类型(boolean byte char short int long float double)、String
b. IntentBundle
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. // 1. 数据传递
    2. // a. 创建Intent对象(显示Intent)
    3. Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
    4. // b. 通过putExtra()方法传递一个字符串到SecondActivity;
    5. // putExtra()方法接收两个参数:第一个是键,第二个是值(代表真正要传递的数据)
    6. intent.putExtra("data","I come from FirstActivity");
    7. // c. 启动Activity
    8. startActivity(intent);
    9. // 2. 数据取出(在被启动的Activity中)
    10. // a. 获取用于启动SecondActivit的Intent
    11. Intent intent = getIntent();
    12. // b. 调用getStringExtra(),传入相应的键名,就可得到传来的数据
    13. // 注意数据类型 与 传入时保持一致
    14. String data = intent.getStringExtra("data");
  • 方法2:Bundle

    // 1. 数据传递

    1. // a. 创建Intent对象(显示Intent)
    2. Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
    3. // b. 创建bundle对象
    4. Bundle bundle = new Bundle();
    5. // c. 放入数据到Bundle
    6. bundle.putString("name", "carson");
    7. bundle.putInt("age", 28);
    8. // d. 将Bundle放入到Intent中
    9. intent.putExtras(bundle);
    10. // e. 启动Activity
    11. startActivity(intent);
    12. // 2. 数据取出(在被启动的Activity中)
    13. // a. 获取用于启动SecondActivit的Intent
    14. Intent intent = getIntent();
    15. // b. 通过Intent获取bundle
    16. Bundle bundle = intent.getExtras();
    17. // c. 通过bundle获取数据传入相应的键名,就可得到传来的数据
    18. // 注意数据类型 与 传入时保持一致
    19. String nameString = bundle.getString("name");
    20. 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接口

  1. // 如传递User类的对象
  2. public class User implements Serializable {
  3. ...
  4. }
  5. // 传递时
  6. User user = new User();
  7. Intent intent = new Intent(MyActivity.this,OthereActivity.class);
  8. Bundle bundle = new Bundle();
  9. bundle.putSerializable("user", user);
  10. intent.putExtras(bundle);

putExtra()更多使用于单次传递、传递简单数据类型的应用场景


5. 总结

  • 本文对Android中的Intent组件进行了全面的介绍
  • Carson带你学Android系列文章
    Carson带你学Android:学习方法
    Carson带你学Android:四大组件
    Carson带你学Android:自定义View
    Carson带你学Android:异步-多线程
    Carson带你学Android:性能优化
    Carson带你学Android:动画

欢迎关注Carson_Ho的CSDN博客 与 公众号!

博客链接:https://carsonho.blog.csdn.net/
06b2ed0fbb8979f0aba34c14ede04b59.png


请帮顶 / 评论点赞!因为你的鼓励是我写作的最大动力!

发表评论

表情:
评论列表 (有 0 条评论,332人围观)

还没有评论,来说两句吧...

相关阅读