显式Intent和隐式Intent

本文详细介绍了在Android应用中如何使用Intent进行显式和隐式跳转,并结合AndroidManifest.xml实现Action动作配置。同时,展示了如何在代码中设置Intent的额外参数和组件名称,以及如何在配置文件中定义Activity的Intent过滤器。

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

显式 Intent 调用

 1     //创建一个显式的 Intent 对象(方法一:在构造函数中指定)
 2      Intent intent = new Intent(Intent_Demo1.this, Intent_Demo1_Result1.class);
 3      
 4      Bundle bundle = new Bundle();
 5      bundle.putString("id", strID);
 6      intent.putExtras(bundle);
 7      
 8      intent.putExtra("name", "bbb");
 9      intent.putExtra("userInfo", new UserInfo(1, "name"));
10      startActivity(intent);
11      
12      //创建一个显式的 Intent 对象(方法二:用 setClass 方法)
13      Intent intent = new Intent();
14      Bundle bundle = new Bundle();
15      bundle.putString("id", strID);
16      intent.setClass(Intent_Demo1.this, Intent_Demo1_Result1.class);
17      intent.putExtras(bundle);
18      startActivity(intent);
19      
20      //创建一个显式的 Intent 对象(方法三:用 setClass 方法)
21      Intent intent = new Intent();
22      Bundle bundle = new Bundle();
23      bundle.putString("id", strID);
24      intent.setClassName(Intent_Demo1.this, "com.great.activity_intent.Intent_Demo1_Result1");
25      intent.putExtras(bundle);
26      startActivity(intent);
27      
28      //创建一个显式的 Intent 对象(方法四:用 setComponent 方法)
29      Intent intent = new Intent();
30      Bundle bundle = new Bundle();
31      bundle.putString("id", strID);
32      //setComponent方法的参数:ComponentName
33      intent.setComponent(new ComponentName(Intent_Demo1.this, Intent_Demo1_Result1.class));
34      intent.putExtras(bundle);
35      startActivity(intent);


Intent隐式跳转 Action

 1     //创建一个隐式的 Intent 对象:Action 动作
 2     /**
 3      * 这里指定的是 AndroidManifest.xml 文件中配置的
 4      * <intent-filter>标签中的<action android:name="com.great.activity_intent.Intent_Demo1_Result3" />
 5      * 所在的 Activity,注意这里都要设置 <category android:name="android.intent.category.DEFAULT" />
 6      */
 7     Intent intent = new Intent();
 8     //设置 Intent 的动作
 9     intent.setAction("com.great.activity_intent.Intent_Demo1_Result3");
10     Bundle bundle = new Bundle();
11     bundle.putString("id", strID);
12     intent.putExtras(bundle);
13     startActivity(intent);

AndroidManifest.xml

1    <activity android:name="Intent_Demo1_Result3"
2                   android:label="Intent_Demo1_Result3">
3             <intent-filter>
4                 <action android:name="com.great.activity_intent.Intent_Demo1_Result3" />
5                 <category android:name="android.intent.category.DEFAULT" />
6             </intent-filter>
7         </activity>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值