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

案例代码

相关推荐
一笑的小酒馆18 小时前
Android中的tcp通信
android
默阳123421 小时前
2026年Android中高级面试题专栏(Android进阶篇)
android
古法安卓1 天前
Android-日志系统源码解析
android·java·android studio
恋猫de小郭1 天前
Android 17 + OkHttp 5.5.0 ,全新 ECH 下你的 HTTPS 域名可以请求时被安全隐藏
android·前端·flutter
小强闯江湖1 天前
不只是让 AI 写代码:ViewCompose 把 Android UI 做成了可编译、可渲染的闭环
android·人工智能·kotlin
hunterandroid1 天前
StateFlow 与 SharedFlow 的边界:状态与事件的正确建模
android·前端
YF02111 天前
Android遥控器对频详解
android·android things
码农coding1 天前
android12 WindowManagerService窗口的布局过程
android·源码
杉氧1 天前
状态管理变迁史:为什么我们放弃了 Redux 选择 Zustand?
android·前端·react native
三少爷的鞋1 天前
别再靠 Code Review 守底线:我做了一个静态分析项目 RedLine
android