Example usage for android.content Context ACTIVITY_SERVICE

List of usage examples for android.content Context ACTIVITY_SERVICE

Introduction

In this page you can find the example usage for android.content Context ACTIVITY_SERVICE.

Prototype

String ACTIVITY_SERVICE

To view the source code for android.content Context ACTIVITY_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.app.ActivityManager for interacting with the global system state.

Usage

From source file:Main.java

public static boolean isServiceRunning(Context context, String className) {
    boolean isRunning = false;
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> serviceList = activityManager
            .getRunningServices(Integer.MAX_VALUE);

    if (serviceList.size() <= 0) {
        return false;
    }/*from w  w w  .j a  va2 s  .  c  om*/

    for (int i = 0; i < serviceList.size(); i++) {
        if (serviceList.get(i).service.getClassName().equals(className) == true) {
            isRunning = true;
            break;
        }
    }

    return isRunning;
}

From source file:Main.java

/**
 * Checks if this application is foreground
 * //from w  w  w .  j a  va  2  s . c om
 * @param context Context to be examined
 * @return true if this application is running on the top; false otherwise
 */
public static boolean isContextForeground(Context context) {

    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    int pid = getPid();
    for (RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.pid == pid) {
            return appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
        }
    }
    return false;
}

From source file:Main.java

/**
 * To check if the specified service is running.
 * This util needs `android.permission.GET_TASKS` permission.
 *
 * @param  context   the context//from  www .  java 2s  . c  o  m
 * @param  className class name of service
 * @return           true if the service is running
 */
public static boolean isServiceRunning(Context context, String className) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo info : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (info.service.getClassName().equals(className)) {
            return true;
        }
    }

    return false;
}

From source file:Main.java

public static boolean isNamedProcess(Context context, String processName) {
    if (context == null || TextUtils.isEmpty(processName)) {
        return false;
    }//  w  w w  .j  a  v  a2  s . c  om

    int pid = android.os.Process.myPid();
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> processInfoList = manager.getRunningAppProcesses();
    if (processInfoList == null) {
        return true;
    }

    for (RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
        if (processInfo.pid == pid && processName.equalsIgnoreCase(processInfo.processName)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * whether this process is named with processName
 *//*from  www.  j a va2 s  . c om*/
public static boolean isNamedProcess(Context context, String processName) {
    if (context == null || TextUtils.isEmpty(processName)) {
        return false;
    }

    int pid = android.os.Process.myPid();
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> processInfoList = manager.getRunningAppProcesses();
    if (processInfoList != null) {
        for (ActivityManager.RunningAppProcessInfo processInfo : processInfoList) {
            if (processInfo.pid == pid && processName.equalsIgnoreCase(processInfo.processName)) {
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

/**
 * is out app running in background ?/*from   w  w  w.j a  v a 2 s .  c  o  m*/
 * 
 * when system reboot, this method will return false although our app is not already running, so is this a bug?
 * 
 * @param context
 * @return true if in background; false if not.
 */
public static boolean isInBackground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    for (RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(context.getPackageName())) {
            if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND
                    || appProcess.importance == RunningAppProcessInfo.IMPORTANCE_SERVICE) {
                return true;
            } else {
                return false;
            }
        }
    }
    return false;
}

From source file:Main.java

public static String getProcessName(Context cxt, int pid) {
    ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
    if (runningApps == null) {
        return null;
    }/*from  ww  w . j ava2s . c om*/
    for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
        if (procInfo.pid == pid) {
            return procInfo.processName;
        }
    }
    return null;
}

From source file:Main.java

/**
 * get total size of ram. Work for API 16 and above.
 * @return total size in byte/*from   w ww .j a v  a  2s.  c om*/
 */
public static long getTotalRamSize(Context context) {
    if (am == null) {
        am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    }
    am.getMemoryInfo(outInfo);
    return outInfo.totalMem;
}

From source file:Main.java

/**
 * Used to know if Apollo was sent into the background
 * //from  w  w w .ja  v  a  2s.c om
 * @param context The {@link Context} to use
 */
public static final boolean isApplicationSentToBackground(final Context context) {
    final ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    final List<RunningTaskInfo> tasks = activityManager.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        final ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * Returns the Memory Class of the App or Context provided.
 * <br><br>/*from   w  ww . j ava 2  s  .  c  o m*/
 * Memory Class defines the application's maximum heap size. They are based on the
 * device's overall memory. The values are in base of 16. You may use
 * it to manage your application's memory properly.
 * <br><br>
 * Example Classes: 16, 24, 48, 64, 80, 96, 112, 128 etc...
 */
public static int getMemoryClass(Context context) {

    if (context == null)
        return -1;

    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    return am.getMemoryClass();
}