is Application Brought To Background - Android android.content.pm

Android examples for android.content.pm:App

Description

is Application Brought To Background

Demo Code

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;

public class Main {

  public static boolean isApplicationBroughtToBackground(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (tasks != null && !tasks.isEmpty()) {
      ComponentName topActivity = tasks.get(0).topActivity;
      if (!topActivity.getPackageName().equals(context.getPackageName())) {
        return true;
      }/*from w ww . jav  a  2  s .  c  o  m*/
    }
    return false;
  }

}

Related Tutorials