网上各种办法,最后经过测试,在栈中你能获取到的只有baseActivity和topActivity,如果你需要判断的Activity在栈别的位置,那么根本判断不了,因为返回的只有栈中Activity的个数,并没有Activity的名字。
private boolean isExistMainActivity(Class<?> activity) {
Intent intent = new Intent(this, activity);
ComponentName cmpName = intent.resolveActivity(getPackageManager());
boolean flag = false;
if (cmpName != null) { // 说明系统中存在这个activity
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfoList =am.getRunningTasks(10);//
这里获取的是APP栈的数量,一般也就两个
ActivityManager.RunningTaskInfo runningTaskInfo = taskInfoList.get(0);// 只是拿 当前运行的栈
int numActivities = taskInfoList.get(0).numActivities;
for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
if (taskInfo.baseActivity.equals(cmpName)) {// 说明它已经启动了
flag = true;
break;//跳出循环,优化效率
}
}
}
return flag;
}