判断一个Activity在当前激活状态

// 需要添加android.permission.GET_TASKS权限
protected static boolean isTopActivity(Activity activity) {
String packageName = "com.su.testalldialog";
ActivityManager activityManager = (ActivityManager) activity
.getSystemService(Context.ACTIVITY_SERVICE);
List tasksInfo = activityManager.getRunningTasks(1);
if (tasksInfo.size() > 0) {
System.out.println("---------------包名-----------"
+ tasksInfo.get(0).topActivity.getPackageName());
// 应用程序位于堆栈的顶层
if (packageName.equals(tasksInfo.get(0).topActivity
.getPackageName())) {
return true;
}
}
return false;
}

你可能感兴趣的:(判断一个Activity在当前激活状态)