Java tutorial
//package com.java2s; //License from project: Apache License import android.app.ActivityManager; import android.content.Context; import java.util.List; public class Main { public static boolean isAppForeground(Context context) { return isAppForeground(context, context.getPackageName()); } public static boolean isAppForeground(Context context, String packageName) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); @SuppressWarnings("deprecation") List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); return tasks != null && !tasks.isEmpty() && tasks.get(0).topActivity.getPackageName().equals(packageName); } }