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

案例代码

相关推荐
齊家治國平天下5 小时前
Android 14 Vehicle HAL (VHAL) 设计与实现深度解析
android·aaos·carservice·车载开发·android 14·vhal·vehicle hal
AFinalStone11 小时前
Android 7系统网络(八)应用API层—ConnectivityManager使用与实战调试
android·网络
plainGeekDev11 小时前
kapt 替换为 KSP
android·java·kotlin
蜡台12 小时前
通过Gradle脚本声明更改Java变量
android·java·开发语言·python·kotlin·gradle·groovy
阿pin15 小时前
Android随笔-SharedPreferences
android·sp
阿pin15 小时前
Android随笔-DataStore
android·datastore
茶栀(*´I`*)16 小时前
Android 测试入门指南:ADB 基础配置与常用设备管理命令解析
android·adb
ihuyigui18 小时前
海外酒店预订短信接口
android·java·运维·服务器·前端·后端·架构
gxgldyh18 小时前
Android Framework源码解析(七):BootAnimation 启动流程解析——开机动画是如何显示出来的?
android
达达尼昂19 小时前
在 Claude Cowork 中用好 Claude Fable 5
android·人工智能·后端