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

案例代码

相关推荐
千里马学框架2 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台2 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone2 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc2 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo2 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077002 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼2 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone2 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen2 天前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone2 天前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui