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 isApplicationBroughtToBackground(final Context context) {
    try {/* w  w  w .j  av a2  s .c o  m*/
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(context.getPackageName())) {
                return true;
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return false;
}

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 servicesList = activityManager.getRunningServices(2147483647);
    Iterator l = servicesList.iterator();
    while (l.hasNext()) {
        ActivityManager.RunningServiceInfo si = (ActivityManager.RunningServiceInfo) l.next();
        if (className.equals(si.service.getClassName())) {
            isRunning = true;/*  w  ww  .j ava 2 s.c o  m*/
        }
    }
    return isRunning;
}

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<ActivityManager.RunningServiceInfo> servicesList = activityManager
            .getRunningServices(Integer.MAX_VALUE);
    Iterator<ActivityManager.RunningServiceInfo> l = servicesList.iterator();
    while (l.hasNext()) {
        ActivityManager.RunningServiceInfo si = (ActivityManager.RunningServiceInfo) l.next();
        if (className.equals(si.service.getClassName())) {
            isRunning = true;//from ww w .j  a v  a 2 s. co m
        }
    }
    return isRunning;
}

From source file:Main.java

private static ComponentName getBaseActivity(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (am == null) {
        return null;
    }/*from   ww w  . j a  v a 2  s .  co  m*/
    List<RunningTaskInfo> info = am.getRunningTasks(1);
    if (info == null) {
        return null;
    }
    ComponentName activity = info.get(0).baseActivity;
    return activity;
}

From source file:Main.java

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

    }
    return null;
}

From source file:Main.java

public static String getProcessName(Context context) {
    int pid = android.os.Process.myPid();
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
        if (appProcess.pid == pid) {
            return appProcess.processName;
        }//  w  w  w . j  a va2 s  . c o m
    }
    return null;
}

From source file:Main.java

public static void forceStopPackage(Context ctx, String packageName) {

    ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);

    Method method;/*  ww  w.  j ava 2 s  . com*/
    try {
        method = Class.forName("android.app.ActivityManager").getMethod("forceStopPackage", String.class);
        method.invoke(am, packageName);
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * get usable memory for device//from   w  w  w  .  j av a2 s.c o  m
 *
 * @param context
 * @return current usable memory
 */
public static int getDeviceUsableMemory(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
    activityManager.getMemoryInfo(mi);
    return (int) (mi.availMem / (1024 * 1024));
}

From source file:Main.java

public static boolean isServiceRunning(Context context, String serviceName) {
    if (TextUtils.isEmpty(serviceName)) {
        return false;
    }//  ww  w  . jav a  2  s.  c  o  m
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> mServiceList = manager.getRunningServices(100);
    for (int i = 0; i < mServiceList.size(); i++) {
        String clasString = mServiceList.get(i).service.getClassName();
        if (clasString.equals(serviceName)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static String getCurProcessName(Context context) {
    int pid = android.os.Process.myPid();
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    if (activityManager == null) {
        return null;
    }/*w  w  w.j  a v  a  2s  .c  o m*/

    List<RunningAppProcessInfo> runningProcessList = activityManager.getRunningAppProcesses();
    if (runningProcessList == null || runningProcessList.isEmpty()) {
        return null;
    }

    for (ActivityManager.RunningAppProcessInfo appProcess : runningProcessList) {
        if (appProcess.pid == pid) {
            return appProcess.processName;
        }
    }
    return null;
}