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 getAvailMemory(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    MemoryInfo mi = new MemoryInfo();
    am.getMemoryInfo(mi);//w w w  . j  av a  2s . co m
    return Formatter.formatFileSize(context, mi.availMem);
}

From source file:Main.java

public static boolean isApplicationBackground(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;
        }// w w  w.  j a  v  a 2s.  com
    }
    return false;
}

From source file:Main.java

public static boolean isStart(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(100);
    if (tasks == null || tasks.size() == 0)
        return false;
    String name = "com.mid.market.service.MarketService";
    for (RunningTaskInfo task : tasks) {
        if (task.baseActivity.getClassName().equals(name)) {
            return true;
        }/*from  ww  w .  j av a2  s  . c o m*/
    }
    return false;
}

From source file:Main.java

public static boolean isServiceRunning(Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        Log.d("InformatonUtils", "Service Name = " + service.service.getClassName());
        if ("com.safecell.TrackingService".equals(service.service.getClassName())) {
            return true;
        }/*from  w w  w.jav a2  s.c o  m*/
    }
    return false;
}

From source file:Main.java

public static boolean isBackground(Context context) {

    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();

    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(context.getPackageName())) {
            if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
                return true;
            } else {
                return false;
            }/*from  ww w. ja v a 2  s.co  m*/
        }
    }
    return false;
}

From source file:Main.java

public static boolean isProessRunning(Context context, String proessName) {
    boolean isRunning = false;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    List<ActivityManager.RunningAppProcessInfo> lists = am.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo info : lists) {
        if (info.processName.equals(proessName)) {
            isRunning = true;/*  w  ww  .  ja  va  2  s . c  om*/
            return isRunning;
        }
    }

    return isRunning;
}

From source file:Main.java

public static void killProcesses(Context context, int pid, String processName) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    String packageName;//from  w w  w .  j a va2s .co  m
    try {
        if (!processName.contains(":")) {
            packageName = processName;
        } else {
            packageName = processName.split(":")[0];
        }
        activityManager.killBackgroundProcesses(packageName);
        Method forceStopPackage = activityManager.getClass().getDeclaredMethod("forceStopPackage",
                String.class);
        forceStopPackage.setAccessible(true);
        forceStopPackage.invoke(activityManager, packageName);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean isMainProcess(Context context) {
    ActivityManager am = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE));
    List<RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
    String mainProcessName = context.getPackageName();
    int myPid = android.os.Process.myPid();
    for (RunningAppProcessInfo info : processInfos) {
        if (info.pid == myPid && mainProcessName.equals(info.processName)) {
            return true;
        }/*from   ww w. j  av  a 2s.  c o  m*/
    }
    return false;
}

From source file:Main.java

public static boolean isTopActivy(Context context, String activityName) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ComponentName cName = am.getRunningTasks(1).size() > 0 ? am.getRunningTasks(1).get(0).topActivity : null;

    if (null == cName)
        return false;
    return cName.getClassName().equals(activityName);
}

From source file:Main.java

public static boolean isServiceRun(Context context, Class<?> clazz) {
    boolean isRun = false;
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> serviceList = activityManager
            .getRunningServices(Integer.MAX_VALUE);
    int size = serviceList.size();
    for (int i = 0; i < size; i++) {
        if (serviceList.get(i).service.getClassName().equals(clazz.getName())) {
            isRun = true;//ww w  .  ja va 2  s  .c  o  m
            break;
        }
    }
    return isRun;
}