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();
    }

案例代码

相关推荐
陈大头铃儿响叮当1 小时前
Android Studio升级后,Flutter运行android设备报错
android·flutter·android studio
勤劳打代码1 小时前
isar_flutter_libs 引发 Namespace not specified
android·flutter·groovy
奔跑吧 android3 小时前
【android bluetooth 协议分析 18】【PBAP详解 2】【车机为何不显示电话号码为空的联系人信息】
android·蓝牙电话·hfp·pbap·电话簿
深盾科技3 小时前
安卓二次打包技术深度拆解:从逆向篡改到防护逻辑
android
4Forsee3 小时前
【Android】消息机制
android·java·前端
2501_915921434 小时前
iOS 虚拟位置设置实战,多工具协同打造精准调试与场景模拟环境
android·ios·小程序·https·uni-app·iphone·webview
龚礼鹏4 小时前
Android 图像显示框架三——演示demo以及解析
android·交互
QuantumLeap丶5 小时前
《Flutter全栈开发实战指南:从零到高级》- 11 -状态管理Provider
android·flutter·ios
百锦再5 小时前
第6章 结构体与方法
android·java·c++·python·rust·go
gustt5 小时前
用小程序搭建博客首页:从数据驱动到界面展示
android·前端·微信小程序