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(Context context, String className) {
    boolean isRunning = false;
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningServiceInfo> servicesList = activityManager.getRunningServices(Integer.MAX_VALUE);
    Iterator<RunningServiceInfo> l = servicesList.iterator();
    while (l.hasNext()) {
        RunningServiceInfo si = l.next();
        if (className.equals(si.service.getClassName())) {
            isRunning = true;//from   w w w .j av  a  2s .  c  o m
        }
    }
    return isRunning;
}

From source file:Main.java

/**
 * Judge whether an app is in background
 *
 * @param context/*from w w w  . ja va 2 s .c  om*/
 * @return
 */
public static boolean isAppInBackground(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskList = am.getRunningTasks(1);
    if (taskList != null && !taskList.isEmpty()) {
        ComponentName topActivity = taskList.get(0).topActivity;
        if (topActivity != null && !topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

private static String getTopActivityName(Context context) {
    String topActivityClassName = null;
    ActivityManager activityManager = (ActivityManager) (context
            .getSystemService(android.content.Context.ACTIVITY_SERVICE));
    List<RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1);
    if (runningTaskInfos != null) {
        ComponentName f = runningTaskInfos.get(0).topActivity;
        topActivityClassName = f.getClassName();
    }/*from  w  w  w . j a v  a 2s. c  o m*/
    return topActivityClassName;
}

From source file:Main.java

public static boolean isServiceRunning(Context ctx, String className) {
    boolean isRunning = false;
    ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningServiceInfo> servicesList = activityManager.getRunningServices(Integer.MAX_VALUE);
    Iterator<RunningServiceInfo> l = servicesList.iterator();
    while (l.hasNext()) {
        RunningServiceInfo si = (RunningServiceInfo) l.next();
        String serName = si.service.getClassName();
        Log.d("isServiceRunning", serName);
        if (className.equals(serName)) {
            isRunning = true;//from w ww . j ava 2  s.c o  m
        }
    }
    return isRunning;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static int getDensityOptimizedIconSize(final Context context) {
    int iconSize;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        iconSize = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE))
                .getLauncherLargeIconSize();
    } else {//from   w  w  w .ja va 2s.com
        iconSize = (int) context.getResources().getDimension(android.R.dimen.app_icon_size);
    }
    return iconSize;
}

From source file:Main.java

@TargetApi(VERSION_CODES.KITKAT)
public static boolean isLowMemory(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    return (VERSION.SDK_INT >= VERSION_CODES.KITKAT && activityManager.isLowRamDevice())
            || activityManager.getMemoryClass() <= 64;
}

From source file:Main.java

/**
 * Get package name of the process id./*from  w ww .j  a v a 2 s  . c  o m*/
 * @param context
 * @param pid
 * @return
 */
public static final String getPackageNameFromPid(Context context, int pid) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> processes = am.getRunningAppProcesses();
    for (RunningAppProcessInfo info : processes) {
        if (info.pid == pid) {
            String[] packages = info.pkgList;
            if (packages.length > 0) {
                return packages[0];
            }
            break;
        }
    }
    return null;
}

From source file:Main.java

/**
 * get top activity name/*from   w w w .  j a va2  s  . com*/
 *
 * @param context context of activity
 * @return top activity name
 */
public static String getTopActivity(Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    // Note: getRunningTasks is deprecated in API 21(Official)
    List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
    if (runningTaskInfos != null)
        return (runningTaskInfos.get(0).topActivity).toString();
    else
        return null;
}

From source file:Main.java

/**
 * Check for a running service/*  ww  w. j  a  va  2 s . c  o m*/
 *
 * @param ctx                   the application context
 * @param serviceClassName      the service class name
 * @return      true if service is running, false overwise
 */
public static boolean checkForRunningService(Context ctx, String serviceClassName) {
    ActivityManager manager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClassName.equals(service.service.getClassName())) {
            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++;// www  . j  a  v  a2s  .com
            } 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;
}