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 isRunningServer(String className, Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningServiceInfo> runningServices = activityManager.getRunningServices(1000);
    for (RunningServiceInfo runningServiceInfo : runningServices) {
        ComponentName service = runningServiceInfo.service;
        String className2 = service.getClassName();
        if (className2.equals(className)) {
            return true;
        }/*from w w  w . j av  a2s .  c  om*/
    }

    return false;
}

From source file:Main.java

public static ActivityManager getManager(Context context) {
    return (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
}

From source file:Main.java

public static boolean isServiceRunning(Context context, String className) {
    if (context == null) {
        return false;
    }//from   ww  w. j  a v  a  2s.c  o m

    boolean isRunning = false;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ;
    List<ActivityManager.RunningServiceInfo> lists = am.getRunningServices(1000);
    for (ActivityManager.RunningServiceInfo info : lists) {
        if (info.service.getClassName().equals(className)
                && info.service.getPackageName().equals(context.getPackageName())) {
            isRunning = true;
            break;
        }
    }

    return isRunning;
}

From source file:Main.java

public static String getTopPackageName(Context context) {
    String topPackageName = null;
    try {/* www.j av a  2 s .  co m*/
        ActivityManager activityManager = (ActivityManager) (context
                .getSystemService(android.content.Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1);
        if (runningTaskInfos != null) {
            ComponentName f = runningTaskInfos.get(0).topActivity;
            topPackageName = f.getPackageName();
        }
    } catch (Exception e) {
    }
    return topPackageName;
}

From source file:Main.java

public static final String getTopAppPackageName(final Context context) {
    String packageName = null;//w  w  w  .  jav  a 2  s  .  co m
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> list = am.getRunningTasks(1);
    ComponentName comp = list.get(0).topActivity;
    if (null != comp) {
        packageName = comp.getPackageName();
    }
    return packageName;
}

From source file:Main.java

public static boolean isAppForeground(Context context, String packageName) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    @SuppressWarnings("deprecation")
    List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
    return tasks != null && !tasks.isEmpty() && tasks.get(0).topActivity.getPackageName().equals(packageName);
}

From source file:Main.java

public static boolean isServiceRunning(Context context, String className) {
    boolean isServiceFound = false;

    try {//from w w w.j  a v a2  s . c om
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
        for (int i = 0; i < services.size(); i++) {
            if (services.get(i).topActivity.getClassName().equalsIgnoreCase(className)) {
                isServiceFound = true;
            }
        }
    } catch (Exception ex) {
    }

    return isServiceFound;
}

From source file:Main.java

public static boolean isAppRunning(Context context, String packageName) {
    if (packageName == null)
        return false;
    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;
        }//from   www .  j a v  a2 s .co m
    }
    return false;
}

From source file:Main.java

public static boolean isApplicationToBackground(final Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }/*from   w w w  . ja va2 s . co  m*/
    }
    return false;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static long getTotalMemoryJB(Context context) {
    ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    activityManager.getMemoryInfo(mi);//from   w  ww  . ja  va  2 s.co  m
    return mi.totalMem;
}