Android --- 使用Intent传递对象的方式

本文详细介绍了Android中Intent机制的使用方法,包括如何通过组件名称启动应用内外的Activity,如何使用Action和Category来指定启动行为,以及如何配置Data和Type来传递特定类型的数据。

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

Intent 内容

ComponentName, 用来描述 Intent 中目标组件名

应用内部
Intent it = new Intent();
// 组件信息
//ComponentName cn = new ComponentName(this,Activity01.class);
ComponentName cn = new ComponentName(this,"com.xykj.intentandbradcast.Activity01");
it.setComponent(cn);
startActivity(it);

跨应用
Intent it1 = new Intent();
// 组件信息 ( 应用包名 , 组件名 )
ComponentName cn1 = new ComponentName("com.xykj.mediaplayerdemo","com.xykj.mediaplayerdemo.MainActivity");
it1.setComponent(cn1);
startActivity(it1);


Action  以及 Category , action 表示目标组件的启动方式, category 表示启动的类别


注册:
<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
自定义的 Action
<intent-filter>
    <action android:name="abc123"/>
    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
使用
Intent it2 = new Intent();
// 设置 Action 信息
it2.setAction("abc123");
startActivity(it2);

Data  和 Type 其中 Data 表示启动的 Intent 携带的 Uri 数据, Type 表示该数据的 mime 类型


注册
<intent-filter>
    ...
    <data android:mimeType="text/*"/>
    <data android:mimeType="image/*" />
    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

使用
Intent it4 = new Intent(Intent.ACTION_SEND);
// 配置数据和类型
//it4.setType("text/plain");
//it4.putExtra(Intent.EXTRA_TEXT,"Hello abc");
File image = new File(Environment.getExternalStorageDirectory().toString()+"/pic.jpg");
Uri uri = Uri.fromFile(image);
it4.setDataAndType(uri,"image/jpg");
startActivity(Intent.createChooser(it4," 发送到 "));

目标组件解析数据
Intent it = getIntent();
Uri uri = it.getData();
if(null != uri){
    iv.setImageURI(uri);
}


带数据弹框分享:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值