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) {
    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();
    }/*w  ww .  ja  v a 2s. c o m*/
    return topActivityClassName;
}

From source file:Main.java

/**
 * Indicates if the service given by <tt>activityClass</tt> is currently
 * running.// www.  j a  v  a2 s.c o m
 *
 * @param context the Android context
 * @param activityClass the activity class to check
 * @return <tt>true</tt> if the activity given by the class is running,
 * <tt>false</tt> - otherwise
 */
public static boolean isActivityRunning(Context context, Class<?> activityClass) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    List<ActivityManager.RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);

    boolean isServiceFound = false;

    for (int i = 0; i < services.size(); i++) {
        if (services.get(i).topActivity.getClassName().equals(activityClass.getName())) {
            isServiceFound = true;
        }
    }
    return isServiceFound;
}

From source file:Main.java

/**
 * Check if the Jarvis services are running.
 * @param context - the context of the calling application.
 * @param serviceName - the name of the service.
 * @return <b>True</b> if the service is running,<br/><b>False</b> if the service is not running.
 * @author Vibhor/*from  w  w  w . j  av a2  s  .c  om*/
 */
public static boolean isIrisServiceRunning(Context context, String serviceName) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceName.equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * Returns {@code true} if the current process is main.
 *
 * @param context The instance of {@link android.content.Context}.
 * @return {@code true} if the current process is main.
 */// w  ww.  j av  a 2s .  co  m
public static boolean isMainProcess(@NonNull final Context context) {
    final int currentPid = android.os.Process.myPid();
    final ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    final List<RunningAppProcessInfo> runningProcesses = activityManager.getRunningAppProcesses();

    String currentProcessName = null;
    for (RunningAppProcessInfo process : runningProcesses) {
        if (process.pid == currentPid) {
            currentProcessName = process.processName;
            break;
        }
    }

    return context.getPackageName().equals(currentProcessName);
}

From source file:Main.java

/**
 * Checks whether the recording service is currently running.
 * //from ww w . jav  a2 s.  c  o  m
 * @param ctx
 *            the current context
 * @return true if the service is running, false otherwise
 */
public static boolean isServiceRunning(final Context ctx, final Class<?> cls) {
    final ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
    final List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);

    for (final RunningServiceInfo serviceInfo : services) {
        final ComponentName componentName = serviceInfo.service;
        final String serviceName = componentName.getClassName();
        if (serviceName.equals(cls.getName())) {
            return true;
        }
    }
    return false;
}

From source file:com.binkery.app.filemanager.fragments.ThumbnailLoader.java

public static ThumbnailLoader getInstance(Context context) {
    if (mLoader == null) {
        mLoader = new ThumbnailLoader();
        mLoader.mContext = context;// w  w  w. j  a v a  2 s .c  om
        final int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE))
                .getMemoryClass();

        // Use 1/8th of the available memory for this memory cache.
        final int cacheSize = 1024 * 1024 * memClass / 8;
        mLoader.mCache = new LruCache<String, Bitmap>(cacheSize);
        mLoader.mTaskList = new ArrayList<ThumbnailLoader.TaskItem>();
    }
    return mLoader;
}

From source file:Main.java

/**
 * Checks if the application is in the background (i.e behind another application's Activity).
 *
 * @param context {@link Context}/*from w  w w . j  av  a 2 s  . c  o  m*/
 * @return true if another application is above this one.
 */
public static boolean isApplicationBroughtToBackground(final Context context) {
    if (context.checkCallingOrSelfPermission(Manifest.permission.GET_TASKS) == PackageManager.PERMISSION_DENIED)
        return false;

    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())
                && !topActivity.getPackageName().equals("com.google.android.voicesearch")) {
            return true;
        }
    }

    return false;
}

From source file:Main.java

public static void killProcesses(Context context, int pid, String processName) {
    /*String cmd = "kill -9 "+pid;
    Process process = null;//ww w  .j a  v a 2s .c om
    DataOutputStream os = null;
    try {
      process = Runtime.getRuntime().exec("su"); 
      os = new DataOutputStream(process.getOutputStream());
      os.writeBytes(cmd + "\n");
      os.writeBytes("exit\n");
      os.flush();
      process.waitFor();
    } catch (Exception e) {
      e.printStackTrace();
    }
    AbLogUtil.d(AbAppUtil.class, "#kill -9 "+pid);*/

    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    String packageName = null;
    try {
        if (processName.indexOf(":") == -1) {
            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 long[] getPrivDirty(Context context, int pid) {

    ActivityManager mAm = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    int[] pids = new int[1];
    pids[0] = pid;/*from ww w.java  2s.  c o  m*/
    MemoryInfo[] memoryInfoArray = mAm.getProcessMemoryInfo(pids);
    MemoryInfo pidMemoryInfo = memoryInfoArray[0];
    long[] value = new long[3]; // Natvie Dalvik Total
    value[0] = pidMemoryInfo.nativePrivateDirty;
    value[1] = pidMemoryInfo.dalvikPrivateDirty;
    value[2] = pidMemoryInfo.getTotalPrivateDirty();
    return value;
}

From source file:Main.java

/**
 * Get Process Name by getRunningAppProcesses
 * <p/>//from  w ww  .j  ava  2  s . c  o  m
 * It's been reported that sometimes, the list returned from
 * getRunningAppProcesses simply doesn't contain your own process
 * (especially when called from Application).
 * Use {@link #getProcessName(Context, int)} instead
 */
public static String getProcessName_PM(Context context, int pID) {
    String processName = "";
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List l = am.getRunningAppProcesses();
    Iterator i = l.iterator();
    PackageManager pm = context.getPackageManager();
    while (i.hasNext()) {
        ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
        try {
            if (info.pid == pID) {
                return info.processName;
            }
        } catch (Exception e) {
            Log.e(TAG, "getAppName: error", e);
        }
    }
    return processName;
}