Android Context Check isAppOnForeground(Context context)

Here you can find the source of isAppOnForeground(Context context)

Description

is App On Foreground

Declaration

public static boolean isAppOnForeground(Context context) 

Method Source Code

//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 context) {
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        // Returns a list of application processes that are running on the device 
        List<RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        if (appProcesses == null)
            return false;
        for (RunningAppProcessInfo appProcess : appProcesses) {
            // importance: 
            // The relative importance level that the system places  
            // on this process. 
            // May be one of IMPORTANCE_FOREGROUND, IMPORTANCE_VISIBLE,  
            // IMPORTANCE_SERVICE, IMPORTANCE_BACKGROUND, or IMPORTANCE_EMPTY. 
            // These constants are numbered so that "more important" values are 
            // always smaller than "less important" values. 
            // processName: 
            // The name of the process that this object is associated with. 
            if (appProcess.processName.equals(context.getPackageName())
                    && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            }/*from   www.j  a  va 2 s.c om*/
        }
        return false;
    }
}

Related

  1. hasVibrationPermission(Context context)
  2. is3G(Context context)
  3. isAccessibilityEnabled(Context context, String id)
  4. isAppInstalled(Context context)
  5. isAppInstalled(Context context, String uri)
  6. isApplicationBroughtToBackground( final Context context)
  7. isApplicationBroughtToBackground( final Context context)
  8. isCallable(Context context, String url)
  9. isConnected(Context connext)