1. 对于知道应用包名的情况
PackageManager packageManager = getPackageManager();
Intent intent= packageManager.getLaunchIntentForPackage("package.name");
startActivity(intent);
2. 对于知道应用首页scheme的情况
Intent intent = new Intent();
intent.setData(Uri.parse("scheme://host/path"));
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
此处重要的细节在于设置flag
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK
因为应用可能为了防止多次开启的情况,设置了 isTaskRoot 的判断,对于直接从另一个程序开启而不设置flag的情况,当前应用的首页并不是根activity,导致开启失败:
if (!this.isTaskRoot()) { // 判断当前activity是不是所在任务栈的根
Log.w(TAG, "splash is not task root, should finish it?");
finish();
return;
}
关于首页scheme的设置,直接在首页activity中再增加一个 intent-filter 即可
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="scheme" android:host="host" android:path="/path"/>
</intent-filter>