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 isServiceRunning(Class<? extends Service> service, Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo runningServiceInfo : manager
            .getRunningServices(Integer.MAX_VALUE)) {
        if (service.getName().equals(runningServiceInfo.service.getClassName())) {
            return true;
        }// w  w  w.  j  ava2  s  .  c  o m
    }
    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);//www . jav a2 s. co 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:Main.java

public static boolean isServiceRunning(Context mContext, String className) {
    boolean isRunning = false;
    ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> serviceList = activityManager
            .getRunningServices(Integer.MAX_VALUE);
    if (serviceList.size() == 0) {
        return false;
    }/*  w  w w  .  ja  v  a  2s  . com*/
    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 String getTopActivityClassName(Context context) {
    try {// ww w . j av  a 2 s .  c  o  m
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        return cn.getClassName();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static boolean isServiceRunning(Context mContext, String className) {
    boolean isRunning = false;
    android.app.ActivityManager activityManager = (android.app.ActivityManager) mContext
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<android.app.ActivityManager.RunningServiceInfo> serviceList = activityManager
            .getRunningServices(Integer.MAX_VALUE);
    if (serviceList.size() == 0) {
        return false;
    }//w  w w .j ava 2  s .c o  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

/**
 * @return null may be returned if the specified process not found
 *///from  w  w  w  . ja  v  a 2 s  .  com
public static String getProcessName(Context cxt, int pid) {
    ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
    if (runningApps == null) {
        return null;
    }
    for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
        if (procInfo.pid == pid) {
            return procInfo.processName;
        }
    }
    return null;
}

From source file:Main.java

public static long getAvailableMemory(Context context) {
    ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    activityManager.getMemoryInfo(mi);//from  w  ww  . j a va  2 s  .  c om
    return mi.availMem;
}

From source file:Main.java

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

From source file:Main.java

public static boolean isBackgroundRunning(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }//from   ww w. j  a v  a2 s.  c om
    }
    return false;
}

From source file:Main.java

public static void releaseMemory(Context context) {
    ActivityManager activityManger = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> list = activityManger.getRunningAppProcesses();
    if (list != null) {
        for (int i = 0; i < list.size(); i++) {
            ActivityManager.RunningAppProcessInfo apinfo = list.get(i);
            String[] pkgList = apinfo.pkgList;
            if (apinfo.importance > ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE
                    && !apinfo.processName.equals("com.tt.realeasememory")) {
                for (int j = 0; j < pkgList.length; j++) {
                    if (Build.VERSION.SDK_INT >= 8) {
                        activityManger.killBackgroundProcesses(pkgList[j]);
                    } else {
                        activityManger.restartPackage(pkgList[j]);
                    }// w  ww  .  j a va  2  s  . c  o m
                }
            }
        }
    }
}