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

static public boolean isAppInForeground(Context context) {
    List<ActivityManager.RunningTaskInfo> tasks = ((ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1);
    if (tasks.isEmpty()) {
        return false;
    }// ww  w .  j a  v a  2s  .  co m
    return tasks.get(0).topActivity.getPackageName().equalsIgnoreCase(context.getPackageName());
}

From source file:Main.java

/**
 * Gets the openGL version that is actually available on this device (ie GL_VERSION_31).
 *
 * @param context// w  ww  . ja  va 2  s  . c  o  m
 * @return
 */
public static int getDeviceGLVersion(Context context) {
    final ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
    return configurationInfo.reqGlEsVersion;
}

From source file:Main.java

private static ComponentName getTopActivity(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (am == null) {
        return null;
    }//w  w w.ja v  a 2s  . c o  m
    List<RunningTaskInfo> info = am.getRunningTasks(1);
    if (info == null) {
        return null;
    }
    ComponentName activity = info.get(0).topActivity;
    return activity;
}

From source file:Main.java

public static int getMemoryClass(Context context) {
    return ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
}

From source file:Main.java

public static String getActivityName(Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.RunningTaskInfo info = manager.getRunningTasks(1).get(0);
    String shortClassName = info.topActivity.getShortClassName();
    System.out.println("shortClassName=" + shortClassName);
    return shortClassName;
}

From source file:Main.java

public static int getScreenDensity(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    return activityManager.getLauncherLargeIconDensity();
}

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  ww  .j a  v  a2s  . co  m*/
    }
    return false;
}

From source file:Main.java

/**
 * whether application is in background//from   ww  w. j  a v a  2 s . c  o m
 * <ul>
 * <li>need use permission android.permission.GET_TASKS in Manifest.xml</li>
 * </ul>
 * 
 * @param context
 * @return if application is in background return true, otherwise return
 *         false
 */
public static boolean isApplicationInBackground(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<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

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;
        }/*from ww  w  .j  a  va 2s .c  o  m*/
    }
    return false;
}

From source file:Main.java

/**
 * Function to check if a Service is running.
 *
 * @param serviceClass The service's class.
 * @return true if running, else false./*  w  ww.ja va 2s.  co  m*/
 */
private static boolean serviceIsRunning(Class<?> serviceClass, Activity activity) {
    ActivityManager manager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);

    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}