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

/**
 * Method checks if the app is in background or not
 *//*from   w  w w  .j  a  va 2s  .  c  om*/
public static boolean isAppIsInBackground(Context context) {
    boolean isInBackground = true;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
        List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
            if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                for (String activeProcess : processInfo.pkgList) {
                    if (activeProcess.equals(context.getPackageName())) {
                        isInBackground = false;
                    }
                }
            }
        }
    } else {
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;
        if (componentInfo.getPackageName().equals(context.getPackageName())) {
            isInBackground = false;
        }
    }

    return isInBackground;
}

From source file:Main.java

/**
 * Checks if is service running.//from w  w  w  .  j  a va  2s  . c  o  m
 *
 * @param ctx the ctx
 * @param theService the the service
 * @return true, if is service running
 */
public static boolean isServiceRunning(Context ctx, Class theService) {
    ActivityManager manager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (theService.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean isRemoteService(Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningAppProcessInfo process : manager.getRunningAppProcesses()) {
        if (VoiceClientSerivceProcessName.equals(process.processName)) {
            return Process.myPid() == process.pid;
        }/*from   www  .j av  a2  s.c  om*/
    }
    return false;
}

From source file:Main.java

public static boolean isAppOnRunning(Context context, String packageName) {
    if (packageName == null || context == null) {
        return false;
    }/* ww w  . ja v  a  2 s.c om*/
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null)
        return false;
    for (RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(packageName)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean isAppOnForeground(Context context, String packageName) {
    if (packageName == null || context == null) {
        return false;
    }/*from w w  w  . ja v a 2  s .  co  m*/
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null) {
        return false;
    }
    for (RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(packageName)
                && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean isForeground(Context context, String className) {
    if (context == null || TextUtils.isEmpty(className)) {
        return false;
    }//w w  w . j a  va  2s  . com

    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
    if (list != null && list.size() > 0) {
        ComponentName cpn = list.get(0).topActivity;
        if (className.equals(cpn.getClassName())) {
            return true;
        }
    }

    return false;
}

From source file:Main.java

/**
 * kill other process and services/* w  w w . ja  v  a  2 s.com*/
 *
 * @param context
 * @return count of clean up process and services
 */
public static int gc(Context context) {
    int count = 0;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> serviceList = am.getRunningServices(100);
    if (serviceList != null) {
        for (ActivityManager.RunningServiceInfo service : serviceList) {
            if (service.pid == android.os.Process.myPid())
                continue;
            try {
                android.os.Process.killProcess(service.pid);
                count++;
            } catch (Exception e) {
                e.getStackTrace();
            }
        }
    }

    List<ActivityManager.RunningAppProcessInfo> processList = am.getRunningAppProcesses();
    if (processList != null) {
        for (ActivityManager.RunningAppProcessInfo process : processList) {
            if (process.importance > ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                String[] pkgList = process.pkgList;
                for (String pkgName : pkgList) {
                    try {
                        am.killBackgroundProcesses(pkgName);
                        count++;
                    } catch (Exception e) {
                        e.getStackTrace();
                    }
                }
            }
        }
    }
    return count;
}

From source file:Main.java

/**
 * Exit current app(clear memory). This util needs
 * `android.permission.KILL_BACKGROUND_PROCESSES` permission.
 *
 * @param context context//ww w. ja va 2 s  .  co  m
 */
public static void exitApp(Context context) {
    if (context == null) {
        return;
    }

    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    manager.killBackgroundProcesses(context.getPackageName());
    System.exit(0);
}

From source file:Main.java

private static float getMemeorySize(Context context) {
    if (Build.VERSION.SDK_INT >= 16) {
        ActivityManager actManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
        actManager.getMemoryInfo(memInfo);
        float totalMemory = (memInfo.totalMem / 1073741824.0f);
        return totalMemory;
    } else {/* w  ww  .  j  ava 2 s . c  o m*/
        return getTotalRAM();
    }
}

From source file:Main.java

public static boolean isNamedProcess(Context context, String processName) {
    if (null == context) {
        return false;
    }//from  www.jav  a 2  s  .c o m

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

    for (RunningAppProcessInfo info : processInfoList) {
        if (pid == info.pid && processName.equals(info.processName)) {
            return true;
        }
    }
    return false;
}