Java tutorial
//package com.java2s; import java.util.List; import android.app.ActivityManager; import android.app.ActivityManager.RunningAppProcessInfo; import android.content.Context; public class Main { public static boolean isAppOnForeground(Context ctx) { ActivityManager activityManager = (ActivityManager) ctx.getApplicationContext()// .getSystemService(Context.ACTIVITY_SERVICE); String packageName = ctx.getApplicationContext().getPackageName(); List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null) { return false; } for (RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return true; } } return false; } }