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