使用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/*");