setClass(Context packageContext,Class<?> cls)
setClassName(Context packageName,String className)
setClassName(String packageName,String className)
setComponent(ComponentName component)
setClass/setClassName:
参数1:被调用组件所在包的Context/包名
参数2:被调用的组件Class/className
setComponent:
参数:被调用的组件
App A中的MainActivity调用SecondActivity
App A中的MainActivity调用App B中的MainActivity
其中:
App A:
com.example.zzz_a.MainActivity
com.example.zzz_a.SecondActivity
App B:
com.example.zzz_b.MainActivity
setClass(Context packageContext,Class<?> cls)
i. App内部调用:
Intent intent = new Intent(); intent.setClass(getApplicationContext(), SecondActivity.class); startActivity(intent);
setClassName(Context packageContext,String className)
i. App内部调用
Intent intent = new Intent(); intent.setClassName(getApplicationContext(), "com.example.zzz_a.SecondActivity"); startActivity(intent);
ii. App间调用
Intent intent = new Intent(); //获取com.example.zzz_b的Context Context context = createPackageContext("com.example.zzz_b", CONTEXT_IGNORE_SECURITY|CONTEXT_INCLUDE_CODE); intent.setClassName(context, "com.example.zzz_b.MainActivity"); startActivity(intent);
setClassName(String packageName,String className)
i. App内部调用:
Intent intent = new Intent(); intent.setClassName("com.example.zzz_a", "com.example.zzz_a.SecondActivity"); startActivity(intent);
ii. App间调用:
Intent intent = new Intent(); intent.setClassName("com.example.zzz_b", "com.example.zzz_b.MainActivity"); startActivity(intent);
setComponent(ComponentName component)
ComponentName对象的实例化有四种方法,分别是:
new ComponentName(parcel);
new ComponentName(Context,Class<?>);
new ComponentName(Context,String);
new ComponentName(String,String);
i. App内部调用:
Intent intent = new Intent(); ComponentName component = new ComponentName("com.example.zzz_a ", "com.example.zzz_a.SecibdActivity"); intent.setComponent(component); startActivity(intent);
ii. App间调用
Intent intent = new Intent(); ComponentName component = new ComponentName("com.example.zzz_b", "com.example.zzz_b.MainActivity"); intent.setComponent(component); startActivity(intent);
或者:
Intent intent = new Intent(); Context context = createPackageContext("com.example.zzz_b", CONTEXT_IGNORE_SECURITY|CONTEXT_INCLUDE_CODE); ComponentName component = new ComponentName(context, "com.example.zzz_b.MainActivity"); intent.setComponent(component); startActivity(intent);
……
上述代码片段就是四种API分别用于App内部/App间的组件调用的具体实现。
注:被调用的第三方App组件在manifest中的exposed属性值必须为true,否则调用会出错。
App内部组件调用:
App组件间调用: