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);
注册:
<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);
}
带数据弹框分享: