1. 通过包名启动APP
启动方式,只需知道目标APP包名即可
例如:app1 跳转到app2
app2包名为:com.demo.skipdemo
app1启动代码如下:
//通过目标APP的包名创建intent对象
Intent intent = getPackageManager().getLaunchIntentForPackage("com.demo.skipdemo");
// 这里如果intent为空,就说名没有安装要跳转的应用嘛
if (intent != null) {
//传参方式与Activity间跳转相同
intent.putExtra("name", "Liu xiang");
intent.putExtra("birthday", "1983-7-13");
startActivity(intent);
} else {
//没有安装目标app应用
}
该方式只能跳转到目标APP的启动页。
2.启动APP的任意页
该方法启动时,需要在目标APP的manifest中想要启动的页面添加intent-filter。
例如:app1 跳转到app2的TestActivity
app2的manifest中目标activity添加intent-filter如下:
<activity android:name=".TestActivity" android:exported="true" > <intent-filter> <action android:name="skip" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
注*:exported要设置为true,action的name要记住,启动时标志即为这个。
app1的启动代码如下:
// 标志(要和目标APP的清单文件中的标志一样) Intent intent = new Intent("skip"); intent.putExtra("name", "Liu xiang"); intent.putExtra("birthday", "1983-7-13"); startActivity(intent);
总结:两种方式都可以实现APP间跳转,第一种只可跳转至app2的启动页,但app2无需任何设置;第二种可跳转至任何页,但需要app2中进行相应设置。两种方式的传参方式都与普通activity传参一致,同时也可返回参数。