- protected static boolean isTopActivity(Activity activity){
String packageName = "xxxxx";
ActivityManager activityManager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> 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;
} - 需要添加android.permission.GET_TASKS权限
android 如何判断程序是否在前台运行
最新推荐文章于 2021-05-26 02:43:10 发布