我正在开发一个Android应用程序作为启动程序,它将在不同的窗口中打开另一个应用程序,如任务栏(https://play.google.com/store/apps/details?id=com.farmerbb.taskbar&hl=en)应用程序。
我尝试使用组件名称和反射方法打开另一个应用程序。问题是当新应用程序在另一个窗口中打开时,我的启动器应用程序被杀死。我怎么能停止杀死我的启动器应用程序。
这是在新窗口中启动新应用程序的代码段:
public void launchApplication(String componentName) {
ActivityOptions options = getActivityOptions(AppConstants.FREEFORM_WORKSPACE_STACK_ID);
Intent intent = new Intent();
intent.setComponent(ComponentName.unflattenFromString(componentName));
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
try {
startActivity(intent, options.toBundle());
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
public ActivityOptions getActivityOptions(int applicationType) {
ActivityOptions options = ActivityOptions.makeBasic();
Integer stackId = null;
switch (applicationType) {
case FULLSCREEN_WORKSPACE_STACK_ID:
stackId = FULLSCREEN_WORKSPACE_STACK_ID;
break;
case FREEFORM_WORKSPACE_STACK_ID:
stackId = FREEFORM_WORKSPACE_STACK_ID;
break;
case DOCKED_STACK_ID:
stackId = DOCKED_STACK_ID;
break;
}
if (stackId != null) {
try {
Method method = ActivityOptions.class.getMethod("setLaunchStackId", int.class);
method.invoke(options, stackId);
} catch (Exception e) {
e.printStackTrace();
}
}
return options;
}