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