我们可以通过ComponentName以及Intent的setComponent来实现:在一个应用程序里面启动另外一个已经安装的应用程序或系统程序。
下面是一个在一个应用程序里面启动另外一个已经安装的程序,如下:
ComponentName componentName = new ComponentName("com.example.dcjx",
"com.example.dcjx.MainActivity");
Intent intent = new Intent();
// Bundle bundle = new Bundle();
// bundle.putString("keystring", "阿");
// intent.putExtras(bundle);
intent.setComponent(componentName);
startActivity(intent);
例子:
第一种:从一个应用跳到另一个应用的主activity。
//组件名称,第一个参数是要跳转的应用程序的包名,后一个是这个应用程序的主Activity所在的包名的位置。这两个包名也许是不一致的。
ComponentName componentName = new ComponentName("com.example.dcjx",
"com.example.dcjx.MainActivity");
Intent intent = new Intent();
// Bundle bundle = new Bundle();
// bundle.putString("keystring", "阿");
// intent.putExtras(bundle);
intent.setComponent(componentName);
startActivity(intent);
记住:一定要看好包名和activity名称是否正确,他们两个不一定一致,要跟你的应用的包名和activity名称一一对应。
第二种:从一个应用跳到另一个应用的非主activity,则需要action来指明。
例如A要跳转到B应用的某个activity中
1.在A应用的activity中,指明action
//第一个是应用程序的包名,第二个是activity所在的位置。 他们的包名也许不一样,一个是清单文件里面的入口的哪里的。一个是src文件下的包名哪里的。如果自己不做改变一般是一样的。
ComponentName componentName = new ComponentName(
"com.zhongke.wdzd", "com.zhongke.wdzd.SanBuActivity");
Intent intent = new Intent();
// Bundle bundle = new Bundle();
// bundle.putString("keystring", "阿");
// intent.putExtras(bundle);
intent.putExtra("word", "helpday");
intent.setAction("com.zhongke.wdzd.SanBuActivity");
intent.setComponent(componentName);
startActivity(intent);
2.在B应用的配置文件中,给此activity加上intentFilter,两个标签分别是action和category,这个action的值跟A应用中activity中intent.setAction的内容一致。这样才能匹配。
<activity
android:name="com.zhongke.wdzd.SanBuActivity"
android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="com.zhongke.wdzd.SanBuActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
3.B应用记得改变了清单文件,所以需要重新安装运行。
4.在启动要A应用中调跳转的activity
第三种:从一个应用跳到另一个应用的activity,或者在清单文件里面,在这个activity的标签里面加一句exported=true。那样就不同指明action了。
<activity
android:name="com.goodteacher.kewenxuexi.TongBuJiaoXueActivity"
android:label="@string/app_name"
android:exported="true"
android:screenOrientation="landscape" >
<!-- <intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter> -->
</activity>