Intent总结01 Intent的结构
Intent对象由component、Action、Data、Category、Component和Extra等属性组成组成。下面,分别对各个属性进行介绍说明。
1 Component
明确指定类名,用于跳转。通过setComponent(ComponentName)或setClass(Context, Class)等设置Intent的component(即组成成分),来指定一个明确的类进行跳转。通过设置component的intent是显式意图,即Explicit Intents;其余的都是隐式意图,Implicit Intents。
2 Action
规定了Intent要完成的动作,是一个字符串常量。可以通过setAction()来设置Action属性,getAction()来获得Action属性。使用Action时,我们既可以使用系统内置的Action,也可以自己定义。系统自定义的action,如ACTION_VIEW, ACTION_EDIT, ACTION_MAIN等等。
3 Data
设置intent的数据,该数据一般用Uri封装。
下面,介绍几组 action/data的示例:
ACTION_VIEW content://contacts/people/1 -- 进入联系人查看界面,并显式id为1的联系人的信息。
ACTION_EDIT content://contacts/people/1 --进入联系人编辑界面,并编辑id为1的联系人的信息。
ACTION_DIAL content://contacts/people/1 --进入拨号程序,并输入id为1的联系人的号码。
4 Type
明确指定了要操作的intent数据类型。通常在data中就暗含了type类型。通过设置intent的type,我们能过滤掉type不同的intent。如mp4对应的type是"video/mp4",mp3的type为“audio/x-mpeg”。
5 Extras
设置intent的附加数据,通常用来传递除“data”和“type”之外的额外内容。例如:boolean、int等基本类型数据或它们对应的数组、bundle对象、以及parcelable或seriable对象。parcelable或seriable对象可以参考”Android Bundle总结“。
6 Category
附加到action上的额外信息。例如“android.intent.category.LAUNCHER”表示将该activity的图标在主界面的程序列表中显示,“android.intent.category.DEFAULT”是通过隐式intents来查找该类的必备属性。
总体来说:
(01)若只是单纯的跳转到目标类,而不传递任何数据:可以通过设置component进行显示跳转;亦可以只设置action进行隐式跳转。
如通过显式意图跳转,则直接在调用时通过API来进行component的设置。参考”Intent总结02 component和显式意图“
如通过隐式意图跳转,则先在目标类对应的AndroidManifest.xml中注册对应的action,然后在调用时通过API设置ation进行跳转。参考"Intent总结02 component和显式意图"
(02)data、type、extra主要用于传递额外的参数。。
它们之间的区别:
data主要用来传递Uri数据,例如“http地址”、“拨打联系人的号码”、“SQL语句”等;关于Uri的详细说明,可以参考“Intent总结04 Data和Type属性”。
type主要是用来系统data的类型,如查找数据时,是查找集合,还是查找某条记录;如,不同格式的音乐对应的type也不同。参考"Intent总结04 Data和Type属性"。
extra主要用来传递boolean、byte、int、string等基本类型或它们的数组,bundle对象,parcelable或seriable对象。参考"Intent总结05 extra属性"。
(03)category:附加的额外信息。例如,若要通过隐式意图查找某activity,则在该activity对应的AndroidManifest.xml中注册intent-filter时,必须加上“android.intent.category.DEFAULT”。参考"Intent总结06 category属性"。
component和显式意图
1 介绍
显式意图,即Explicit Intents。显式意图通过component明确指定类名,用于跳转。通常用到的API有setComponent(ComponentName)或setClass(Context, Class)等。
常用的显示意图跳转方法说明 方法01:通过构造函数Intent(ContextpackageContext, Class<?> cls),直接指定目的类。方法02:通过setClass (Context packageContext,Class<?> cls)指定目的类
- //jump Method - 01 : Intent(Context packageContext, Class<?> cls)
- Intentintent = new Intent(Activity01.this, Activity02.class);
- startActivity(intent);
方法03:通过setClassName (String packageName,String className)指定目的类
- //jump Method - 02 : setClass (Context packageContext, Class<?> cls)
- Intentintent = new Intent().setClass(Activity01.this, Activity02.class);
方法04:通过setComponent(ComponentNamecomponent)
- //jump Method - 03 : setClassName (String packageName, String className)
- Intentintent = new Intent().setClassName("com.test","com.test.Activity02");
- startActivity(intent);
- // jump Method - 04 :setComponent(ComponentName component)
- ComponentName cn = new ComponentName("com.test","com.test.ExplicitDestination");
- Intent intent = new Intent();
- intent.setComponent(cn);
- startActivity(intent);
Intent总结03Action属性
Action :规定了Intent要完成的动作,是一个字符串常量。使用setAction()来设置Action属性,使用getAction()来获得Action属性。既可以使用系统内置的Action,也可以自己定义。系统自定义的action,如ACTION_VIEW, ACTION_EDIT, ACTION_MAIN等等。
1.自定义Action
在“目的Activity”的AndroidManifest.xml中指定action常量。
- <activity android:name=".ActionDestination">
- <intent-filter>
- <action android:name="Skywang_ACTION" />
- <category android:name="android.intent.category.DEFAULT"/>
- </intent-filter>
- </activity>
<categoryandroid:name="android.intent.category.DEFAULT" />的作用是用来说明,可以通过隐式跳转(即其它类调用setAction("Skywang_ACTION"))来找到ActionDestination这个activity。这样,其它的类就可以通过下面的代码跳转到ActionDestination。跳转时,setAction的字符串"Skywang_ACTION"必须与AndroidManifest.xml中定义的"Skywang_ACTION"一致。
- Intent intent = new Intent();
- intent.setAction("Skywang_ACTION");
- startActivity(intent);
2系统Action
- // 流量网页
- Uri uri =Uri.parse("http://www.baidu.com");
- Intent intent = newIntent(Intent.ACTION_VIEW, uri);
- startActivity(intent);<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
- // 拨打电话
- // if you want to use ACTION_DIAL, you mustadd permissin in manifest, the permission is bellow
- // <uses-permissionandroid:name="android.permission.CALL_PHONE" />
- Uri uri = Uri.parse("tel:12580");
- Intent it = new Intent(Intent.ACTION_DIAL,uri);
- startActivity(it);
- // 发送短信
- Uri uri = Uri.parse("smsto:13410177756");
- Intent it = newIntent(Intent.ACTION_SENDTO, uri);
- it.putExtra("sms_body", "TheSMS text");
- startActivity(it);
- //播放mp3
- Intent it = new Intent(Intent.ACTION_VIEW);
- Uri uri =Uri.parse("file:///sdcard/song.mp3");
- it.setDataAndType(uri, "audio/mp3");
- startActivity(it);