Android学习笔记10---Intent

本文详细介绍了Android中的Intent机制,包括其工作原理、显式与隐式Intent的区别、如何使用Intent来启动Activity及传递数据等核心内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Intent,顾名思义,意图,是不同应用程序之间进行交互,通讯以及各种操作。

http://www.jianshu.com/p/6c39ae8065f7

意图设置动作激活一个新的界面

1.开启目标Activity,会去清单文件中匹配要开启的Activity的意图过滤器的动作。如果匹配成功,开启目标Activity;如果匹配失败,抛出没有找个这个Activity的异常。

2.配置文件中可以写多个意图过滤器,只要匹配成功一个就可以打开Activity

3.设置数据和类型setDataAndType()

        //1.创建意图对象
        Intent intent = new Intent();
        //2.设置动作
        intent.setAction("action");
        //类别不写就是默认的
        intent.addCategory("android.intent.category.DEFAULT");
        //可写可不写(写了的话在清单文件中要写,只写冒号左边的代码)
        intent.setData(Uri.parse("suibian://www.123.com"));
        //intent.setType("www");
        //3.激活意图
        startActivity(intent);
        //数据和类型同时存在的时候,用intent.setDataAndType(URi.parse("suibian://www.123.com"),"www");

清单文件:

            <!-- 一个意图过滤器的里东西必须完全匹配 -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>


隐式意图和显式意图(重点)

1.隐式意图:开启目标activity,回去清单文件中匹配意图过滤器,如果匹配成功,开启activity,如果匹配失败,抛一个找不到这个activity的异常

缺点:效率低,代码书写麻烦

优点:可以开启自己应用中的activity,也可以开启另外一个应用的activity

应用场景:开启其他或者自己应用中的activity

 

2.显示意图(开发常用):开启目标activity,指定目标Activity的字节码,通过反射开启activity

优点:效率高,书写简单

缺点:只能开启自己应用中的activity

应用场景:开启自己应用程序中的activity

编写步骤:

//直接指定类的字节码(this是开启者, SecondActivity是被开启者)

Intent intent = new Intent(this, SecondActivity.class);

startActivity(intent);

//开启自带浏览器

intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");

设计意图的目的

目的:组件之间解耦

作用:开启组件,传递数据

Activity跳转时传递数据

意图传递数据(重点)

一共能传递5种类型的数据

1.Java中八大基本类型及其数组

2.Serializable :把内存中的数据序列化到硬盘里

3.Parcelable :把数据序列化到公共内存

4.bundle  :数据捆,类似HashMap

5.Intent :意图

 

在主Activity设置要传递的数据:

Intent intent = new Intent(this,SecondActivity.class);

//设置要传递的数据

intent.putExtra("name", name);

intent.putExtra("age", age);

startActivity(intent);

在跳转的页面进行接收mainActivity传递过来的数据:

//接收Mainactivity传递过来的数据

Intent intent = getIntent();

String name = intent.getStringExtra("name");

int age = intent.getIntExtra("age", 0);//0是默认值, 如果传递过来的错误就走默认值

System.out.println("name:"+name+" age:"+age);

 

 

传递bundle类型的数据:

Intent intent = new Intent(this,SecondActivity.class);

//数据捆

Bundle bundle = new Bundle();

bundle.putInt("age", age);

bundle.putString("name", name);

bundle.putString("oname", oName);

//把数据困放到intent里去在activity中去传递

intent.putExtra("b", bundle);

 

在跳转的页面接收mainActivity传递过来的数据:

//接受mainActivity传递过来的数据

Intent intent = getIntent();

//拿到意图中的bundle

Bundle bundle = intent.getBundleExtra("b");

String name = bundle.getString("name", "");

int age = bundle.getInt("age", 0);

String oname = bundle.getString("oname");


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值