使用Intent启动activity
- setClass(Context pkg, Class<?> cls),设置intent将要启动的组件的包与类
- setClassName(Context pkg, String cls),设置intent将要启动的组件的包与类
- setClassName(String pkg, String cls),设置intent将要启动的组件的包与类
- setComponent(Component comp),设置intent将要启动的组件,Component对象的参数与上面参数一致(均为pkg/cls),显式指定了启动的activity
intent-filter配置
- Aciton是要完成的一个抽象动作(可以自定义)
- Category是标识组件的类别(可自定义,与Action作用类似)
- Uri是指示了数据的匹配与方式
//intent-filter配置(在activity中)
<intent-filter>
<!--指定了该组件匹配的action-->
<action android:name="com.example.action.ACT"/>
<!--指定了该组件匹配的category-->
<category android:name="com.example.category.CAT"/>
<!--必须添加这部分,因为intent启动时会自动为对应组件添加category.DEFAULT的匹配要求-->
<category android:name="android.intent.category.DEFAULT"/>
<!--指定数据Uri的匹配,协议类型为scheme,主机名为host-->
<data android:scheme="com.example" android:host="com.example">
<!--指定数据的匹配类型(所有的图片类型)-->
<data android:mimeType="image/*">
</intent-filter>
//启动对应的组件
Intent intent=new Intent();
//设置对应的动作action
intent.setAction("com.example.action.ACT");
//设置对应的类别category
intent.setCategory("com.example.category.CAT");
//设置数据匹配Uri
//intent.setData(Uri.parse("com.example://com.example/xxx"));
//设置数据类型mimeType,但setType()方法会清除setData()所做的所有操作(无法与setData同时使用)
//intent.setType("image/*");
//同时设置Uri与mimeType(需要同时设置Data与Type时应该使用该方法)
intent.setDataAndType(Uri.parse("com.example://com.example/xxx"), "image/*");