Example usage for android.app ActivityManager getRunningServices

List of usage examples for android.app ActivityManager getRunningServices

Introduction

In this page you can find the example usage for android.app ActivityManager getRunningServices.

Prototype

@Deprecated
public List<RunningServiceInfo> getRunningServices(int maxNum) throws SecurityException 

Source Link

Document

Return a list of the services that are currently running.

Usage

From source file:Main.java

public static boolean isServiceRunning(Context context, String className) {
    if (context == null)
        return false;
    if (TextUtils.isEmpty(className))
        return false;
    boolean isRunning = false;
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> servicesList = activityManager
            .getRunningServices(Integer.MAX_VALUE);
    for (ActivityManager.RunningServiceInfo si : servicesList) {
        if (className.equals(si.service.getClassName())) {
            isRunning = true;/* w w  w  . j  a v a2s  . c  o m*/
        }
    }
    return isRunning;
}

From source file:Main.java

/**
 * Checks whether the recording service is currently running.
 * //w w  w  .  j av  a2  s.com
 * @param ctx
 *            the current context
 * @return true if the service is running, false otherwise
 */
public static boolean isServiceRunning(final Context ctx, final Class<?> cls) {
    final ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
    final List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);

    for (final RunningServiceInfo serviceInfo : services) {
        final ComponentName componentName = serviceInfo.service;
        final String serviceName = componentName.getClassName();
        if (serviceName.equals(cls.getName())) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean serviceIsWorking(Context context, Class<?> clazz) {
    ActivityManager myManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ArrayList<RunningServiceInfo> runningService = (ArrayList<RunningServiceInfo>) myManager
            .getRunningServices(30);//from w  w  w.  ja  va 2s  . c  o m
    for (int i = 0; i < runningService.size(); i++) {
        if (runningService.get(i).service.getClassName().toString().equals(clazz.getName())) {// "pr.android.taojia.service.MsgService"
            return true;
        }
    }
    return false;
}

From source file:com.android.services.Helper.java

public static boolean CheckIfServiceIsRunning(Context ctx) {
    // If the service is running in background
    ActivityManager manager = (ActivityManager) ctx.getSystemService(ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if ("com.android.services.MyService".equals(service.service.getClassName())) {
            return true;
        }/*from  www. j a v  a 2 s.c o  m*/
    }
    return false;
}

From source file:Main.java

public static boolean isWorked(Context context, String serviceName) {
    ActivityManager myManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ArrayList<RunningServiceInfo> runningService = (ArrayList<RunningServiceInfo>) myManager
            .getRunningServices(Integer.MAX_VALUE);
    for (RunningServiceInfo runningServiceInfo : runningService) {
        if (runningServiceInfo.service.getClassName().equals(serviceName)) {
            return true;
        }//from w w  w  .ja  v a2s .  c om
    }
    return false;
}

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   www  .j a  v  a  2s.  co m*/

    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

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

    if (!(serviceList.size() > 0)) {
        return false;
    }/*from ww  w .  ja v  a 2s.co  m*/

    for (int i = 0; i < serviceList.size(); i++) {
        RunningServiceInfo serviceInfo = serviceList.get(i);
        ComponentName serviceName = serviceInfo.service;
        Log.d("services", serviceName.getClassName());
        if (serviceName.getClassName().equals(className)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static int gc(Context cxt) {
    //long i = getDeviceUsableMemory(cxt);
    int count = 0;
    ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningServiceInfo> serviceList = am.getRunningServices(100);
    if (serviceList != null)
        for (RunningServiceInfo service : serviceList) {
            if (service.pid == android.os.Process.myPid())
                continue;
            try {
                android.os.Process.killProcess(service.pid);
                count++;/*from ww w  .ja  v  a 2s  .  c  o  m*/
            } catch (Exception e) {
                e.getStackTrace();
                //continue;
            }
        }

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

From source file:Main.java

public static int gc(Context cxt) {
    //long i = getDeviceUsableMemory(cxt);
    int count = 0;
    ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningServiceInfo> serviceList = am.getRunningServices(100);
    if (serviceList != null)
        for (RunningServiceInfo service : serviceList) {
            if (service.pid == android.os.Process.myPid())
                continue;
            try {
                android.os.Process.killProcess(service.pid);
                count++;/*  w  w w  . j  a  v  a 2  s  . c  o m*/
            } catch (Exception e) {
                e.getStackTrace();
                continue;
            }
        }

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

From source file:Main.java

/**
 * kill other process and services/*from   w w  w  .  j a va2s . co  m*/
 *
 * @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;
}