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 String getTopActivity(Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);

    if (runningTaskInfos != null)
        return runningTaskInfos.get(0).topActivity.getClassName();
    else/* w ww.j  a v  a  2s  .co m*/
        return "";
}

From source file:Main.java

public static boolean isServiceRunning(Class<?> serviceClass, Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }// w  ww .  j  av a2s  .co m
    }
    return false;
}

From source file:Main.java

public static boolean isMyServiceRunning(Context context, Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName()))
            return true;
    }/*w w  w  . j  a  va 2  s  . c o m*/
    return false;
}

From source file:Main.java

public static boolean isServiceRunning(Context context, Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }/*from  w w  w  . ja  v  a  2  s.c om*/
    }
    return false;
}

From source file:Main.java

public static String getAvailMemory(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
    am.getMemoryInfo(mi);//from   w ww . ja va  2s .  c o m
    System.out.println("availMem;" + mi.availMem);
    return Formatter.formatFileSize(context, mi.availMem);
}

From source file:Main.java

public static String getTopPackage(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    String ret = "";
    try {/*from w  ww  . j a v a  2 s .c  o m*/
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        ret = cn.getPackageName();
    } catch (Exception e) {

    }
    return ret;
}

From source file:Main.java

public static void shutDownPck(Context context, String pckName) {
    if (context != null) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        am.killBackgroundProcesses(pckName);
    }// w  w  w  .jav  a2 s . co  m
}

From source file:Main.java

public static boolean isApplicationForeground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
    for (ActivityManager.RunningTaskInfo info : services) {
        if (info.topActivity.getPackageName().equalsIgnoreCase(context.getPackageName())) {
            return true;
        }//w  ww  .j  ava  2s .  c om
    }
    return false;
}

From source file:Main.java

public static String getCurrentActivityPkgName(Context context) {

    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
    // String className=cn.getClassName();
    return cn.getPackageName();
}

From source file:Main.java

public static boolean isAppInBackground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null)
        return true;
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(context.getPackageName())) {
            return appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
        }//from www .  j  a va 2 s .com
    }
    return false;
}