Android——显式/隐式Intent

概述

在Android中,Intent是各个组件之间信息通信的桥梁,它用于Android各组件的通信。

Intent 的组成部分

一、显式 Intent

第一种方式

java 复制代码
	Intent intent = new Intent(this, ActFinishActivity.class);
	startActivity(intent);

第二种方式

java 复制代码
    Intent intent = new Intent();
    intent.setClass(this, ActFinishActivity.class);
    startActivity(intent);

第三种方式

java 复制代码
    Intent intent = new Intent();
    ComponentName componentName = new ComponentName(this, ActFinishActivity.class);
    intent.setComponent(componentName);
    startActivity(intent);

可以看出第一、二种比较简单且相似,但是这两种方式都需要获取到要跳转的类。如果要跳到其他应用就不行了,用 ComponentName 这种方式可以解决,如下:

java 复制代码
    ComponentName componentName = new ComponentName("com.example.study_android", "com.example.study_android.ActFinishActivity");

二、隐式 Intent

隐式 Intent 没有明确指定要跳转的目标活动,只给出一个动作字符串让系统自动匹配,属于模糊匹配。

  • 常见系统动作的取值说明

    动作名既可以通过 setAction 方法指定,也可以通过构造函数 Intent(String action) 直接生成意图对象。
  • 跳转到拨号系统应用
java 复制代码
    private void handleNavHidden() {
        String phoneNo = "12345";

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_DIAL);

        Uri uri = Uri.parse("tel:" + phoneNo);
        intent.setData(uri);

        startActivity(intent);
    }
  • 跳转到另一个APP
java 复制代码
    private void handleNavMy() {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.MYAPP");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        startActivity(intent);
    }

在另一个APP的主Activity中添加以下代码:

xml 复制代码
   <intent-filter>
        <action android:name="android.intent.action.MYAPP"/>
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

三、向下一个Activity发送数据

  • Intent 使用 Bundle 对象存放待传递的数据信息。
  • Bundle 对象操作各类型数据的读写方法说明见下表:

    通过 Bundle 传递
java 复制代码
    private void handleNextData() {
        Intent intent = new Intent(this, ActFinishActivity.class);
        Bundle bundle = new Bundle();

        long currentTimeMillis = System.currentTimeMillis();
        
        bundle.putString("request_time", Long.toString(currentTimeMillis));
        bundle.putString("request_content", "hello,world");
        intent.putExtras(bundle);

        startActivity(intent);
    }

在跳转后的页面中接收 Bundle

java 复制代码
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_finish);

        tv_desc=findViewById(R.id.tv_desc);

        Bundle bundle = getIntent().getExtras();
        String request_time = bundle.getString("request_time");
        String request_content = bundle.getString("request_content");

        String desc = String.format("消息是:%s,\n时间是:%s", request_content, request_time);
        tv_desc.setText(desc);
    }

也可以不通过Bundle,直接传单个数据

java 复制代码
 	intent.putExtra("name","张三");

接收单个数据

java 复制代码
	String name = getIntent().getStringExtra("name");

四、向上一个Activity发送数据

步骤:

  • 当前页面通过 registerForActivityResult 注册回调
java 复制代码
    private ActivityResultLauncher<Intent> register;
java 复制代码
        register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result != null) {
                Intent intent = result.getData();

                if (intent != null && result.getResultCode() == Activity.RESULT_OK) {
                    Bundle bundle = intent.getExtras();
                    String response_time = bundle.getString("response_time");
                    String response_content = bundle.getString("response_content");
                    String desc = String.format("返回的消息是:%s\n,时间:%s", response_content, response_time);
                    tv_res.setText(desc);
                }
            }
        });
  • 当前页面通过 register.launch 发起跳转
java 复制代码
    private void handleDataRes() {
        Intent intent = new Intent(this, ActFinishActivity.class);
        Bundle bundle = new Bundle();

        long currentTimeMillis = System.currentTimeMillis();
        bundle.putString("request_time", Long.toString(currentTimeMillis));
        bundle.putString("request_content", "hello,world");
        bundle.putString("name", "张三");

        intent.putExtras(bundle);
        register.launch(intent);
    }
  • 在下个页面调用 setResult 设置数据,并通过 finish结束页面
java 复制代码
    public void onClick(View view) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();

        long currentTimeMillis = System.currentTimeMillis();

        bundle.putString("response_time", Long.toString(currentTimeMillis));
        bundle.putString("response_content", "你好啊");

        intent.putExtras(bundle);
		// 携带意图返回上一个页面,RESULT_OK 表示处理成功
        setResult(Activity.RESULT_OK, intent);
		// 结束当前活动页
        finish();
    }

案例代码

相关推荐
安卓开发者8 小时前
Android RxJava 组合操作符实战:优雅处理多数据源
android·rxjava
阿华的代码王国9 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端
一条上岸小咸鱼9 小时前
Kotlin 基本数据类型(三):Booleans、Characters
android·前端·kotlin
Jerry说前后端9 小时前
RecyclerView 性能优化:从原理到实践的深度优化方案
android·前端·性能优化
alexhilton10 小时前
深入浅出着色器:极坐标系与炫酷环形进度条
android·kotlin·android jetpack
一条上岸小咸鱼16 小时前
Kotlin 基本数据类型(一):Numbers
android·前端·kotlin
Huntto16 小时前
最小二乘法计算触摸事件速度
android·最小二乘法·触摸事件·速度估计
一笑的小酒馆16 小时前
Android中使用Compose实现各种样式Dialog
android
红橙Darren16 小时前
手写操作系统 - 编译链接与运行
android·ios·客户端
鹏多多.20 小时前
flutter-使用device_info_plus获取手机设备信息完整指南
android·前端·flutter·ios·数据分析·前端框架