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 getTopActivityName(Context context) {
    if (context == null) {
        return "";
    }//from   w  ww . j  av a  2 s  . co m
    String topActivityClassName = null;
    try {
        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;
            topActivityClassName = f.getClassName();
        }
    } catch (Exception e) {
    }
    return topActivityClassName;

}

From source file:Main.java

/**
 * Determine if the app is currently visible to the user
 *
 * @param context// w  w  w.  ja  v a2  s . c  om
 * @return boolean Return true if the app is visible to the user otherwise false
 */
public static boolean isAppInForeground(Context context) {
    final List<RunningTaskInfo> task = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE))
            .getRunningTasks(1);
    if (task.isEmpty() && task.size() > 0) {
        return false;
    }
    return task.get(0).topActivity.getPackageName().equalsIgnoreCase(context.getPackageName());
}

From source file:Main.java

/**
 * Gets the amount of memory available to the application.
 *
 * Queries whether the largeHeap manifest flag is set.
 *
 * @param context/*from w w w.  jav a 2  s  .  c om*/
 *         the application context
 * @return the amount of memory in bytes available to the application
 */
private static int getAvailableMemory(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    boolean largeHeap = (context.getApplicationInfo().flags & 1048576) != 0;
    int memoryClass = largeHeap ? activityManager.getLargeMemoryClass() : activityManager.getMemoryClass();
    return memoryClass * 1048576;
}

From source file:Main.java

public static boolean isNamedProcess(Context context, String processName) {
    if (context == null || TextUtils.isEmpty(processName)) {
        return false;
    }//from  w  w  w. ja  va 2  s  .  co  m

    int pid = android.os.Process.myPid();
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> processInfoList = manager.getRunningAppProcesses();
    if (processInfoList == null) {
        return true;
    }

    for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
        if (processInfo.pid == pid && processName.equalsIgnoreCase(processInfo.processName)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean isAppOnBackground(Context context) {
    // Returns a list of application processes that are running on the
    // device/*  w w w .  j  ava2s.co  m*/

    ActivityManager activityManager = (ActivityManager) context.getApplicationContext()
            .getSystemService(Context.ACTIVITY_SERVICE);
    String packageName = context.getApplicationContext().getPackageName();

    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null)
        return false;

    for (RunningAppProcessInfo appProcess : appProcesses) {
        // The name of the process that this object is associated with.
        if (appProcess.processName.equals(packageName)
                && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean isAppOnForeground(Context context) {
    // Returns a list of application processes that are running on the
    // device//from  ww w.  j  av  a 2  s  . c om

    ActivityManager activityManager = (ActivityManager) context.getApplicationContext()
            .getSystemService(Context.ACTIVITY_SERVICE);
    String packageName = context.getApplicationContext().getPackageName();

    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null)
        return false;

    for (RunningAppProcessInfo appProcess : appProcesses) {
        // The name of the process that this object is associated with.
        if (appProcess.processName.equals(packageName)
                && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * Check whether my service is running.//  w w w .ja  va 2 s .  com
 * @param context application's context
 * @param serviceClass class of the service to look for
 * @return true if the service is running, false otherwise
 */
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;
    return false;
}

From source file:Main.java

/**
 * check whether specific service is running.
 * @param context//from w w  w .  jav  a2  s .  c o  m
 * @param serviceClassFullName the full class name of service
 * @return true if service is running
 */
public static boolean isServiceRunning(Context context, String serviceClassFullName) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> runningServices = manager.getRunningServices(1024);
    for (ActivityManager.RunningServiceInfo info : runningServices) {
        if (info.getClass().getName().equals(serviceClassFullName))
            return true;
    }
    return false;
}

From source file:Main.java

public static boolean isServiceRunning(Context context, String className) {
    if (context == null)
        return false;
    if (TextUtils.isEmpty(className))
        return false;
    boolean isRunning = false;
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> servicesList = activityManager
            .getRunningServices(Integer.MAX_VALUE);
    for (ActivityManager.RunningServiceInfo si : servicesList) {
        if (className.equals(si.service.getClassName())) {
            isRunning = true;//from   w ww . j a va  2  s .co  m
        }
    }
    return isRunning;
}

From source file:Main.java

/**
 * get available size of ram. Work for API 16 and above.
 * @return available size in byte/*from www . j  av  a  2s  .c o  m*/
 */
public static long getAvailableRamSize(Context context) {
    if (am == null) {
        am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    }
    am.getMemoryInfo(outInfo);
    return outInfo.availMem;
}